当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring+Hiernate整合

详解Spring+Hiernate整合

2019年07月22日  | 移动技术网IT编程  | 我要评论
一、整合目标 1.由ioc容器管理hibernate的sessionfactory 2.让hibernate使用spring的声明式事务 二、整合步骤 先加入h

一、整合目标

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映射文件。

<?xml version="1.0" encoding="utf-8"?>
<!doctype hibernate-configuration public
    "-//hibernate/hibernate configuration dtd 3.0//en"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!-- 数据库连接用spring配置
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mydb</property>
    <property name="hibernate.connection.username">root</property>
    -->
    <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property>
    <property name="show_sql">true</property>
   <!--  类映射也可用spring来配置
    <mapping resource="com/itnba/maya/entities/family.hbm.xml"/>
    <mapping resource="com/itnba/maya/entities/info.hbm.xml"/>
    <mapping resource="com/itnba/maya/entities/nation.hbm.xml"/>
    <mapping resource="com/itnba/maya/entities/title.hbm.xml"/>
    <mapping resource="com/itnba/maya/entities/work.hbm.xml"/> 
    -->
  </session-factory>
  
</hibernate-configuration>

第二步:加入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 <context:property-placehoder/>

配置实体化c3p0的数据源combopooleddatasource

(测试数据源配置成功)

  <!--加载资源对象 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 实例化c3p0数据源 -->
    <bean class="com.mchange.v2.c3p0.combopooleddatasource" id="datasource">
      <property name="driverclass" value="${driverclass}"></property>
      <property name="jdbcurl" value="${jdbcurl}"></property>
      <property name="user" value="${user}"></property>
      <property name="password" value="${password}"></property>
      <property name="minpoolsize" value="${minpoolsize}"></property>
      <property name="maxpoolsize" value="${maxpoolsize}"></property>
      <property name="initialpoolsize" value="${initialpoolsize}"></property>
    </bean>

2)配置hibernate的sessionfactory——通过spring提供的localsessionfactorybean来配置

<!-- 配置hibernate的sessionfactory -->
<bean class="org.springframework.orm.hibernate5.localsessionfactorybean" id="factory">

<!--配置数据源属性-->
<property name="datasource" ref="datasource"></property>

<!--配置hibernate配置文件的位置-->
<property name="configlocation" value="classpath:hibernate.cfg.xml"></property>

<!--配置hibernate映射文件的位置,可以使用通配符-->
<property name="mappinglocations" value="com/itnba/maya/entities/*.hbm.xml"></property>
</bean>

3)配置spring的声明式事务

配置事务管理器 -- hibernatetransactionmanager

<!-- 配置spring的事务管理器 -->
    <bean class="org.springframework.orm.hibernate5.hibernatetransactionmanager" id="transactionmanager"><!-- 要根据hibernate的版本配置 -->
      <property name="sessionfactory" ref="factory"></property>
    </bean>

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

  <!-- 配置事务属性 -->
    <tx:advice id="txadvice" transaction-manager="transactionmanager">
      <tx:attributes>
        <tx:method name="*"/>
      </tx:attributes>
    </tx:advice>

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

  <!-- 配置事务切入点 -->
    <aop:config>
      <aop:pointcut expression="execution(* com.itnba.maya.entities.*.*(..))" id="pointcut"/>
      <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"/>
    </aop:config>

第三步:编写代码

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

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.itnba.maya.entities"></context:component-scan>
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配置文件

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
    >
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.itnba.maya.entities"></context:component-scan>
    <!--加载资源对象 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 实例化c3p0对象 -->
    <bean class="com.mchange.v2.c3p0.combopooleddatasource" id="datasource">
      <property name="driverclass" value="${driverclass}"></property>
      <property name="jdbcurl" value="${jdbcurl}"></property>
      <property name="user" value="${user}"></property>
      <property name="password" value="${password}"></property>
      <property name="minpoolsize" value="${minpoolsize}"></property>
      <property name="maxpoolsize" value="${maxpoolsize}"></property>
      <property name="initialpoolsize" value="${initialpoolsize}"></property>
    </bean>
    <!-- 配置hibernate的sessionfactory -->
    <bean class="org.springframework.orm.hibernate5.localsessionfactorybean" id="factory">
      <property name="datasource" ref="datasource"></property>
      <property name="configlocation" value="classpath:hibernate.cfg.xml"></property>
      <property name="mappinglocations" value="com/itnba/maya/entities/*.hbm.xml"></property>
    </bean>
    <!-- 配置spring的声明性事务 -->
    <bean class="org.springframework.orm.hibernate5.hibernatetransactionmanager" id="transactionmanager"><!-- 要根据hibernate的版本配置 -->
      <property name="sessionfactory" ref="factory"></property>
    </bean>
    <!-- 配置事务属性 -->
    <tx:advice id="txadvice" transaction-manager="transactionmanager">
      <tx:attributes>
        <tx:method name="*"/>
      </tx:attributes>
    </tx:advice>
    <!-- 配置事务切入点 -->
    <aop:config>
      <aop:pointcut expression="execution(* com.itnba.maya.entities.*.*(..))" id="pointcut"/>
      <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"/>
      
    </aop:config>

</beans>

另外:

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

<property name="hibernateproperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop>
....
</props>
</property>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网