实例演示如何在spring4.2.2中集成hibernate5.0.2并创建sessionFactory
本文地址: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的更多相关文章
- 如何在 ASP.NET MVC 中集成 AngularJS(3)
今天来为大家介绍如何在 ASP.NET MVC 中集成 AngularJS 的最后一部分内容. 调试路由表 - HTML 缓存清除 就在我以为示例应用程序完成之后,我意识到,我必须提供两个版本的路由表 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(2)
在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(1)
介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 REST 的 Web API 服务.另外,还有最新的数据库技术.最新 ...
- 如何在Spring Boot项目中集成微信支付V3
Payment Spring Boot 是微信支付V3的Java实现,仅仅依赖Spring内置的一些类库.配置简单方便,可以让开发者快速为Spring Boot应用接入微信支付. 演示例子: paym ...
- eclipse中集成svn maven开发手册---创建提分支
开发时,需要拉取分支进行修改等操作 右键项目,选择team->分支/标记 输入创建分支地址 注意: 1,创建分支路径时,最后一层文件名称为项目的名称 2,点击浏览按钮可能会出现无法选择,且ecl ...
- MVC 中集成 AngularJS1
在 ASP.NET MVC 中集成 AngularJS(1) 介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 R ...
- 如何简单的在 ASP.NET Core 中集成 JWT 认证?
前情提要:ASP.NET Core 使用 JWT 搭建分布式无状态身份验证系统 文章超长预警(1万字以上),不想看全部实现过程的同学可以直接跳转到末尾查看成果或者一键安装相关的 nuget 包 自上一 ...
- 在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client
在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client 阅读目录 验证代码流程 Refreshing a Token Built-In Providers 这个包能够让你 ...
- 如何在Linux命令行中创建以及展示演示稿
导读 你在准备一场演讲的时候,脑海可能会先被图文并茂.形象华丽的演示图稿所占据.诚然,没有人会否认一份生动形象的演讲稿所带来的积极作用.然而,并非所有的演讲都需要TED Talk的质量.更多时候,演讲 ...
随机推荐
- PHP 抓取网页内容的几个函数
<?php //获取所有内容url保存到文件 function get_index($save_file, $prefix="index_"){ $count = 68; $ ...
- git checkout+文件丢失
坑:不知什么时候, 应该是初学git的时候, 在桌面git init了一下, 这次忘记切目录直接在桌面git checkout了, 导致文件丢失了. 解决: 简单复原: git reflog # 查看 ...
- kafka知识体系
最近一直在整理kafka相关资料,以构建自己的知识体系. 主要分为五大方面: Kafka设计与原理分析 Kafka配置分析 Kafka运维手册 Kafka编程开发 kafka源码分析
- CSS3和H5的新特性
H5的新特性 1. 用于绘画 canvas 元素. 2. 用于媒介回放的 video 和 audio 元素. 3. 本地离线存储 localStorage 长期存储数据,浏览器关闭后数据不 ...
- SSO-单点统一登录系统的设计与实现
本文主要基于web类应用展开讨论,提供的是一种通用机制和方法,所以不论何种技术栈都可进行相应的具体实现. 实现目标 可以在相同或跨域环境下完成各应用的统一登录/注销 方案原理 本质上是采用了web应用 ...
- Python 字符串常见的27个操作
有字符串 mystr = "hello world itcast and itcastcpp",以下是常见的操作: 1. mystr.find(str, start=0, end= ...
- preg_replace引发的phpmyadmin(4.3.0-4.6.2)命令执行漏洞
编辑器坏了 (: 今天看到这个phpmyadmin的代码执行,https://www.waitalone.cn/phpmyadmin-preg_replace-rce.html 记录一下:preg_ ...
- BZOJ3622 已经没有什么好害怕的了
Description Input Output Sample Input 4 2 5 35 15 45 40 20 10 30 Sample Output 4 HINT 输入的2*n个数字保证全不相 ...
- [HAOI2012]道路
题目描述 C国有n座城市,城市之间通过m条[b]单向[/b]道路连接.一条路径被称为最短路,当且仅当不存在从 它的起点到终点的另外一条路径总长度比它小.两条最短路不同,当且仅当它们包含的道路序列不同. ...
- hy 的惩罚
[问题描述] hy 抄题解又被老师抓住了,现在老师把他叫到了办公室. 老师要 hy 和他 玩一个游戏.如果 hy 输了,老师就要把他开除信息组: 游戏分为 k 轮.在游戏开始之前,老师会将 n 个由 ...