详解Spring+Hiernate整合

网友投稿 193 2023-05-26


详解Spring+Hiernate整合

一、整合目标

1.由IoC容器管理Hibernate的SessionFactory

2.让Hibernate使用Spring的声明式事务

二、整合步骤

先加入Hibernat,再加入Spring,再进行整合。

第一步:

配置Hibernate

1.加入Hibernate相关的包

Hibernate的必需包

c3p0包和数据库驱动包

AspectJWeaver.jar

数据库驱动包

2.添加Hibernate的配置文件hibernate.cfg.xml

a.Hibernate的数据源配置可以拿到Spring中去配置,所以无需在hibernate.cfg.xml中配置。

b.关联的.hbm.xml文件也可以在Spring配置文件中配置SessionFactory时进行配置。

c.在hibernate.cfg.xml中可以配置sql方言,sql显示,自动生成表,二级缓存等内容

3.编写实体类和对应的hbm.xml映射文件。

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">

org.hibernate.dialect.MySQLDialect

true

第二步:加入Spring

1.加入Spring包。

Spring的jar包

aspectjweaver.jar

2.加入Spring的配置文件。

配置数据源

1)建立db.properties的资源文件,配置数据源的连接信息。

driverClass=com.mysql.jdbc.Driver

jdbcUrl=jdbc:mysql://localhost:3306/mydb

user=root

password=

minPoolSize=5

maxPoolSize=20

initialPoolSize=5

在Spring配置文件中导入db.properties

配置实体化c3p0的数据源ComboPooledDataSource

(测试数据源配置成功)

&ltvgnPz;!-- 实例化c3p0数据源 -->

2)配置Hibernate的SessionFactory——通过Spring提供的LocalSessionFactoryBean来配置

3)配置Spring的声明式事务

配置事务管理器 -- HibernateTransactionManager

配置事务属性 -- 导入tx命名空间

配置事务切点,并把切点和事务属性关联起来。--导入aop命名空间

第三步:编写代码

1.在Spring配置文件中配置自动扫描的包

package com.itnba.maya.entities;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

@Repository//自动扫描

public class InfoDao {

@Autowired//自动扫描

private SessionFactory factory;

public Session getSession(){

return factory.getCurrentSession();

}

public void select() {

Info data = getSession().get(Info.class, "p005");

System.out.println(data.getName());

}

}

用 main函数执行

package com.itnba.maya.entities;

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) throws SQLException {

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

InfoDao data=(InfoDao) context.getBean(InfoDao.class);

data.select();

}

}

结果:

完整的Spring配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.3.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.3.xsd"

>

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.3.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.3.xsd"

>

另外:

Spring整合Hibernate,也可以不使用 Hibernate的配置文件,把Hibernate配置文件中的内容放在Spring的配置文件中。(一般不这么用)

true

org.hibernate.dialect.MySQLDialect

....


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

上一篇:Ajax验证用户名或昵称是否已被注册
下一篇:Spring中BeanFactory解析bean详解
相关文章

 发表评论

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