Spring中hibernateTemplate的使用

  1. <beans xmlns="http://www.springframework.org/schema/beans"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

  3. xmlns:aop="http://www.springframework.org/schema/aop"

  4. xmlns:tx="http://www.springframework.org/schema/tx"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans

  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  7. http://www.springframework.org/schema/context

  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  9. http://www.springframework.org/schema/tx

  10. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

  11. http://www.springframework.org/schema/aop

  12. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"\>

  13. <context:annotation-config />

  14. <context:component-scan base-package="com.spring" />

  15. <bean id="logInterceptor" class="com.spring.aop.LogInterceptor" />

  16. <bean

  17. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  18. <property name="locations">

  19. <value>classpath:jdbc.properties</value>

  20. </property>

  21. </bean>

  22. <bean id="dataSource" destroy-method="close"

  23. class="org.apache.commons.dbcp.BasicDataSource">

  24. <property name="driverClassName" value="${jdbc.driverClassName}" />

  25. <property name="url" value="${jdbc.url}" />

  26. <property name="username" value="${jdbc.username}" />

  27. <property name="password" value="${jdbc.password}" />

  28. </bean>

  29. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  30. <property name="dataSource" ref="dataSource"/>

  31. <property name="mappingResources">

  32. <list>

  33. <value>com/spring/model/user.hbm.xml</value>

  34. <value>com/spring/model/userlog.hbm.xml</value>

  35. </list>

  36. </property>

  37. <property name="hibernateProperties">

  38. <value>

  39. hibernate.dialect=org.hibernate.dialect.MySQLDialect

  40. hibernate.show_sql=true

  41. hibernate.hbm2ddl.auto=create

  42. </value>

  43. </property>

  44. </bean>

  45. <tx:advice transaction-manager="transactionManager" id="txManager">

  46. tx:attributes\

  47. <tx:method name="save"/>

  48. </tx:attributes>

  49. </tx:advice>

  50. aop:config\

  51. <aop:pointcut id="entryPointMethod" expression="execution(public * com.spring.service..*.*(..))"/>

  52. <aop:advisor

  53. advice-ref="txManager"

  54. pointcut-ref="entryPointMethod"

  55. />

  56. </aop:config>

  57. <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

  58. <property name="sessionFactory" ref="sessionFactory"></property>

  59. </bean>

  60. </beans>

2、使用@resource 注入,然后调用save方法:

[java] view plain copy

  1. @Component("userDaoImpl")

  2. public class UserDaoImpl implements UserDao{

  3. @Resource

  4. private HibernateTemplate hibernateTemplate;

  5. public HibernateTemplate getHibernateTemplate() {

  6. return hibernateTemplate;

  7. }

  8. public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {

  9. this.hibernateTemplate = hibernateTemplate;

  10. }

  11. @Override

  12. public void save(User u) {

  13. try {

  14. hibernateTemplate.save(u);

  15. } catch (HibernateException e) {

  16. e.printStackTrace();

  17. }

  18. }

  19. }

评论

暂无评论