1. 程式人生 > >整合Spring框架和Hibernate框架

整合Spring框架和Hibernate框架

slf4j erl update rep java 監聽 session hiberna .cn

-------------------siwuxie095

整合 Spring 框架和 Hibernate 框架

1、導入相關 jar 包(共 28 個)

1)導入 Spring 的核心 jar 包和日誌相關的 jar 包(6 個)

技術分享

Commons Logging 下載鏈接:

http://commons.apache.org/proper/commons-logging/download_logging.cgi

LOG4J 下載鏈接:

https://www.apache.org/dist/logging/log4j/

2)導入 Spring 的 AOP 開發的 jar 包(4 個)

技術分享

AOP Alliance 下載鏈接:

http://mvnrepository.com/artifact/aopalliance/aopalliance

AspectJ Weaver 下載鏈接:

http://mvnrepository.com/artifact/org.aspectj/aspectjweaver

3)導入 Spring 的 JDBC 開發的 jar 包(2 個)

技術分享

4)導入

Spring 整合 Web 項目的 jar 包(1 個)

技術分享

5)導入 Hibernate 的基本 jar 包(10 個)

技術分享

其中:

1hibernate-entitymanager 和其它包不在同一個文件夾下

2)在 Struts2 和 Hibernate 中,都有 javassist,會產生沖突,

選擇高版本,刪除低版本即可

3)導入 Hibernate 日誌相關的包(2 個)

技術分享

SLF4J 下載鏈接:https://www.slf4j.org/dist/,其中包含

slf4j-api

slf4j-log4j

其實,Hibernate 日誌相關的包還包含 log4j,因為在 Spring 中已經

有了,所以這裏就不再添加

4)導入 MySQL 的 JDBC 驅動的 jar 包(1 個)

技術分享

mysql-connector-java 下載鏈接:

https://dev.mysql.com/downloads/connector/j/

5)導入 Spring 整合 Hibernate 的 jar 包(1 個)

技術分享

「也可用來整合其它 ORM 框架」

6)導入 C3P0 的 jar 包(1 個)

技術分享

C3P0 下載鏈接:

http://mvnrepository.com/artifact/c3p0/c3p0

註意:如果使用的是 0.9.1 版本,只需要一個 jar 包即可,如果使用

的是 0.9.2 版本,還需要導入一個輔助包 mchange-commons-java

Mchange Commons Java 下載鏈接:

http://mvnrepository.com/artifact/com.mchange/mchange-commons-java

2、測試

1)編寫一個實體類

User.java:

package com.siwuxie095.entity;

public class User {

private Integer uid;

private String username;

private String address;

public Integer getUid() {

return uid;

}

public void setUid(Integer uid) {

this.uid = uid;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

@Override

public String toString() {

return "User [uid=" + uid + ", username=" + username +

", address=" + address + "]";

}

}

2)在 Hibernate 映射配置文件中進行配置

User.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.siwuxie095.entity.User" table="t_user">

<id name="uid" column="uid">

<generator class="native"></generator>

</id>

<property name="username" column="username"></property>

<property name="address" column="address"></property>

</class>

</hibernate-mapping>

3)在 Hibernate 核心配置文件中進行配置

hibernate.cfg.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>

<property name="hibernate.show_sql">true</property>

<property name="hibernate.format_sql">true</property>

<!-- 註意:只有配置 hibernate.hbm2ddl.auto update,才能自動創建表 -->

<property name="hibernate.hbm2ddl.auto">update</property>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!--

原來的配置:

<property name="hibernate.current_session_context_class">thread</property>

SSH 框架整合中會報錯,要麽將這個配置刪了,要麽改成如下配置

參考鏈接:http://blog.csdn.net/maoyuanming0806/article/details/61417995

-->

<property name="hibernate.current_session_context_class">

org.springframework.orm.hibernate5.SpringSessionContext

</property>

<mapping resource="com/siwuxie095/entity/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

4)在 Spring 核心配置文件中進行配置

applicationContext.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"

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.xsd

http://www.springframework.org/schema/context

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

http://www.springframework.org/schema/tx

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

<!-- 配置 C3P0 連接池 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="com.mysql.jdbc.Driver"/>

<!--

jdbc:mysql:///test_db jdbc:mysql://localhost:3306/test_db 的簡寫

-->

<property name="jdbcUrl" value="jdbc:mysql:///test_db"/>

<property name="user" value="root"/>

<property name="password" value="8888"/>

</bean>

<!-- SessionFactory 對象的創建交給 Spring 進行管理 -->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

<!--

因為在 Hibernate 核心配置文件中,沒有數據庫配置,

而是在 Spring 的核心配置文件中進行配置,所以需要

註入 dataSource

LocalSessionFactoryBean 中有相關屬性,所以可以

註入

-->

<property name="dataSource" ref="dataSource"></property>

<!-- 指定 Hibernate 核心配置文件的位置 -->

<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>

</bean>

</beans>

5)在部署描述文件中進行配置

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!-- 配置 Spring 的監聽器 ContextLoaderListener -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- 配置 Spring 核心配置文件的位置(路徑) -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

</web-app>

6)啟動 Tomcat 服務器,發現 MySQL 數據庫中自動創建了表 t_user

技術分享

前提:

Hibernate 核心配置文件中配置了 hibernate.hbm2ddl.autoupdate

【made by siwuxie095】

整合Spring框架和Hibernate框架