本文地址:http://blog.csdn.net/sushengmiyan/article/details/49388209

文章作者:苏生米沿

本文目的:使用spring4.2.2集成hibernate5.0.2并创建sessionFactory实例。

开发环境:eclipse(jee_mars) JDK1.8 MYSQL5.6

spring下载地址: https://repo.spring.io/list/release/org/springframework/spring/4.2.2.RELEASE/

一。XML配置方式

环境搭建:新建一个java工程xmlBasedspringhelloword,引入以下jar包:

1.引入hibernate需要的jar包:antlr-2.7.7.jar、dom4j-1.6.1.jar、geronimo-jta_1.1_spec-1.1.1.jar、hibernate-core-5.0.2.Final.jar、hibernate-jpa-2.1-api-1.0.0.Final.jar、jandex-1.2.2.Final.jar、javassist-3.18.1-GA.jar、jboss-logging-3.3.0.Final.jar

2.新建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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/basichibernatepractice?characterEncoding=UTF8</property>
<property name="connection.username">root</property>
<property name="connection.password">123123</property> <!-- =============== 配置使用c3p0数据库连接池 =================== -->
<property name="connection.pool_size">1</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">120</property>
<property name="c3p0.idle_test_period">3000</property> <!-- =============== 数据库方言设置 =================== -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- =============== 二级缓存设置 =================== -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- =============== 控制台打印sql语句设置设置 =================== -->
<property name="show_sql">true</property> <!-- =============== 数据库表结构更新设置 =================== -->
<property name="hbm2ddl.auto">update</property> <!-- =============== 实体关系列表 =================== --> </session-factory> </hibernate-configuration>

3.导入spring需要的jar包:spring-beans-4.2.2.RELEASE.jar、spring-context-4.2.2.RELEASE.jar、spring-core-4.2.2.RELEASE.jar、spring-expression-4.2.2.RELEASE.jar、spring-orm-4.2.2.RELEASE.jar、spring-tx-4.2.2.RELEASE.jar

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置hibernate5的SessionFactory -->
<bean name = "localSessionFactoryBean" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean> </beans>

5.导入的jar包如下所示。

6.新建测试类Spring4WithHibernate5HelloTest

实现如下:

package com.sushengmiyan.hellospring.hellohibernate5;

import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Spring4WithHibernate5HelloTest { public static void main(String[] args) { @SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
SessionFactory sf = context.getBean("localSessionFactoryBean", SessionFactory.class);
System.out.println(sf);
}
}

运行测试,成功输出sessionFactory.

实现讲解:

1。正常方式配置hibernate

2。创建spring的容器,容器中添加对hibernate的引用。

3.容器中获取,注意容器中添加的是org.springframework.orm.hibernate5.LocalSessionFactoryBean但是从容器中取出的时候,是取得它生成的SessionFactory.

要不然会产生异常Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'localSessionFactoryBean' must be of type [org.springframework.orm.hibernate5.LocalSessionFactoryBean], but was actually of type [org.hibernate.internal.SessionFactoryImpl]

二。java配置方式

1.新建工程javaBasedspringhelloword,将上述hibernate配置移植到本工程。

2.新添加jar包spring-aop-4.2.2.RELEASE.jar

3.填写配置文件java类 Appconfig.java

内容如下:

package com.sushengmiyan.hellospring.helloHibernate5;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; @Configuration
public class AppConfig { @Bean
public LocalSessionFactoryBean localSessionFactoryBean(){
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
return localSessionFactoryBean; }
}

此等同于上述xml配置中的Application.xml文件。

4.测试类书写

package com.sushengmiyan.hellospring.helloHibernate5;

import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Spring4WithHibernate5HelloTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
SessionFactory sf = context.getBean("localSessionFactoryBean", SessionFactory.class);
System.out.println(sf);
} }

运行也可以看到正常输出。

实例演示如何在spring4.2.2中集成hibernate5.0.2并创建sessionFactory的更多相关文章

  1. 如何在 ASP.NET MVC 中集成 AngularJS(3)

    今天来为大家介绍如何在 ASP.NET MVC 中集成 AngularJS 的最后一部分内容. 调试路由表 - HTML 缓存清除 就在我以为示例应用程序完成之后,我意识到,我必须提供两个版本的路由表 ...

  2. 如何在 ASP.NET MVC 中集成 AngularJS(2)

    在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...

  3. 如何在 ASP.NET MVC 中集成 AngularJS(1)

    介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 REST 的 Web API 服务.另外,还有最新的数据库技术.最新 ...

  4. 如何在Spring Boot项目中集成微信支付V3

    Payment Spring Boot 是微信支付V3的Java实现,仅仅依赖Spring内置的一些类库.配置简单方便,可以让开发者快速为Spring Boot应用接入微信支付. 演示例子: paym ...

  5. eclipse中集成svn maven开发手册---创建提分支

    开发时,需要拉取分支进行修改等操作 右键项目,选择team->分支/标记 输入创建分支地址 注意: 1,创建分支路径时,最后一层文件名称为项目的名称 2,点击浏览按钮可能会出现无法选择,且ecl ...

  6. MVC 中集成 AngularJS1

    在 ASP.NET MVC 中集成 AngularJS(1)   介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 R ...

  7. 如何简单的在 ASP.NET Core 中集成 JWT 认证?

    前情提要:ASP.NET Core 使用 JWT 搭建分布式无状态身份验证系统 文章超长预警(1万字以上),不想看全部实现过程的同学可以直接跳转到末尾查看成果或者一键安装相关的 nuget 包 自上一 ...

  8. 在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client

    在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client   阅读目录 验证代码流程 Refreshing a Token Built-In Providers 这个包能够让你 ...

  9. 如何在Linux命令行中创建以及展示演示稿

    导读 你在准备一场演讲的时候,脑海可能会先被图文并茂.形象华丽的演示图稿所占据.诚然,没有人会否认一份生动形象的演讲稿所带来的积极作用.然而,并非所有的演讲都需要TED Talk的质量.更多时候,演讲 ...

随机推荐

  1. [转]Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法

    使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情况下会遇到:UnicodeEncodeError: 'gbk' codec can't encode character ' ...

  2. js分页效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 图片处理之 Base64

    网页上的图片资源如果采用 http 形式的 url 的话都会额外发送一次请求,网页发送的 http 请求次数越多,会造成页面加载速度越慢.而采用Base64格式的编码,将图片转化为字符串后,图片文件会 ...

  4. 完成 bass 库的频谱显示效果图

    效果如图所示,比 bass 官方自带的例子效果要好那么一点点(峰值有滞留)...

  5. scala求交集、并集、差集命令

    交集 scala> Set(1,2,3) & Set(2,4)res1: scala.collection.immutable.Set[Int] = Set(2) 并集 scala> ...

  6. 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- Demo分析

    如何创建工程 下载最新的Unity发布插件包. 打开Unity,新建一个项目 将插件包导入 在菜单中点击ASRuntime/Create ActionScript3 FlashDevelop HotF ...

  7. 探寻 webpack 插件机制

    webpack 可谓是让人欣喜又让人忧,功能强大但需要一定的学习成本.在探寻 webpack 插件机制前,首先需要了解一件有意思的事情,webpack 插件机制是整个 webpack 工具的骨架,而 ...

  8. ES6 new syntax of Rest and Spread Operators

    Rest and Spread Operators.md Why we need rest and spread operators? var showCollections = function(i ...

  9. [LeetCode] Maximum Average Subarray I 子数组的最大平均值

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  10. js 一些基础的理解

    javascript(JS)的组成? DOM 文档对象模型 BOM 浏览器对象模型 ECMAScript javascript(JS)在页面中处理了什么事情? 特效交互 数据交互 逻辑操作 常见特效的 ...