Configuration and use of HibernateTemplate:
1. Configure the bean file: Because sessionFactory is required, it will be copied.
[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"
Xmlns: tx = "http://www.springframework.org/schema/tx"
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/tx
Http://www.springframework.org/schema/tx/spring-tx-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>
<Value> com/spring/model/userlog. 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>
<! -- Declarative transaction management: the transaction needs a data source, which is obtained from sessionFactory.
This is an application of AOP -->
<Bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager">
<Property name = "sessionFactory" ref = "sessionFactory"/>
</Bean>
<! -- Configure the method for managing transactions -->
<Tx: advice transaction-manager = "transactionManager" id = "txManager">
<Tx: attributes>
<Tx: method name = "save"/>
</Tx: attributes>
</Tx: advice>
<! -- Configure aop to set the cut and weaving point logic -->
<Aop: config>
<Aop: pointcut id = "entryPointMethod" expression = "execution (public * com. spring. service... * (...)"/>
<Aop: advisor
Advice-ref = "txManager"
Pointcut-ref = "entryPointMethod"
/>
</Aop: config>
<! -- Configure a hibernateTemplate -->
<Bean id = "hibernateTemplate" class = "org. springframework. orm. hibernate3.HibernateTemplate">
<Property name = "sessionFactory" ref = "sessionFactory"> </property>
</Bean>
</Beans>
2. Use @ resource injection and then call the save method:
[Java]
@ Component ("userDaoImpl ")
Public class UserDaoImpl implements UserDao {
@ Resource
Private HibernateTemplate hibernateTemplate;
Public HibernateTemplate getHibernateTemplate (){
Return hibernateTemplate;
}
Public void setHibernateTemplate (HibernateTemplate hibernateTemplate ){
This. hibernateTemplate = hibernateTemplate;
}
@ Override
Public void save (User u ){
Try {
HibernateTemplate. save (u );
} Catch (HibernateException e ){
E. printStackTrace ();
}
}
}
Author: zhang6622056