Skip to content

Latest commit

 

History

History
121 lines (88 loc) · 3.85 KB

07.md

File metadata and controls

121 lines (88 loc) · 3.85 KB

[TOC]

BOS物流项目07———bos-web模块配置之applicationContext.xml

一、db.properties文件

这个文件配置数据库连接的

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///bosdb
jdbc.user=xiaoming
jdbc.password=123456

二、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"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 加载属性文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 配置LocalSessionFactoryBean,spring提供的用于整合hibernate的工厂bean -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 注入hibernate相关的属性配置 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <!-- 注入hibernate的映射文件 -->
        <property name="mappingLocations">
            <list>
                <value>classpath:com/qwm/bos/domain/*.xml</value>
            </list>
        </property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- 组件扫描 -->
    <context:component-scan base-package="com.qwm.bos"/>

    <!-- 支持spring注解 -->
    <context:annotation-config/>

    <tx:annotation-driven/>
</beans>


三、配置说明

applicationContext.xml 的配置,和我们以前说的配置是一样,主要就是映射文件的配置不同而已,以前我们使用的配置是

<!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
<property name="mappingDirectoryLocations" value="classpath:com/qwm/ssh_crm/domain"></property>

现在我们使用的配置

 <!-- 注入hibernate的映射文件 -->
  <property name="mappingLocations">
     <list>
        <value>classpath:com/qwm/bos/domain/*.xml</value>
      </list>
   </property>

四、参考文章

SSH与SSM学习之SSH整合06——Hibernate与Spring整合

SSH与SSM学习之SSH整合07——Spring整合c3p0连接池


五、源码下载

https://github.com/wimingxxx/bos-parent