使用注解方式进行spring和hibernate整合

来源:互联网 发布:lol末日人工智能攻略 编辑:程序博客网 时间:2024/05/17 03:19

整合spring和hibernate需要五个要素,分别包括持久化的类, 数据源,SessionFactory, TransactionManager和持久化操作的DAO类。

持久化类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @Entity  
  2. public class Spitter {  
  3.     private long id;  
  4.     private String userName, passWord, fullName;  
  5.       
  6.     public Spitter(long id, String n, String p, String f){  
  7.         this.id = id;  
  8.         this.userName = n;  
  9.         this.passWord = p;  
  10.         this.fullName = f;  
  11.     }  
  12.     public Spitter(){}  
  13.     public void setId(long id){  
  14.         this.id = id;  
  15.     }  
  16.     @Id  
  17.     public long getId(){  
  18.         return id;  
  19.     }  
  20.     public String getUserName(){  
  21.         return this.userName;  
  22.     }  
  23.     public void setUserName(String n){  
  24.         this.userName = n;  
  25.     }  
  26.     public String getPassWord(){  
  27.         return this.passWord;  
  28.     }  
  29.     public void setPassWord(String p){  
  30.         this.passWord = p;  
  31.     }  
  32.     public String getFullName(){  
  33.         return this.fullName;  
  34.     }  
  35.     public void setFullName(String f){  
  36.         this.fullName = f;  
  37.     }  
  38.   
  39. }  


数据源(在spring配置文件中配置):
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >  
  2.     <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  3.     <property name="url" value="jdbc:mysql://localhost:3306/spitter" />  
  4.     <property name="username" value="root" />  
  5.     <property name="password" value="root" />  
  6.     <property name="initialSize" value="5"/>  
  7.     <property name="maxActive" value="10" />  
  8. </bean>  

SessionFactory类(在spring配置文件中配置):
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <bean id="sessionFactory"   
  2.     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  3.     <property name="dataSource" ref="dataSource" />  
  4.     <property name="annotatedClasses">  
  5.         <list>  
  6.             <value>Spitter.spitterOne.Spitter</value>  
  7.         </list>  
  8.     </property>  
  9.   
  10.     <property name="hibernateProperties">  
  11.         <props>  
  12.             <prop key="dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>  
  13.             <prop key="hibernate.show_sql">true</prop>  
  14.             <prop key="hibernate.format_sql">true</prop>  
  15.             <prop key="hibernate.hbm2ddl.auto">update</prop>  
  16.               
  17.         </props>  
  18.     </property>  
  19.       
  20. </bean>  
配置hibernate事务:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!-- 设定transactionManager -->    
  2.     <bean id="txManager"    
  3.        class="org.springframework.orm.hibernate3.HibernateTransactionManager">    
  4.        <property name="sessionFactory" ref="sessionFactory" />    
  5.     </bean>    
  6.    
  7.    <!--启动spring事务注解功能-->    
  8.    <tx:annotation-driven transaction-manager="txManager"/>  


进行持久化操作的DAO类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @Repository  
  2. public class HibernateSpitterDao implements SpitterDAO {  
  3.     private SessionFactory sessionFactory;  
  4.       
  5.     @Autowired  
  6.     public HibernateSpitterDao(SessionFactory sessionFactory){  
  7.         this.sessionFactory = sessionFactory;  
  8.     }  
  9.     private Session currentSession(){  
  10.         return this.sessionFactory.getCurrentSession();  
  11.     }  
  12.     /** 
  13.      * 进行持久化的方法需要使用@Transactional进行事务管理 
  14.      */  
  15.     @Transactional(readOnly = false, rollbackFor = RuntimeException.class)  
  16.     public void addSpitter(Spitter spitter){  
  17.         this.currentSession().save(spitter);  
  18.     }  
  19.     public Spitter getSpitterById(long id){  
  20.         return (Spitter)this.currentSession().get(Spitter.class, id);  
  21.     }  
  22.     @Transactional(readOnly = false, rollbackFor = RuntimeException.class)  
  23.     public void saveSpitter(Spitter spitter){  
  24.         this.currentSession().update(spitter);  
  25.     }  
  26.     public static void main(String [] args){  
  27.         Spitter ss = new Spitter(103,"zhang sfdasf454352an""cccninini""zhang shan fdasfsdfewe");  
  28.       
  29.         ApplicationContext ctx = new ClassPathXmlApplicationContext("Spitter/spitterOne/spring-idol.xml");  
  30.         SpitterDAO dao = (SpitterDAO) ctx.getBean("hibernateSpitterDao");  
  31.         dao.addSpitter(ss);  
  32.           
  33.     }  
  34. }  


因为Spring只能对接口进行aop操作,所以红色代码部分只能将hibernateSpitterDao强制转换成SpitterDAO接口。

0 0
原创粉丝点击