博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring集成Hibernate步骤:
阅读量:5254 次
发布时间:2019-06-14

本文共 5755 字,大约阅读时间需要 19 分钟。

以Student对象为例:

1,编写domain;

Student类:

public class Student {    private Long id;    private String  name;    private Integer age;//此处省略getter和setter方法}

2,编写映射文件;

3,编写Dao接口;

package com.gxxy.dao;import java.util.List;import com.gxxy.domain.Student;public interface IStudentDAO {    /**     * 保存学生     *      * @param st     */    void save(Student st);    /**     * 删除学生     *      * @param st     */    void delete(Student st);    /**     * 更新学生信息     *      * @param st     */    void update(Student st);    /**     * 获取单个学生信息     *      * @param id     */    Student get(Long id);    /**     * 获取所有学生信息     *      * @return     */    List
list();}

4,编写Dao实现   

①:拷贝jar包(hiberante中必须的包,数据库驱动,Spring的core,test,bean,context);

hibernate中所需要的包:

 

Spring中所需要的包:

 

②:Spring配置文件:

  1)-配置datasource;

    2)-添加并配置db.properties文件;

 3)配置sessionFactory

    1-导入Spring中的jar  jdbc/tx/orm;
    2-使用LocalSessionFactoryBean来创建SessionFactory;
      1.配置dataSource;
      2.配置hibernate的常规相关配置;
      3.配置映射文件(mappingDirectoryLocations)

hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=none hibernate.show_sql=true

 4)编写Dao:

     1,直接在Dao中注入一个SessionFactory;
     2,在Dao中直接使用SessionFactory.getCurrentSession()来得到我们需要的session;

package com.gxxy.dao.impl;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import com.gxxy.dao.IStudentDAO;import com.gxxy.domain.Student;public class StudentDaoImpl implements IStudentDAO {    private SessionFactory sessionFactory;    // setter 注入    public void setSessionFactory(SessionFactory session) {        this.sessionFactory = session;    }    @Override    public void save(Student st) {        Session session = sessionFactory.getCurrentSession();        session.save(st);    }    @Override    public void delete(Student st) {        Session session = sessionFactory.getCurrentSession();        session.delete(st);    }    @Override    public void update(Student st) {        Session session = sessionFactory.getCurrentSession();        session.update(st);    }    @Override    public Student get(Long id) {        Session session = sessionFactory.getCurrentSession();        return (Student) session.get(Student.class, id);    }    @SuppressWarnings("unchecked")    @Override    public List
list() { Session session = sessionFactory.getCurrentSession(); return session.createQuery("SELECT stu FROM Student stu").list(); }}

5)在Spring中配置Dao;

5,编写Service:

  1)编写service接口和实现;

  service 接口

package com.gxxy.service;import java.util.List;import com.gxxy.domain.Student;public interface IStudentServiceDAO {    /**     * 保存学生     *      * @param st     */    void save(Student st);    /**     * 删除学生     *      * @param st     */    void delete(Student st);    /**     * 更新学生信息     *      * @param st     */    void update(Student st);    /**     * 获取单个学生信息     *      * @param id     */    Student get(Long id);    /**     * 获取所有学生信息     *      * @return     */    List
list();}

service接口的实现

package com.gxxy.service.impl;import java.util.List;import com.gxxy.dao.IStudentDAO;import com.gxxy.domain.Student;import com.gxxy.service.IStudentServiceDAO;public class StudnetServiceImpl implements IStudentServiceDAO {    private IStudentDAO dao;    // setter注入dao对象    public void setDao(IStudentDAO dao) {        this.dao = dao;    }    @Override    public void save(Student st) {        dao.save(st);    }    @Override    public void delete(Student st) {        dao.delete(st);    }    @Override    public void update(Student st) {        dao.update(st);    }    @Override    public Student get(Long id) {        return dao.get(id);    }    @Override    public List
list() { return dao.list(); }}

  2)在Spring中配置servicebean;

  3)配置事务:

    1,配置transcationMaanager,使用HibernateTransactionManager,并传入一个sessionFactory;

    2,配置事务属性; 

    3,配置事务切面;

测试类:

package com.gxxy.test;import java.util.List;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.gxxy.domain.Student;import com.gxxy.service.IStudentServiceDAO;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:ApplicationContext.xml")public class OperateStudentTest {    @Autowired    private IStudentServiceDAO service;    @Test    public void testSave() {        service.save(new Student("张三", 18));    }    @Test    public void testdelete() {        Student student = new Student(5L);        service.delete(student);    }    @Test    public void testUpdate() {        Student st = new Student("李四", 16);        st.setId(8L);        service.update(st);    }    @Test    public void testget() {        Student student = service.get(8L);        System.out.println(student);    }    @Test    public void testlist() {        List
list = service.list(); System.out.println(list); }}

 

转载于:https://www.cnblogs.com/zhang-bo/p/6648569.html

你可能感兴趣的文章