一、集成的注意事项
SessionFactory的创建交由IOC容器来管理,通过Configuration对象创建。
Hibernate事务交给spring的声明式事务管理。
现可以通过spring配置,依赖IOC容器,DI注入来实现。
两种方式:
方式1 Spring直接加载hibernate .cfg.xml文件的方式整合
方式2 连接池交给spring管理 【一部分配置写到hibernate中(hibernate常用配置),一部分在spring中完成(session Factory注入,dataSource连接池)】
二、具体步骤
1、导入所需要的hibernate、Spring的jar包
核心:
spring-orm-3.2.18.RELEASE.jar 【spring对hibernate的支持】
spring-tx-3.2.18.RELEASE.jar 【事务相关】
数据库连接所需要的包:
2、实体类Student
package entity; public class Student { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "user [id=" + id + ", name=" + name + "]"; } } 复制代码
student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity"> <class name="entity.Student" table="user"> <id name="id" column="id"> <generator class="native"> </generator> </id> <property name="name" column="name"></property> </class> </hibernate-mapping> 复制代码
3、配置文件
方式一: Spring直接加载hibernate.cfg.xml文件的方式整合
Application.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="hibernate.cfg.xml"></property> </bean> </beans> 复制代码
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="hibernate.cfg.xml"></property> </bean> </beans> jdbc:mysql://localhost:3306/mybase?serverTimezone=UTC&useSSL=false com.mysql.cj.jdbc.Driver root 123456<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="hibernate.cfg.xml"></property> </bean> </beans>org.hibernate.dialect.MySQLDialect<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="hibernate.cfg.xml"></property> </bean> </beans>true<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="hibernate.cfg.xml"></property> </bean> </beans>true update<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="hibernate.cfg.xml"></property> </bean> </beans> 复制代码
方式2 : 连接池交给spring管理 【一部分配置写到hibernate中(hibernate常用配置),一部分在spring中完成(sessionFactory注入,dataSource连接池)】
Application.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity"> <class name="entity.Student" table="user"> <id name="id" column="id"> <generator class="native"> </generator> </id> <property name="name" column="name"></property> </class> </hibernate-mapping><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity"> <class name="entity.Student" table="user"> <id name="id" column="id"> <generator class="native"> </generator> </id> <property name="name" column="name"></property> </class> </hibernate-mapping>entity/student.hbm.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="entity"> <class name="entity.Student" table="user"> <id name="id" column="id"> <generator class="native"> </generator> </id> <property name="name" column="name"></property> </class> </hibernate-mapping> org.hibernate.dialect.MySQLDialect<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="hibernate.cfg.xml"></property> </bean> </beans> 复制代码
4.StudentDao类
public interface schoolDao { /*按学号查询学生*/ public Student findStudent(int id); /*按学号删除学生*/ public void deleteStudent(int id); } 复制代码
5、schoolDaoImpl
public class schoolDaoImpl implements schoolDao { private static Session session = null; private Student student = new Student(); /*获取对应的的session*/ public Session getSession() { ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml"); SessionFactory sf = (SessionFactory) ac.getBean("sessionFactory"); if (session == null) { session = sf.openSession(); } return session; } @Override public Student findStudent(int id) { Transaction ts = getSession().beginTransaction(); student = (Student) getSession().get(Student.class, id); /*System.out.println(u); System.out.println("success!!!");*/ ts.commit(); getSession().close(); return student; } @Override public void deleteStudent(int id) { Transaction ts = getSession().beginTransaction(); getSession().delete((Student) getSession().get(Student.class, id)); ts.commit(); getSession().close(); } } 复制代码
6、编写test测试类
public class test { public static void main(String[] args) { schoolDao schooldao=new schoolDaoImpl(); System.out.println(schooldao.findStudent(1)); } } 复制代码
三、出错解决
1、Error creating bean with name 'sessionFactory' defined in class path resource
出现这个问题是sessionFactory创建失败,首先排查可能是使用的hibernate文件中实体映射错误,注意hbm.xml和xml中的映射是否一致(注意区分大小写),使用注解@Column(name="xx")中对应的数据表的字段,同样注意大小写。
看到所报错误的最后面
如果是maven项目,有可能配置文件没有编译到classes里面、检查有没有其他遗漏的配置文件。
2、.jar版本问题
如果遇到版本问题,更换版本重新尝试。
感谢阅读完!!!如果文章对你有用,就点点赞吧!!
来源:https://blog.caogenba.net/m0_46013789/article/details/122471353
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!