TIPS:
How to configure the source code to the relevant jar package in MyEclipse:
Jar package --- propreties ------ JavaSourceAttachment ------ External File ---- fileof source. jar
1. Compile jdbc. properties to configure jdbc parameters.
[Html]
Jdbc. driverClassName = com. mysql. jdbc. Driver
Jdbc. url = jdbc: mysql: // localhost: 3306/sms
Jdbc. username = root
Jdbc. password = root
2. Compile the Javabean and ing files:
[Java]
JavaBean:
Package com. spring. model;
Public class User {
Private int id;
Private String name;
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
}
Ing file:
[Html]
<? Xml version = "1.0"?>
<! DOCTYPE hibernate-mapping PUBLIC
"-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<Hibernate-mapping
Package = "com. spring. model">
<Class name = "User">
<Id name = "id">
<Generator class = "native"/>
</Id>
<Property name = "name"/>
</Class>
</Hibernate-mapping>
3. Configure spring container to initialize sessionFactory
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>
<Context: annotation-config/>
<! -- Configure the container resource scan package -->
<Context: component-scan base-package = "com. spring"/>
<! -- Write the front class to the container -->
<Bean id = "logInterceptor" class = "com. spring. aop. LogInterceptor"/>
<! --
Configure the data source <bean id = "myDataSource"
Class = "org. apache. commons. dbcp. BasicDataSource"
Destroy-method = "close"> <property name = "driverClassName"
Value = "com. mysql. jdbc. Driver"/> <property name = "url"
Value = "jdbc: mysql: // localhost: 3306/sms"/> <property name = "username"
Value = "root"/> <property name = "password" value = "root"/> </bean>
-->
<! -- Placeholder -->
<Bean
Class = "org. springframework. beans. factory. config. PropertyPlaceholderConfigurer">
<Property name = "locations">
<Value> classpath: jdbc. properties </value>
</Property>
</Bean>
<! -- Configure dataSource -->
<Bean id = "dataSource" destroy-method = "close"
Class = "org. apache. commons. dbcp. BasicDataSource">
<Property name = "driverClassName" value = "$ {jdbc. driverClassName}"/>
<Property name = "url" value = "$ {jdbc. url}"/>
<Property name = "username" value = "$ {jdbc. username}"/>
<Property name = "password" value = "$ {jdbc. password}"/>
</Bean>
<! -- Inject the configured dataSource into SessionFactory -->
<Bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean">
<Property name = "dataSource" ref = "dataSource"/>
<Property name = "mappingResources">
<List>
<Value> com/spring/model/user. hbm. xml </value>
</List>
</Property>
<Property name = "hibernateProperties">
<Value>
Hibernate. dialect = org. hibernate. dialect. MySQLDialect
Hibernate. show_ SQL = true
Hibernate. hbm2ddl. auto = create
</Value>
</Property>
</Bean>
</Beans>
4. Compile the test class:
[Java]
Package com. spring. test;
Import org. junit. Test;
Import org. springframework. beans. factory. BeanFactory;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Import com. spring. service. UserService;
Public class SpringTest {
@ Test
Public void test01 (){
BeanFactory applicationContext = new ClassPathXmlApplicationContext (
"Beans. xml ");
UserService user = (UserService) applicationContext. getBean ("userService ");
User. save ();
}
}
Required hibernate Jar package:
Required spring jar package:
Author: zhang6622056