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

时间:2021-06-24 23:16:55

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

持久化类:

@Entity
public class Spitter {
private long id;
private String userName, passWord, fullName;

public Spitter(long id, String n, String p, String f){
this.id = id;
this.userName = n;
this.passWord = p;
this.fullName = f;
}
public Spitter(){}
public void setId(long id){
this.id = id;
}
@Id
public long getId(){
return id;
}
public String getUserName(){
return this.userName;
}
public void setUserName(String n){
this.userName = n;
}
public String getPassWord(){
return this.passWord;
}
public void setPassWord(String p){
this.passWord = p;
}
public String getFullName(){
return this.fullName;
}
public void setFullName(String f){
this.fullName = f;
}

}


数据源(在spring配置文件中配置):

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spitter" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="initialSize" value="5"/>
<property name="maxActive" value="10" />
</bean>

SessionFactory类(在spring配置文件中配置):

 <bean id="sessionFactory" 
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>Spitter.spitterOne.Spitter</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>

</props>
</property>

</bean>
配置hibernate事务:

 <!-- 设定transactionManager -->  
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!--启动spring事务注解功能-->
<tx:annotation-driven transaction-manager="txManager"/>


进行持久化操作的DAO类:

@Repository
public class HibernateSpitterDao implements SpitterDAO {
private SessionFactory sessionFactory;

@Autowired
public HibernateSpitterDao(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
private Session currentSession(){
return this.sessionFactory.getCurrentSession();
}
/**
* 进行持久化的方法需要使用@Transactional进行事务管理
*/
@Transactional(readOnly = false, rollbackFor = RuntimeException.class)
public void addSpitter(Spitter spitter){
this.currentSession().save(spitter);
}
public Spitter getSpitterById(long id){
return (Spitter)this.currentSession().get(Spitter.class, id);
}
@Transactional(readOnly = false, rollbackFor = RuntimeException.class)
public void saveSpitter(Spitter spitter){
this.currentSession().update(spitter);
}
public static void main(String [] args){
Spitter ss = new Spitter(103,"zhang sfdasf454352an", "cccninini", "zhang shan fdasfsdfewe");

ApplicationContext ctx = new ClassPathXmlApplicationContext("Spitter/spitterOne/spring-idol.xml");
<span style="color:#ff0000;">SpitterDAO dao = (SpitterDAO) ctx.getBean("hibernateSpitterDao");</span>
dao.addSpitter(ss);

}
}


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



相关文章