首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Spring3调整Hibernate3.6之一:简单CRUD

2012-11-23 
Spring3整合Hibernate3.6之一:简单CRUD本来是想拿Spring整合Hibernate4的,事实证明我道行尚浅 未遂……看到

Spring3整合Hibernate3.6之一:简单CRUD




本来是想拿Spring整合Hibernate4的,事实证明我道行尚浅 未遂……


看到这个异常,且在用Hibernate4的同学就要考虑Hibernate的版本问题了

(解决完这个问题发现4里边把HibernateTemplate取消掉了,所以就没再死扣)。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' XXXXX ': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [applicationContext-common.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;





原来弄过spring+ibatis的整合,其实道理差不多

都是spring帮忙注入,OR框架去数据库中CRUD,仅有的一点区别就是ibatis的SQL是手动的,Hibernate的HQL是自动的,所以Hibernate要实体Student用Annotation声明一下

Spring3调整Hibernate3.6之一:简单CRUD





一:model

用Hibernate的方式,声明实体、表名、主键等。

工程里不再需要 hibernate.cfg.xml 了,在spring配置文件的:hibernateProperties标签里配置就行了

public class StudentServiceTest {@Testpublic void testAdd() throws Exception {//Spring读取spring配置信息ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");//相当于从代理类得到具体实例,由xml决定怎么选择类实例化StudentService service01 = (StudentService)ctx.getBean("StudentService");Student stu = new Student();//手动组装User实例 u//stu.setStudentid(???); 用自动增长序列stu.setName("Spring+Hibernate");stu.setAge(777);service01.add(stu);//把组装好的u传给xml实例化的servicectx.destroy();}@Testpublic void testDelById() throws Exception {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");StudentService service01 = (StudentService)ctx.getBean("StudentService");service01.delById(23);//只需要把id传给实例化的servicectx.destroy();}@Testpublic void testGetAll() throws Exception {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");StudentService service01 = (StudentService)ctx.getBean("StudentService");System.out.println("查询全部:");List<Student> stusAll = (List<Student>)service01.getAll();for(int i=0;i<stusAll.size();i++){System.out.println(stusAll.get(i));}ctx.destroy();}@Testpublic void testUpdateStudent() throws Exception {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");StudentService service01 = (StudentService)ctx.getBean("StudentService");Student stu = new Student();//手动组装User实例 ustu.setStudentid(25);stu.setName("SH");stu.setAge(777);service01.updateStudent(stu);//把组装好的u传给spring实例化的servicectx.destroy();}@Testpublic void testSelectByName() throws Exception {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");StudentService service01 = (StudentService)ctx.getBean("StudentService");System.out.println("查询全部:");String name = "SH";List<Student> stusAll = (List<Student>)service01.selectByName(name);for(int i=0;i<stusAll.size();i++){System.out.println(stusAll.get(i));}ctx.destroy();}}


















热点排行