(9)Spring和Hibernate整合
admin
2023-06-08 01:21:30
0


Spring与Hibernate整合的关键点:

1) Hibernate的SessionFactory对象交给Spring创建;

2) Hibernate事务交给Spring的声明式事务管理。

 

Spring和Hibernate整合的步骤:

1)引入jar包

2)配置:hibernate.cfg.xml、*.hbm.xml、applicationContext.xml

3)搭建环境、单独测试

 

1、引入jar包


hibernate3相关jar包

hibernate3.jar

antlr-2.7.6.jar (required)

commons-collections-3.1.jar (required)

dom4j-1.6.1.jar (required)

javassist-3.12.0.GA.jar (required)

jta-1.1.jar (required)

slf4j-api-1.6.1.jar (required)

hibernate-jpa-2.0-api-1.0.0.Final.jar (jpa)

 

spring-core相关jar包

commons-logging-1.2.jar

spring-beans-3.2.5.RELEASE.jar

spring-context-3.2.5.RELEASE.jar

spring-core-3.2.5.RELEASE.jar

spring-expression-3.2.5.RELEASE.jar

 

spring-aop相关jar包

aopalliance-.jar

aspectjrt.jar

aspectjweaver.jar

spring-aop-3.2.5.RELEASE.jar

 

spring-jdbc相关jar包

spring-jdbc-3.2.5.RELEASE.jar

spring-tx-3.2.5.RELEASE.jar  【事务相关】

 

spring-orm相关jar包

spring-orm-3.2.5.RELEASE.jar  【spring对hibernate的支持】

 

c3p0相关jar包

c3p0-0.9.1.2.jar

 

mysql相关jar包

mysql-connector-java-5.1.38-bin.jar

 


2、测试Hibernate


hibernate.cfg.xml 





    
    
        
        com.mysql.jdbc.Driver
        jdbc:mysql:///test	
        root
        root
		
        org.hibernate.dialect.MySQL5Dialect
        
        
		
		true
		
		false
		
		update
		
		
		thread

		
				

    

Dept.hbm.xml




	
		
			
		
		
		
	

Dept.java

package com.rk.entity;

public class Dept
{
	private int deptId;
	private String deptName;
	private int deptVersion;
	public int getDeptId()
	{
		return deptId;
	}
	public void setDeptId(int deptId)
	{
		this.deptId = deptId;
	}
	public String getDeptName()
	{
		return deptName;
	}
	public void setDeptName(String deptName)
	{
		this.deptName = deptName;
	}
	public int getDeptVersion()
	{
		return deptVersion;
	}
	public void setDeptVersion(int deptVersion)
	{
		this.deptVersion = deptVersion;
	}
	@Override
	public String toString()
	{
		return "Dept [deptId=" + deptId + ", deptName=" + deptName + ", deptVersion=" + deptVersion + "]";
	}
	
}

DeptDao.java

package com.rk.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.rk.entity.Dept;

public class DeptDao
{
	private static SessionFactory sf;
	static
	{
		sf = new Configuration().configure().buildSessionFactory();
	}
	
	public Dept findById(int id)
	{
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		
		Dept dept = (Dept) session.get(Dept.class, id);
		
		session.getTransaction().commit();
		return dept;
	}
	
	public void save(Dept dept)
	{
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		
		session.save(dept);
		
		session.getTransaction().commit();
	}
}

DeptService.java

package com.rk.service;

import com.rk.dao.DeptDao;
import com.rk.entity.Dept;

public class DeptService
{
	private DeptDao deptDao = new DeptDao();
	
	public Dept findById(int id)
	{
		return deptDao.findById(id);
	}
	
	public void save(Dept dept)
	{
		deptDao.save(dept);
	}
}

App.java

package com.rk.test;

import org.junit.Test;

import com.rk.entity.Dept;
import com.rk.service.DeptService;

public class App
{
	@Test
	public void test()
	{
		DeptService deptService = new DeptService();
//		Dept dept = deptService.findById(3);
//		System.out.println(dept);
		
		Dept dept = new Dept();
		dept.setDeptName("HelloWorld");
		deptService.save(dept);
	}
}

 



3、测试Spring环境


applicationContext.xml (添加)




	

App.java (修改)

	@Test
	public void test()
	{
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Dept dept = (Dept) ac.getBean("dept");
		System.out.println(dept);
	}

 

4、Spring和Hibernate整合




4.1、Hibernate的SessionFactory交给Spring创建


其中,hibernate.cfg.xmlDept.hbm.xmlDept.java三个文件没有发生变化。

DeptDao.java (修改)

package com.rk.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.rk.entity.Dept;

public class DeptDao
{
	private SessionFactory sf;
	public void setSf(SessionFactory sf)
	{
		this.sf = sf;
	}
	
	public Dept findById(int id)
	{
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		
		Dept dept = (Dept) session.get(Dept.class, id);
		
		session.getTransaction().commit();
		return dept;
	}
	
	public void save(Dept dept)
	{
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		
		session.save(dept);
		
		session.getTransaction().commit();
	}
}

DeptService.java (修改)

package com.rk.service;

import com.rk.dao.DeptDao;
import com.rk.entity.Dept;

public class DeptService
{
	private DeptDao deptDao;
	public void setDeptDao(DeptDao deptDao)
	{
		this.deptDao = deptDao;
	}
	
	public Dept findById(int id)
	{
		return deptDao.findById(id);
	}
	
	public void save(Dept dept)
	{
		deptDao.save(dept);
	}
}

applicationContext.xml (修改)



	
	
	
	
	
	
		
	
	
	
	
		
	
	
	
	
		
	

App.java (修改)

package com.rk.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rk.entity.Dept;
import com.rk.service.DeptService;

public class App
{
	@Test
	public void test()
	{
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		DeptService deptService = (DeptService) ac.getBean("deptService");
		
		Dept dept = deptService.findById(3);
		System.out.println(dept);
		
		Dept newDept = (Dept) ac.getBean("dept");
		newDept.setDeptName("HelloWorld");
		deptService.save(newDept);
	}
	
}



 

4.2、Hibernate的事务交给Spring的事务控制


其中,Dept.hbm.xmlDept.java没有发生改变。

DeptDao.java (修改)

package com.rk.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.rk.entity.Dept;

public class DeptDao
{
	private SessionFactory sf;
	public void setSf(SessionFactory sf)
	{
		this.sf = sf;
	}
	
	public Dept findById(int id)
	{
		Session session = sf.getCurrentSession();
		Dept dept = (Dept) session.get(Dept.class, id);
		return dept;
	}
	
	public void save(Dept dept)
	{
		Session session = sf.getCurrentSession();
		session.save(dept);
	}
}

DeptService.java (修改)

在save方法中,加入了int i = 1/0;所以会出错,而save方法处于事务当中,因此两条数据不会保存成功。如果去掉错误,两条数据就可以保存成功了。

package com.rk.service;

import com.rk.dao.DeptDao;
import com.rk.entity.Dept;

public class DeptService
{
	private DeptDao deptDao;
	public void setDeptDao(DeptDao deptDao)
	{
		this.deptDao = deptDao;
	}
	
	public Dept findById(int id)
	{
		return deptDao.findById(id);
	}
	
	public void save(Dept dept)
	{
		deptDao.save(dept);
		
		int i = 1/0;
		
		Dept newDept = new Dept();
		newDept.setDeptName("HAHA_LLO");
		deptDao.save(newDept);
	}
}

hibernate.cfg.xml (修改)

 修改的只有一行:删除或注释下面的配置


thread

否则,会报错误:org.hibernate.HibernateException: 方法 is not valid without active transaction





    
    
        
        com.mysql.jdbc.Driver
        jdbc:mysql:///test	
        root
        root
		
        org.hibernate.dialect.MySQL5Dialect
        
        
		
		true
		
		false
		
		update
		
		
		

		
				

    


applicationContext.xml (修改)



	
	
	
	
	
	
		
	
	
	
	
		
	
	
	
	
		
	
	
	
	
		
	
	
	
	
		
			
			
		
	
	
	
	
		
		
	
	


App.java (没有变化,进行测试)

package com.rk.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rk.entity.Dept;
import com.rk.service.DeptService;

public class App
{
	@Test
	public void test()
	{
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		DeptService deptService = (DeptService) ac.getBean("deptService");
		
		Dept dept = deptService.findById(3);
		System.out.println(dept);
		
		Dept newDept = (Dept) ac.getBean("dept");
		newDept.setDeptName("HelloWorld");
		deptService.save(newDept);
	}
	
}


4.3、Hibernate的数据源交给Spring创建

这里只涉及到修改hibernate.cfg.xml和applicationContext.xml

hibernate.cfg.xml 中删除以下内容:

        com.mysql.jdbc.Driver
        jdbc:mysql:///test	
        root
        root

applicationContext.xml中添加dataSource和修改sessionFactory

	
	
		
		
		
		
		
		
		
		
	
	
	
	
		
		
	


完整的hibernate.cfg.xml





    
    
        
		
        org.hibernate.dialect.MySQL5Dialect
        
        
		
		true
		
		false
		
		update
		
		
		

		
				

    

完整的applicationContext.xml



	
	
	
	
	
	
		
	
	
	
	
		
	
	
	
	
		
		
		
		
		
		
		
		
	
	
	
	
		
		
	
	
	
	
		
	
	
	
	
		
			
			
		
	
	
	
	
		
		
	
	



4.4、Hibernate的配置全部写到Spring的配置当中

换句话说,就是删除hibernate.cfg.xml,只保留applicationContext.xml文件 

applicationContext.xml



	
	
	
	
	
	
		
	
	
	
	
		
	
	
	
	
		
		
		
		
		
		
		
		
	
	
	
	
	
		
		

		
		
			
				org.hibernate.dialect.MySQL5Dialect
				true
				false
				update
			
		
		
		
		
		
			
				classpath:com/rk/entity/
			
		
	
	
	
	
		
	
	
	
	
		
			
			
		
	
	
	
	
		
		
	
	


4.5、总结:最终的文件状态


applicationContext.xml



	
	
	
	
	
	
		
	
	
	
	
		
	
	
	
	
		
		
		
		
		
		
		
		
	
	
	
	
	
	
		
		

		
		
			
				org.hibernate.dialect.MySQL5Dialect
				true
				false
				update
			
		
		
		
		
		
			
				classpath:com/rk/entity/
			
		
		
	
	
	
	
	
	
		
	
	
	
	
		
			
			
		
	
	
	
	
		
		
	
	

Dept.hbm.xml




	
		
			
		
		
		
	

Dept.java

package com.rk.entity;

public class Dept
{
	private int deptId;
	private String deptName;
	private int deptVersion;
	public int getDeptId()
	{
		return deptId;
	}
	public void setDeptId(int deptId)
	{
		this.deptId = deptId;
	}
	public String getDeptName()
	{
		return deptName;
	}
	public void setDeptName(String deptName)
	{
		this.deptName = deptName;
	}
	public int getDeptVersion()
	{
		return deptVersion;
	}
	public void setDeptVersion(int deptVersion)
	{
		this.deptVersion = deptVersion;
	}
	@Override
	public String toString()
	{
		return "Dept [deptId=" + deptId + ", deptName=" + deptName + ", deptVersion=" + deptVersion + "]";
	}
	
}

DeptDao.java

package com.rk.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.rk.entity.Dept;

public class DeptDao
{
	private SessionFactory sf;
	public void setSf(SessionFactory sf)
	{
		this.sf = sf;
	}
	
	public Dept findById(int id)
	{
		Session session = sf.getCurrentSession();
		Dept dept = (Dept) session.get(Dept.class, id);
		return dept;
	}
	
	public void save(Dept dept)
	{
		Session session = sf.getCurrentSession();
		session.save(dept);
	}
}

DeptService.java

package com.rk.service;

import com.rk.dao.DeptDao;
import com.rk.entity.Dept;

public class DeptService
{
	private DeptDao deptDao;
	public void setDeptDao(DeptDao deptDao)
	{
		this.deptDao = deptDao;
	}
	
	public Dept findById(int id)
	{
		return deptDao.findById(id);
	}
	
	public void save(Dept dept)
	{
		deptDao.save(dept);
		
//		int i = 1/0;
		
		Dept newDept = new Dept();
		newDept.setDeptName("HAHA_LLO");
		deptDao.save(newDept);
	}
}

App.java

package com.rk.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rk.entity.Dept;
import com.rk.service.DeptService;

public class App
{
	@Test
	public void test()
	{
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		DeptService deptService = (DeptService) ac.getBean("deptService");
		
		Dept dept = deptService.findById(3);
		System.out.println(dept);
		
		Dept newDept = (Dept) ac.getBean("dept");
		newDept.setDeptName("HelloWorld");
		deptService.save(newDept);
	}
	
}

 

 

 

相关内容

热门资讯

山西矿难,一场早有预兆的祸事 文丨李一鸣 姜鸥桐 编辑丨卢伊“矿难”“瓦斯爆炸”,这是很多年没有出现在我们视野中的语汇,但它就是这...
朱杨柱、张志远、黎家盈,领命出... ‍‍央视新闻消息,5月24日,神舟二十三号载人飞行任务航天员乘组出征仪式在酒泉卫星发射中心问天阁圆梦...
女子在车厢内大声唱歌、举止怪异... 极目新闻记者 李淑仪5月23日,有网友发视频称,自己乘坐由呼和浩特铁路局承运的K886/K887次列...
天玑7020等于骁龙多少(天玑... 天玑720相当于高通骁龙765G。天玑720采用了台积电7nm制程工艺,EUV是采用波长15nm的极...
坐便器水箱一直流水怎么办 坐便器水箱一直流水可能是由以下原因导致的:1. 水箱配件老化或损坏:水箱内的配件,如浮球、排水阀等,...
安装墙面收纳柜的好处及注意事项 安装墙面收纳柜可以提供合理的收纳空间,色彩和设计上要简洁明快,注意大小和线路布局,选择适合的颜色和灯...
安装中央空调的注意事项 中央空调是现代家庭的常见设备之一,安装中央空调需要注意以下事项: 1.选择合适的品牌和型号 选择...
led灯不亮了但有弱光怎么办 LED灯在使用过程中,容易出现各种故障,比如灯不亮了,但是有弱光,这怎么处理好?小编带大家一起了解下...
轰动性突破!美国终被伊朗逼怂 伊朗局势看来出现了真正的进展,在多方传出美伊谈判立场相互接近后,特朗普北京时间周日凌晨发帖表示,美国...
男子踩中蛇窝,至少被3条毒蛇咬... 5月22日,云南保山市人民医院血液科蛇伤救治中心病房外,26岁的阿杰(化名)在哥嫂的搀扶下,缓缓走出...