整合Spring框架和Hibernate框架

来源:互联网 发布:电视的mac 编辑:程序博客网 时间:2024/04/30 07:44

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

  

MchangeCommonsJava下载链接:

  

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;

}

publicvoid setUid(Integer uid) {

this.uid = uid;

}

 

public String getUsername() {

return username;

}

publicvoid setUsername(String username) {

this.username = username;

}

 

public String getAddress() {

return address;

}

publicvoid setAddress(String address) {

this.address = address;

}

 

@Override

public String toString() {

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

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

}

 

}

  

  

  

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

  

User.hbm.xml:

  

<?xmlversion="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>

  

<classname="com.siwuxie095.entity.User"table="t_user">

 

<idname="uid"column="uid">

<generatorclass="native"></generator>

</id>

 

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

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

 

</class>

</hibernate-mapping>

  

  

  

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

  

hibernate.cfg.xml:

  

<?xmlversion="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>

 

 

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

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

<!--注意:只有配置 hibernate.hbm2ddl.auto update,才能自动创建表 -->

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

<propertyname="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

-->

<propertyname="hibernate.current_session_context_class">

org.springframework.orm.hibernate5.SpringSessionContext

</property>

 

 

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

 

 

</session-factory>

</hibernate-configuration>

  

  

  

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

  

applicationContext.xml:

  

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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连接池 -->

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

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

<!--

jdbc:mysql:///test_db jdbc:mysql://localhost:3306/test_db的简写

-->

<propertyname="jdbcUrl"value="jdbc:mysql:///test_db"/>

<propertyname="user"value="root"/>

<propertyname="password"value="8888"/>

</bean>

 

 

<!-- SessionFactory对象的创建交给 Spring进行管理 -->

<beanid="sessionFactory"

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

<!--

因为在 Hibernate核心配置文件中,没有数据库配置,

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

注入 dataSource

 

LocalSessionFactoryBean中有相关属性,所以可以

注入

-->

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

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

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

</bean>

 

  

</beans>

  

  

  

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

  

web.xml:

  

<?xmlversion="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/javaeehttp://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】

原创粉丝点击