JS 与 trick 代码的运用及解析全攻略
598
2022-09-02
hibernate和spring的整合
1.导入spring和hibernate必备的jar包文件
2.新建一个源文件config
3.项目分包和建类
4.hibernate.cfg.xml的总配置文件
org.hibernate.dialect.MySQLDialect
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
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
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
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~