美文网首页
2019-01-14 Mybatis 多数据源配置文件

2019-01-14 Mybatis 多数据源配置文件

作者: 始于梦也于梦 | 来源:发表于2019-01-14 17:45 被阅读0次

    <?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

          xmlns:context="http://www.springframework.org/schema/context"

          xmlns:task="http://www.springframework.org/schema/task"

          xmlns:aop="http://www.springframework.org/schema/aop"

          xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"

          xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

    http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

    http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"

          default-lazy-init="true">

    <description>mybatis配置</description>

      <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

    <property name="dataSource" ref="dataSource"/>

    <!-- 指定 domain 路径,为Java类型命名一个短的别名,例如:com.mochasoft 的别名是Category -->

        <property name="typeAliasesPackage" value="cmcc.unify.backlog.collection"/>

    <!--  指定单个类-->

        <!-- 指定mapper.xml路径 -->

        <property name="mapperLocations" value="classpath:cmcc/unify/backlog/collection/**/mapper/*.xml"></property>

    <!-- 1.配置mybatis属性第一种方式:指定 mybatis的配置文件 -->

        <!-- 2.配置mybatis属性第二种方式:直接配置mybatis属性。-->

        <property name="configuration">

    <bean class="org.apache.ibatis.session.Configuration">

    <property name="cacheEnabled" value="${mybatis.cacheEnabled}"/>

    <property name="useGeneratedKeys" value="${mybatis.useGeneratedKeys}"/>

    <property name="defaultExecutorType" value="${mybatis.defaultExecutorType}"/>

    <property name="mapUnderscoreToCamelCase" value="${mybatis.mapUnderscoreToCamelCase}"/>

    <property name="lazyLoadingEnabled" value="${mybatis.lazyLoadingEnabled}"/>

    <property name="multipleResultSetsEnabled" value="${mybatis.multipleResultSetsEnabled}"/>

    <property name="useColumnLabel" value="${mybatis.useColumnLabel}"/>

    <property name="defaultStatementTimeout" value="${mybatis.defaultStatementTimeout}"/>

    <property name="jdbcTypeForNull" value="NULL"></property>

    </bean>

    </property>

    </bean>

    <!-- scan for mappers and let them be autowired,类和xml在同一目录中,名称相同,则自动绑定  -->

      <!-- mybatis 事物托管给spring-->

      <bean name="transcationManagerMybatis" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    <property name="dataSource" ref="dataSource"/>

    </bean>

    <tx:annotation-driven transaction-manager="transcationManagerMybatis"/>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <property name="basePackage" value="cmcc.unify.backlog.collection.**.mapper" />

    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

    </bean>

    <bean name="dataSource_2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

    <property name="url" value="${sgs.db.url}" />

    <property name="username" value="${sgs.db.username}" />

    <property name="password" value="${sgs.db.password}" />

    <!-- 初始化连接大小 -->

        <property name="initialSize" value="0" />

    <!-- 连接池最大使用连接数量 -->

        <property name="maxActive" value="20" />

    <!-- 连接池最大空闲 -->

        <property name="maxIdle" value="20" />

    <!-- 连接池最小空闲 -->

        <property name="minIdle" value="0" />

    <!-- 获取连接最大等待时间 -->

        <property name="maxWait" value="60000" />

    </bean>

      <bean name="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">

    <property name="dataSource" ref="dataSource_2"/>

    <!-- 指定 domain 路径,为Java类型命名一个短的别名,例如:com.mochasoft 的别名是Category -->

        <property name="typeAliasesPackage" value="cmcc.unify.backlog.transform"/>

    <!--  指定单个类-->

        <!-- 指定mapper.xml路径 -->

        <property name="mapperLocations" value="classpath:cmcc/unify/backlog/transform/**/mapper/*.xml"></property>

    <!-- 1.配置mybatis属性第一种方式:指定 mybatis的配置文件 -->

        <!-- 2.配置mybatis属性第二种方式:直接配置mybatis属性。-->

        <property name="configuration">

    <bean class="org.apache.ibatis.session.Configuration">

    <property name="cacheEnabled" value="${mybatis.cacheEnabled}"/>

    <property name="useGeneratedKeys" value="${mybatis.useGeneratedKeys}"/>

    <property name="defaultExecutorType" value="${mybatis.defaultExecutorType}"/>

    <property name="mapUnderscoreToCamelCase" value="${mybatis.mapUnderscoreToCamelCase}"/>

    <property name="lazyLoadingEnabled" value="${mybatis.lazyLoadingEnabled}"/>

    <property name="multipleResultSetsEnabled" value="${mybatis.multipleResultSetsEnabled}"/>

    <property name="useColumnLabel" value="${mybatis.useColumnLabel}"/>

    <property name="defaultStatementTimeout" value="${mybatis.defaultStatementTimeout}"/>

    <property name="jdbcTypeForNull" value="NULL"></property>

    </bean>

    </property>

    </bean>

    <!-- scan for mappers and let them be autowired,类和xml在同一目录中,名称相同,则自动绑定  -->

      <!-- mybatis 事物托管给spring-->

      <bean name="transcationManagerMybatis2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    <property name="dataSource" ref="dataSource_2"/>

    </bean>

    <tx:annotation-driven transaction-manager="transcationManagerMybatis2"/>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <property name="basePackage" value="cmcc.unify.backlog.transform.**.mapper" />

    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>

    </bean>

    </beans>

    注意,两个数据源引用包需要分开,不可混用

    相关文章

      网友评论

          本文标题:2019-01-14 Mybatis 多数据源配置文件

          本文链接:https://www.haomeiwen.com/subject/tqujdqtx.html