Spring中hibernateTemplate的使用

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.      xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.            http://www.springframework.org/schema/context  
  9.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.            http://www.springframework.org/schema/tx  
  11.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  12.            http://www.springframework.org/schema/aop  
  13.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  14.     <context:annotation-config />  
  15.     <!-- 配置容器资源扫描的包 -->  
  16.     <context:component-scan base-package="com.spring" />  
  17.     <!-- 将前面类写入容器 -->  
  18.     <bean id="logInterceptor" class="com.spring.aop.LogInterceptor" />  
  19.   
  20.   
  21.   
  22.     <!--  
  23.         配置数据源 <bean id="myDataSource"  
  24.         class="org.apache.commons.dbcp.BasicDataSource"  
  25.         destroy-method="close"> <property name="driverClassName"  
  26.         value="com.mysql.jdbc.Driver"/> <property name="url"  
  27.         value="jdbc:mysql://localhost:3306/sms"/> <property name="username"  
  28.         value="root"/> <property name="password" value="root"/> </bean>  
  29.     -->  
  30.   
  31.       
  32.     <!-- placeholder 占位符 -->  
  33.     <bean  
  34.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  35.         <property name="locations">  
  36.             <value>classpath:jdbc.properties</value>  
  37.         </property>  
  38.     </bean>  
  39.     <!-- 配置dataSource -->  
  40.     <bean id="dataSource" destroy-method="close"  
  41.         class="org.apache.commons.dbcp.BasicDataSource">  
  42.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  43.         <property name="url" value="${jdbc.url}" />  
  44.         <property name="username" value="${jdbc.username}" />  
  45.         <property name="password" value="${jdbc.password}" />  
  46.     </bean>  
  47.   
  48.     <!-- 将配置好的dataSource注入到SessionFactory中-->  
  49.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  50.     <property name="dataSource" ref="dataSource"/>  
  51.    <property name="mappingResources">  
  52.       <list>  
  53.         <value>com/spring/model/user.hbm.xml</value>  
  54.         <value>com/spring/model/userlog.hbm.xml</value>  
  55.       </list>  
  56.     </property>  
  57.     <property name="hibernateProperties">  
  58.       <value>  
  59.         hibernate.dialect=org.hibernate.dialect.MySQLDialect  
  60.         hibernate.show_sql=true  
  61.         hibernate.hbm2ddl.auto=create  
  62.       </value>  
  63.     </property>  
  64.   </bean>  
  65.       
  66.   
  67.   
  68.   
  69. <!-- 声明式事务管理,事务需要数据源,从sessionFactory中拿到  
  70. 这是一个AOP的应用 -->  
  71. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  72.   <property name="sessionFactory" ref="sessionFactory" />  
  73. </bean>  
  74.   
  75. <!-- 配置事务要管理的方法 -->  
  76. <tx:advice transaction-manager="transactionManager" id="txManager">  
  77.     <tx:attributes>  
  78.         <tx:method name="save"/>  
  79.     </tx:attributes>  
  80. </tx:advice>  
  81.       
  82.   
  83. <!-- 配置aop设置切面和织入点逻辑 -->  
  84. <aop:config>  
  85. <aop:pointcut id="entryPointMethod" expression="execution(public * com.spring.service..*.*(..))"/>  
  86. <aop:advisor  
  87.        advice-ref="txManager"  
  88.        pointcut-ref="entryPointMethod"  
  89.         />  
  90. </aop:config>  
  91.   
  92. <!-- 配置一个hibernateTemplate -->  
  93. <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
  94.     <property name="sessionFactory" ref="sessionFactory"></property>  
  95. </bean>  
  96.   
  97. </beans>  

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

  1. @Component("userDaoImpl")  
  2. public class UserDaoImpl implements UserDao{  
  3.     @Resource   
  4.     private HibernateTemplate hibernateTemplate;  
  5.   
  6.     public HibernateTemplate getHibernateTemplate() {  
  7.         return hibernateTemplate;  
  8.     }  
  9.   
  10.     public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {  
  11.         this.hibernateTemplate = hibernateTemplate;  
  12.     }  
  13.       
  14.     @Override  
  15.     public void save(User u) {  
  16.         try {  
  17.             hibernateTemplate.save(u);  
  18.         } catch (HibernateException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  

转载于:https://my.oschina.net/huewenhua/blog/681541

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值