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

 
 

 
 

 
 

其中:

 
 

1)hibernate-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.auto 为 update

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

整合Spring框架和Hibernate框架的更多相关文章

  1. Spring Boot从入门到精通(九)整合Spring Data JPA应用框架

    JPA是什么? JPA全称Java Persistence API,是Sun官方提出的Java持久化规范.是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. ...

  2. Spring 框架整合Struts2 框架和 Hibernate 框架

    1. Spring 框架整合 Struts2 框架 // [第一种整合方式(不推荐)](http://www.cnblogs.com/linkworld/p/7718274.html) // 从 Se ...

  3. Java - Struts框架教程 Hibernate框架教程 Spring框架入门教程(新版) sping mvc spring boot spring cloud Mybatis

    https://www.zhihu.com/question/21142149 http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?tid=6 ...

  4. 三大框架 之 Hibernate框架概述(概述、配置、核心API)

    目录 Hibernate框架概述 什么是框架 hibernate简介(JavaEE技术三层架构所用到的技术) hibernate是什么框架 ORM hibernate好处 Hibernate基本使用 ...

  5. ECLIPSE/JAVAWEB (二)三大框架之Hibernate框架 持续更新中...

    (一)发展历史 在Struts框架中使用jdbc连接来读写数据库,我们最常见的就是打开数据库连接.使用复杂的sql语句进行读写.关闭连接,获得的数据又需要转换或封装后往外传,这是一个非常繁琐的过程. ...

  6. JAVA框架之Hibernate框架的学习步骤

    首先介绍一下Java三大框架的关系 以CRM项目即客户关系管理项目示例 hibernate框架的学习路线: 1.学习框架入门,自己搭建框架,完成增删改查的操作 2.学习一级缓存,事物管理和基本查询 3 ...

  7. Spring笔记——配置Hibernate框架事务

    原文:http://www.iteye.com/topic/1123347 Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这 ...

  8. Struts2,Spring,Hibernate框架的优缺点

    Struts2,Spring,Hibernate框架的优缺点 Struts2框架(MVC框架)的优点如下:         1)  实现了MVC模式,层次结构清晰,使程序员只需关注业务逻辑的实现:   ...

  9. Spring_day04--课程安排_回顾SSH框架知识点_SSH框架整合思想

    Spring_day04 上节内容回顾 今天内容介绍 回顾SSH框架知识点 Hibernate框架 Struts2框架 Spring框架 SSH框架整合思想 整合struts2和spring框架 Sp ...

随机推荐

  1. java操作hbase1.3.1的增删改查

    我的eclipse程序在windows7机器上,hbase在linux机器上 1,首先在C:\Windows\System32\drivers\etc下面的HOSTS文件,加上linux 集群 2.直 ...

  2. BCGcontrolBar(五) 对话框大小改变控件自动适应

    改变控件大小 首先在 构造函数中加入 EnableLayout(); 在OnInitDialog()函数中加入 CBCGPStaticLayout* pLayout = (CBCGPStaticLay ...

  3. oracle 收集的一些图

    ================================================ ================================================ da ...

  4. storm项目优化

    实现监控脚本监控topology运行状态

  5. QEMU,KVM及QEMU-KVM介绍

    What's QEMU QEMU是一个主机上的VMM(virtual machine monitor),通过动态二进制转换来模拟CPU,并提供一系列的硬件模型,使guest os认为自己和硬件直接打交 ...

  6. SSH 在ssh-copy-id 之后仍需输入密码的问题

    最近在使用Ansible,基于SSH. 远程服务器IP: 192.168.200.193 以下提及的远程服务器都为该服务器. 远程用户: ansible_user 在本地服务器中,ssh-keygen ...

  7. 任务计划程序-Windows2008定时重启

    参考网站:https://www.cnblogs.com/yeyun/p/6209540.html Windows系统的任务计划程序,可以添加计划任务,设置任务开始时间及执行的间隔,实现应用的自动执行 ...

  8. jsp button onclick

    <input type="button" value="MD5哈希转换" onclick="javascript:document.getEle ...

  9. vue基础——组件(组件嵌套)

    介绍 vue中页面是由组件组成的,即以.vue结尾的文件. .vue文件由三部分组成,分别是template.script.style. 分别写html.js.css代码. 组件之间可以互相嵌套.所以 ...

  10. where 命令

    在当前目录及path环境变量指定的目录中搜索相应文件 例:where msbuild 查找msbuild的位置