实例演示如何在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);		
	}

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


posted on 2015-10-24 22:22  木鱼哥  阅读(271)  评论(0编辑  收藏  举报

导航