您的位置:首页 > 编程语言 > Java开发

spring学习笔记 -- day13 基于XML的引入式整合

2017-08-21 13:48 267 查看

一、基于XML的引入式整合

1、明确什么是引入式

引入式整合就是把hibernate.cfg.xml中的配置都挪到spring的配置文件中,项目中没有hibernate.cfg.xml配置文件

2、修改applicationContext-jdbc.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置一个SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 1.连接数据库的信息 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 2.hibernate的基本配置 -->
<property name="hibernateProperties">
<props>
<!-- 数据库的方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 是否显示SQL语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 是否格式化SQL语句 -->
<prop key="hibernate.format_sql">false</prop>
<!-- 选择生成DDL语句的策略 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 把session绑定到当前线程上 -->
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate5.SpringSessionContext
</prop>
</props>
</property>
<!-- 3.映射文件的位置
mappingResources:
它是指定映射文件的位置,是一个数组类型的数据。它可以提供映射文件的位置。但是有几个映射文件,就需要写几个。
mappingDirectoryLocations:
它是指定映射文件所在的包,是一个数组类型的数据。它适合多个映射文件在不同的包下。指定的是包的名称。
mappingLocations:
它是指定映射文件的位置,是一个数据类型的数据。它可以使用通配符。指定的是映射文件的位置。(实际开发中选择此种方式较多)
-->
<property name="mappingLocations">
<array>
<value>classpath:cn/itcast/domain/*.hbm.xml</value>
</array>
</property>
</bean>

<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/crm_ee247"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>

3、删除hibernate主配置文件

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: