本文地址: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. ubuntu安装IBM DB2 Express-C

    去 官网 下载DB2对应Linux下的安装包和语言包.

  2. echarts版本折线图

    1.效果如下:         绘制折线图,应该算是说echarts中使用最简单也算使用频率最高的一种功能了吧.根据官网列子能找出规律,只是有些属性对于初接触者来说,会有点陌生,不过仔细阅读一下还是不 ...

  3. laypage分页控件使用方法

    laypage是一款非常简单易用的分页控件,由于最近项目中使用到了,简单记录一下使用方法 1.引入laypage所需的js和css文件 <link href="js/laypage/1 ...

  4. contenteditable 插入及粘贴纯文本内容

    本文主要介绍 div 标签设置  contenteditable = ' true ' 时,在光标位置插入输入的内容,或在光标位置粘贴纯文本内容.文中涉及知识,可参考以下: http://www.zh ...

  5. [LeetCode] Map Sum Pairs 映射配对之和

    Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...

  6. 【Swift】ios开发中巧用 description 打印对象时,打印对象的属性

    ios开发中我们打印对象的时候,会直接输出对象地址,这样不方便我们开发.我们可以 巧用 description 打印对象时,输出对象的属性 在oc中直接重写即可.swift中需要遵守Printable ...

  7. Oracle数据库基础练习题

    --1.查询和SMITH相同部门的员工姓名和雇用日期select ename,hiredate from emp where deptno=(select deptno from emp where ...

  8. cd

    从当前目录切换到目标目录 cd [目标目录]   切换到用户主目录 cd cd~   切换到根目录 cd /   切换到上级目录 cd .. cd ../ cd ..//   切换到上级目录的父目录 ...

  9. [BZOJ 3332]旧试题

    Description 圣诞节将至.一年一度的难题又摆在wyx面前——如何给妹纸送礼物. wyx的后宫有n人,这n人之间有着复杂的关系网,相互认识的人有m对.wyx想要量化后宫之间的亲密度,于是准备给 ...

  10. [测试题]wows

    Description 山山最近在玩一款游戏叫战舰世界(steam 游戏太少了),他被大舰巨炮的魅力折服,于是山山开了一局游戏,这次发现目标是一艘战列舰新墨西哥级,舰桥很高,原本应该打在目标身后的圆形 ...