一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Spring+Hiernate整合

詳解Spring+Hiernate整合

2020-09-07 08:53濫好人 Java教程

這篇文章主要介紹了詳解Spring+Hiernate整合,spring整合hibernate主要介紹以xml方式實現,有興趣的可以了解一下。

一、整合目標

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顯示,自動生成表,二級緩存等內容

詳解Spring+Hiernate整合

3.編寫實體類和對應的hbm.xml映射文件。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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的資源文件,配置數據源的連接信息。

?
1
2
3
4
5
6
7
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

(測試數據源配置成功)

?
1
2
3
4
5
6
7
8
9
10
11
12
<!--加載資源對象 -->
  <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來配置

?
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 配置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

?
1
2
3
4
<!-- 配置spring的事務管理器 -->
    <bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager"><!-- 要根據hibernate的版本配置 -->
      <property name="sessionFactory" ref="factory"></property>
    </bean>

配置事務屬性 -- 導入tx命名空間

?
1
2
3
4
5
6
<!-- 配置事務屬性 -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="*"/>
    </tx:attributes>
  </tx:advice>

配置事務切點,并把切點和事務屬性關聯起來。--導入aop命名空間

?
1
2
3
4
5
<!-- 配置事務切入點 -->
  <aop:config>
    <aop:pointcut expression="execution(* com.itnba.maya.entities.*.*(..))" id="pointCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
  </aop:config>

第三步:編寫代碼

1.在Spring配置文件中配置自動掃描的包

?
1
2
<!-- 自動掃描 -->
<context:component-scan base-package="com.itnba.maya.entities"></context:component-scan>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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函數執行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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+Hiernate整合

完整的Spring配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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的配置文件中。(一般不這么用)

?
1
2
3
4
5
6
7
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
....
</props>
</property>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/hq233/p/6671247.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 西西人体大胆啪啪私拍色约约 | 天天操天天爽天天射 | 亚洲视频在线免费看 | 日韩毛片免费线上观看 | 99久久精品免费观看区一 | 青青青国产在线观看 | chinese国产打屁股 | 偷拍综合网| 日本免费观看的视频在线 | 午夜精品免费 | 五月性 | 精品国产精品国产 | 亚洲精品午夜在线观看 | 免费午夜影片在线观看影院 | 日本人护士免费xxxx视频 | 免费抽搐一进一出印度 | 国产成人在线视频 | 精品一久久香蕉国产二月 | 国产精品高清视亚洲一区二区 | 国产精品99久久 | 精品人人视屏 | 日本在线观看www | 交换年轻夫妇HD中文字幕 | 日韩亚洲人成网站在线播放 | 青青草原在线免费 | 精品视频二区 | 国产亚洲精品美女2020久久 | 久久亚洲精品AV成人无码 | 高清毛片aaaaaaaaa片 | 日韩精品成人a在线观看 | 美女扒开屁股让我桶免费 | 天堂欧美 | 日韩免费在线看 | 91精品国产高清久久久久 | 国产成人综合一区精品 | 日韩在线免费播放 | 无码精品一区二区三区免费视频 | 青青国产成人久久激情91麻豆 | 美女视频黄a | 成年视频在线播放 | 香蕉久久夜色精品国产小优 |