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

整合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接口。



  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WitsMakeMen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值