hibernate和spring的整合

网友投稿 598 2022-09-02

hibernate和spring的整合

hibernate和spring的整合

1.导入spring和hibernate必备的jar包文件

2.新建一个源文件config

3.项目分包和建类

4.hibernate.cfg.xml的总配置文件

org.hibernate.dialect.MySQLDialect update true true

5.spring中的bean.xml总配置文件;

6.StudentDao类

package com.eduask.dao; import java.util.List; import com.eduask.pojo.Student; //定义一个StudentDao的接口; public interface StudentDao { //根据id查询;Student getStudentById(int id) throws Exception ; //查询所有;List getStudents() throws Exception; //添加;void insertStudent(Student student) throws Exception; //删除;void deleteStudent(int id) throws Exception; //修改; void updateStudent(Student student) throws Exception; }

7.StudentDaoImpl类;

package com.eduask.dao.impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.stereotype.Repository; import com.eduask.dao.StudentDao; import com.eduask.pojo.Student; @Repository public class StudentDaoImpl implements StudentDao { private Session session=null; @Resource(name="sessionFactory") public void setSessionFactory(SessionFactory sessionFactory){ this.session=sessionFactory.openSession(); }public Student getStudentById(int id) throws Exception {// TODO Auto-generated method stubreturn (Student) session.get(Student.class, id);}public List getStudents() throws Exception { //Student为大写;String hql="from Student"; Query query=session.createQuery(hql); return query.list();}public void insertStudent(Student student) throws Exception { Transaction tr=session.beginTransaction(); session.save(student); tr.commit(); session.close();}public void deleteStudent(int id) throws Exception {// TODO Auto-generated method stubStudent student=getStudentById(id);Transaction tr=session.beginTransaction(); session.delete(student);tr.commit(); session.close();}public void updateStudent(Student student) throws Exception {// TODO Auto-generated method stubTransaction tr=session.beginTransaction(); session.update(student);tr.commit(); session.close();} }

8.StudentTest类

package com.eduask.test; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Before; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.eduask.dao.StudentDao; import com.eduask.dao.impl.StudentDaoImpl; import com.eduask.pojo.Student; public class StudentTest { //引入spring中的bean.xml配置文件; private ClassPathXmlApplicationContext cx=null; @Before public void setUp(){ cx=new ClassPathXmlApplicationContext("spring/bean.xml"); } //增加; @Test public void testInsertStudent() throws Exception{ StudentDao studentDao=cx.getBean(StudentDaoImpl.class); Student student=new Student(); student.setName("link"); student.setPwd("123456"); studentDao.insertStudent(student); } //删除 //删除需要获得id,在StudentDaoImpl类中删除方法中调用获得id的方法; @Test public void testDeleteStudent() throws Exception{ StudentDao studentDao=cx.getBean(StudentDaoImpl.class); studentDao.deleteStudent(1); } //修改; //修改需要获得id; @Test public void testUpdateStudent() throws Exception{ StudentDao studentDao=cx.getBean(StudentDaoImpl.class); Student student=new Student(); student=studentDao.getStudentById(2); student.setPwd("12345678"); studentDao.updateStudent(student); } //查询通过id; @Test public void testSelectStudent() throws Exception{ StudentDao studentDao=cx.getBean(StudentDaoImpl.class); System.out.println(studentDao.getStudentById(2)); } //查询所有; @Test public void testSelectAllStudent() throws Exception{ StudentDao studentDao=cx.getBean(StudentDaoImpl.class); List students=studentDao.getStudents(); for (Student student : students) { System.out.println(student); } } }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:RPackage006---xlsx
下一篇:php并发加锁问题分析与设计,可参考学习下(php 锁)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~