ssm 配置多个数据源(mysql、db2)

参考以下三篇文章,或者只参考第三篇即可配置成功
https://blog.csdn.net/xiaobai66073/article/details/73194701
https://blog.csdn.net/u011900448/article/details/79933149
https://blog.csdn.net/qq_33500630/article/details/78203054
https://www.2cto.com/database/201712/703184.html

本文章介绍配置两个mysql,一个db2数据库。
#1、jdbc配置

ds1.jdbc_driver=com.mysql.jdbc.Driver
ds1.jdbc_url=jdbc:mysql://192.168.6.51:3306/ahimmu_inpatient_dev?useSSL=true&useUnicode=true&characterEncoding=UTF-8
#ds1.jdbc_url=jdbc:mysql://192.168.0.135:3306/ahimmu_inpatient_dev?useSSL=true&useUnicode=true&characterEncoding=UTF-8
ds1.jdbc_username=root
ds1.jdbc_password=12345

#生成数据库中表对应的实体类所在包
ds1.model.package=com.system.entity
#生成的mapper接口类所在包
ds1.dao.package=com.system.dao
#生成的mapper xml文件所在包,默认存储在resources目录下
ds1.xml.sqlmapper.package=com.system.mapper.generated

##配置初始化大小
initialSize=1
##配置初始化最大连接数
maxActive=20
##定义最大空闲
maxIdle=20
##配置初始化最小连接数
minIdle=1
##配置获取连接等待超时的时间
maxWait=60000
##配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis=60000 
##配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis=300000

ds2.jdbc_driver=com.mysql.jdbc.Driver
ds2.jdbc_url=jdbc:mysql://192.168.6.51:3306/test?useSSL=true&useUnicode=true&characterEncoding=UTF-8
#ds2.jdbc_url=jdbc:mysql://192.168.0.135:3306/ahimmu_inpatient_dev?useSSL=true&useUnicode=true&characterEncoding=UTF-8
ds2.jdbc_username=root
ds2.jdbc_password=12345

#生成数据库中表对应的实体类所在包
ds2.model.package=com.system.entity.NM
#生成的mapper接口类所在包
ds2.dao.package=com.system.dao.NM
#生成的mapper xml文件所在包,默认存储在resources目录下
ds2.xml.sqlmapper.package=com.system.mapper.generated.NM

ds3.jdbc_driver=com.ibm.db2.jcc.DB2Driver
ds3.jdbc_url=jdbc:db2://192.168.6.51:50000/test1
ds3.jdbc_username=bqcai
ds3.jdbc_password=Ibm123

#生成数据库中表对应的实体类所在包
ds3.model.package=com.system.entity.DB2
#生成的mapper接口类所在包
ds3.dao.package=com.system.dao.DB2
#生成的mapper xml文件所在包,默认存储在resources目录下
ds3.xml.sqlmapper.package=com.system.mapper.generated.DB2

#2、利用generator生成entity、mapper,dao
3个数据源的generatorConfig的配置文件如下:
generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="Mysql" targetRuntime="tk.mybatis.mapper.generator.TkMyBatis3Impl" defaultModelType="flat">

        <property name="javaFileEncoding" value="UTF-8"/>

        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
            <property name="caseSensitive" value="true"/>
        </plugin>

        <!-- 是否去除自动生成的注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"></property>
            <property name="suppressDate" value="true"></property>
            <property name="javaFileEncoding" value="utf-8"/>
        </commentGenerator>

        <!-- 数据库连接的信息 -->
        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}"
                        userId="${userId}"
                        password="${password}">
        </jdbcConnection>

        <!-- false:JDBC DECIMAL、NUMERIC类型解析为Integer,默认方式 -->
        <!-- true: JDBC DECIMAL、NUMERIC类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成model模型的包名和位置 -->
        <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
            <!-- 是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"></property>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"></property>
        </javaModelGenerator>

        <!-- 生成xml mapper映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_java}">
            <property name="enableSubPackages" value="true"></property>
        </sqlMapGenerator>

        <!--生成xml dao 的包名和位置 -->
        <javaClientGenerator targetPackage="${daoPackage}" targetProject="${src_main_java}"  type="XMLMAPPER">
            <property name="enableSubPackages" value="true"></property>
        </javaClientGenerator>

        <!-- sql占位符,表示所有的表 -->
        <table tableName="%" mapperName="{0}Dao"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>

            <!-- 忽略该字段(可省略) -->
            <!--<ignoreColumn column="name" />-->

        </table>
    </context>
</generatorConfiguration>

generatorConfig2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="Mysql" targetRuntime="tk.mybatis.mapper.generator.TkMyBatis3Impl" defaultModelType="flat">

        <property name="javaFileEncoding" value="UTF-8"/>

        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
            <property name="caseSensitive" value="true"/>
        </plugin>

        <!-- 是否去除自动生成的注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"></property>
            <property name="suppressDate" value="true"></property>
            <property name="javaFileEncoding" value="utf-8"/>
        </commentGenerator>

        <!-- 数据库连接的信息 -->
        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}"
                        userId="${userId}"
                        password="${password}">
        </jdbcConnection>

        <!-- false:JDBC DECIMAL、NUMERIC类型解析为Integer,默认方式 -->
        <!-- true: JDBC DECIMAL、NUMERIC类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成model模型的包名和位置 -->
        <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
            <!-- 是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"></property>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"></property>
        </javaModelGenerator>

        <!-- 生成xml mapper映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_java}">
            <property name="enableSubPackages" value="true"></property>
        </sqlMapGenerator>

        <!--生成xml dao 的包名和位置 -->
        <javaClientGenerator targetPackage="${daoPackage}" targetProject="${src_main_java}"  type="XMLMAPPER">
            <property name="enableSubPackages" value="true"></property>
        </javaClientGenerator>

        <!-- sql占位符,表示所有的表 -->
        <table tableName="%" mapperName="{0}Dao"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>

            <!-- 忽略该字段(可省略) -->
            <!--<ignoreColumn column="name" />-->

        </table>
    </context>
</generatorConfiguration>

generatorConfig3.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

    <!--数据库驱动 -->
    <classPathEntry location="db2jcc_license_cu.jar"/>
    <classPathEntry location="db2jcc4.jar"/>
    <classPathEntry location="db2java.jar"/>

    <context id="DB2Tables" targetRuntime="tk.mybatis.mapper.generator.TkMyBatis3Impl" defaultModelType="flat">

        <property name="javaFileEncoding" value="UTF-8"/>

        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
            <property name="caseSensitive" value="true"/>
        </plugin>

        <!-- 是否去除自动生成的注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"></property>
            <property name="suppressDate" value="true"></property>
            <property name="javaFileEncoding" value="utf-8"/>
        </commentGenerator>

        <!-- 数据库连接的信息 -->
        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}"
                        userId="${userId}"
                        password="${password}">
        </jdbcConnection>

        <!-- false:JDBC DECIMAL、NUMERIC类型解析为Integer,默认方式 -->
        <!-- true: JDBC DECIMAL、NUMERIC类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成model模型的包名和位置 -->
        <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
            <!-- 是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"></property>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"></property>
        </javaModelGenerator>

        <!-- 生成xml mapper映射文件的包名和位置 --><!-- mapper.xml -->
        <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_java}">
            <property name="enableSubPackages" value="true"></property>
        </sqlMapGenerator>

        <!--生成xml dao 的包名和位置 --><!-- mapper接口 -->
        <javaClientGenerator targetPackage="${daoPackage}" targetProject="${src_main_java}" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"></property>
        </javaClientGenerator>

        <table schema="test1" tableName="%" mapperName="{0}Dao"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <generatedKey column="id" sqlStatement="DB2" identity="true"/>
        </table>

        <!--<table schema="twhis" tableName="%" mapperName="{0}Dao"-->
               <!--enableCountByExample="false" enableUpdateByExample="false"-->
               <!--enableDeleteByExample="false" enableSelectByExample="false"-->
               <!--selectByExampleQueryId="false">-->
            <!--<generatedKey column="id" sqlStatement="DB2" identity="true"/>-->
        <!--</table>-->

        </context>
    </generatorConfiguration>

#3、spring-mybatis.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
    <!--数据连接池,Mybatis相关,pagehelpler插件,Mybatis通用Mapper配置相关-->

    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:webServiceConfig.properties</value>
            </list>
        </property>
    </bean>

    <!--配置druid监控拦截器-->
    <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
        <property name="slowSqlMillis" value="10000"/>
        <property name="logSlowSql" value="true"/>
    </bean>
    <!--&lt;!&ndash;配置druid日志输出拦截器&ndash;&gt;-->
    <bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
        <property name="resultSetLogEnabled" value="false"/>
        <property name="statementLogErrorEnabled" value="true"/>
        <property name="connectionLogErrorEnabled" value="true"/>
    </bean>
    <!--配置SQL注入拦截器-->
    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
        <property name="dbType" value="mysql"/>
    </bean>

    <!--配置druid连接池-->
    <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本属性 driver、url、user、password -->
        <property name="driverClassName" value="${ds1.jdbc_driver}"/>
        <property name="url" value="${ds1.jdbc_url}"/>
        <property name="username" value="${ds1.jdbc_username}"/>
        <property name="password" value="${ds1.jdbc_password}"/>

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="${initialSize}"/>
        <property name="minIdle" value="${minIdle}"/>
        <property name="maxActive" value="${maxActive}"/>

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="${maxWait}"/>
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/>
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/>

        <property name="validationQuery" value="SELECT 'x'"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <!-- MySQL不需要做PSCache,只有Oracle/DB2/SQL Server之类支持游标的数据库才需要配置成true -->
        <property name="poolPreparedStatements" value="false"/>
        <!-- 如果是Oracle/DB2/SQL Server之类支持游标的数据库需要加上以下配置:指定每个连接上PSCache的大小 -->
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>

        <!-- durib监控,否则不显示数据 -->
        <property name="filters" value="stat"></property>

        <!-- 打开removeAbandoned功能:连接泄漏监测 -->
        <property name="removeAbandoned" value="true"/>
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800"/>
        <!-- 打开abandoned连接时输出错误日志 -->
        <property name="logAbandoned" value="true"/>

        <!--通过proxyFilters属性配置StatFilter-->
        <property name="proxyFilters">
            <list>
                <ref bean="stat-filter"/>
                <ref bean="log-filter"/>
                <ref bean="wall-filter"/>
            </list>
        </property>
    </bean>

    <!--配置druid连接池-->
    <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本属性 driver、url、user、password -->
        <property name="driverClassName" value="${ds2.jdbc_driver}"/>
        <property name="url" value="${ds2.jdbc_url}"/>
        <property name="username" value="${ds2.jdbc_username}"/>
        <property name="password" value="${ds2.jdbc_password}"/>

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="${initialSize}"/>
        <property name="minIdle" value="${minIdle}"/>
        <property name="maxActive" value="${maxActive}"/>

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="${maxWait}"/>
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/>
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/>

        <!-- 检测连接是否有效的SQL -->
        <property name="validationQuery" value="SELECT 'x'"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <!-- MySQL不需要做PSCache,只有Oracle/DB2/SQL Server之类支持游标的数据库才需要配置成true -->
        <property name="poolPreparedStatements" value="false"/>
        <!-- 如果是Oracle/DB2/SQL Server之类支持游标的数据库需要加上以下配置:指定每个连接上PSCache的大小 -->
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>

        <!-- durib监控,否则不显示数据 -->
        <property name="filters" value="stat"></property>

        <!-- 打开removeAbandoned功能:连接泄漏监测 -->
        <property name="removeAbandoned" value="true"/>
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800"/>
        <!-- 打开abandoned连接时输出错误日志 -->
        <property name="logAbandoned" value="true"/>

        <!--通过proxyFilters属性配置StatFilter-->
        <property name="proxyFilters">
            <list>
                <ref bean="stat-filter"/>
                <ref bean="log-filter"/>
                <ref bean="wall-filter"/>
            </list>
        </property>
    </bean>

    <!--配置druid连接池-->
    <bean id="dataSource3" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本属性 driver、url、user、password -->
        <property name="driverClassName" value="${ds3.jdbc_driver}"/>
        <property name="url" value="${ds3.jdbc_url}"/>
        <property name="username" value="${ds3.jdbc_username}"/>
        <property name="password" value="${ds3.jdbc_password}"/>

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="${initialSize}"/>
        <property name="minIdle" value="${minIdle}"/>
        <property name="maxActive" value="${maxActive}"/>

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="${maxWait}"/>
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/>
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/>

        <property name="validationQuery" value="select 1 from sysibm.sysdummy1"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <!-- MySQL不需要做PSCache,只有Oracle/DB2/SQL Server之类支持游标的数据库才需要配置成true -->
        <property name="poolPreparedStatements" value="false"/>
        <!-- 如果是Oracle/DB2/SQL Server之类支持游标的数据库需要加上以下配置:指定每个连接上PSCache的大小 -->
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>

        <!-- durib监控,否则不显示数据 -->
        <property name="filters" value="stat"></property>

        <!-- 打开removeAbandoned功能:连接泄漏监测 -->
        <property name="removeAbandoned" value="true"/>
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800"/>
        <!-- 打开abandoned连接时输出错误日志 -->
        <property name="logAbandoned" value="true"/>

        <!--通过proxyFilters属性配置StatFilter-->
        <property name="proxyFilters">
            <list>
                <ref bean="stat-filter"/>
                <ref bean="log-filter"/>
                <ref bean="wall-filter"/>
            </list>
        </property>
    </bean>

    <bean class="com.system.util.database.MultipleDataSource" id="dynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry value-ref="dataSource1" key="dataSource1"></entry>
                <entry value-ref="dataSource2" key="dataSource2"></entry>
                <entry value-ref="dataSource3" key="dataSource3"></entry>
            </map>
        </property>
        <property name="defaultTargetDataSource" ref="dataSource1"></property>
    </bean>

    <!--配置Mybatis插件 通用mapper组件-->
    <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.system.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

    <!--配置mybatis的sqlSession-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"
          scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dynamicDataSource"/>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            helperDialect=mysql
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

</beans>

#4、spring.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        ">
    <!--总配置文件,引入所有配置文件和事务配置相关-->

    <!--引入所有配置文件-->
    <import resource="spring-mybatis.xml"/>
    <import resource="spring-beans.xml"/>
    <import resource="spring-mvc.xml"/>

    <!-- 扫描注解,除去com.system注解,com.system层注解在mvc配置中扫描 -->
    <context:component-scan
            base-package="com.system">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.RestController"/>
        <context:exclude-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!-- 定义单事务管理器(声明式的事务) -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dynamicDataSource"/>
    </bean>

    <!-- 基于注解的事务配置-->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    <aop:aspectj-autoproxy proxy-target-class="true"/>


</beans>

#5、build.gradle配置

group 'com.buptnml'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

//配置外部属性
ext {
    spring_version = "5.0.6.RELEASE"
}

configurations {
    mybatisGenerator
}

dependencies {
    compile group: 'dom4j', name: 'dom4j', version: '1.6.1'
    compile 'com.fasterxml.jackson.core:jackson-core:2.8.5'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.5'
    compile 'org.springframework:spring-webmvc:4.3.7.RELEASE'
    compile 'org.springframework:spring-orm:4.3.7.RELEASE'
    compile 'org.springframework:spring-context-support:4.3.7.RELEASE'
    compile 'org.springframework:spring-aop:5.0.0.RELEASE'
    compile 'org.springframework:spring-aspects:5.0.0.RELEASE'
    compile 'mysql:mysql-connector-java:5.1.38'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '6.4.0.jre8'
    compile 'org.mybatis:mybatis:3.4.2'
    compile 'org.mybatis:mybatis-spring:1.3.1'
    compile 'com.github.pagehelper:pagehelper:5.0.0'
    compile 'org.slf4j:slf4j-log4j12:1.7.25'
    compile 'tk.mybatis:mapper:3.4.0'
    compile 'com.alibaba:druid:1.0.31'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
    compile "io.springfox:springfox-swagger2:2.7.0"
    compile 'io.springfox:springfox-swagger-ui:2.7.0'
    compile group: 'org.mybatis.generator', name: 'mybatis-generator-core', version: '1.3.6'
    compile group: 'com.github.pagehelper', name: 'pagehelper', version: '5.1.4'
    compile group: 'com.github.jsqlparser', name: 'jsqlparser', version: '1.2'
    compile group: 'com.github.abel533', name: 'ECharts', version: '3.0.0.4'

    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'

    //MyBatis
    compile "org.mybatis:mybatis:3.0.5"
    //mybatis spring 插件
    compile "org.mybatis:mybatis-spring:1.0.1"
    //数据库驱动
    compile "mysql:mysql-connector-java:5.1.23"
    //连接池
    compile "com.alibaba:druid:1.0.12"
    //json
    compile "com.google.code.gson:gson:2.2.4"
    //log4j
    compile "log4j:log4j:1.2.17"
    compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'

    //单元测试
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
    testCompile group: 'org.springframework', name: 'spring-test', version: '2.5'
    
    mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5'
    mybatisGenerator 'org.mybatis.generator:mybatis-generator-maven-plugin:1.3.5'
    mybatisGenerator 'mysql:mysql-connector-java:5.1.38'
    mybatisGenerator 'tk.mybatis:mapper:3.4.0'
}

//执行mybatisGenerate任务自动生成model mapper和dao文件
def getDbProperties = {
    def properties = new Properties()
    file("src/main/resources/jdbc.properties").withInputStream { inputStream ->
        properties.load(inputStream)
    }
    properties
}

task mybatisGenerate  {
    def properties = getDbProperties()
    ant.properties['targetProject'] = projectDir.path
    ant.properties['driverClass'] = properties.getProperty("ds1.jdbc_driver")
    ant.properties['connectionURL'] = properties.getProperty("ds1.jdbc_url")
    ant.properties['userId'] = properties.getProperty("ds1.jdbc_username")
    ant.properties['password'] = properties.getProperty("ds1.jdbc_password")
    ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
    ant.properties['modelPackage'] = properties.getProperty("ds1.model.package")
    ant.properties['daoPackage'] = properties.getProperty("ds1.dao.package")
    ant.properties['sqlMapperPackage'] = properties.getProperty("ds1.xml.sqlmapper.package")
    ant.taskdef(
            name: 'mbgenerator',
            classname: 'org.mybatis.generator.ant.GeneratorAntTask',
            classpath: configurations.mybatisGenerator.asPath
    )

    ant.mbgenerator(overwrite: true,
            configfile: 'src/main/resources/generatorConfig.xml', verbose: true) {
        propertyset {
            propertyref(name: 'targetProject')
            propertyref(name: 'userId')
            propertyref(name: 'driverClass')
            propertyref(name: 'connectionURL')
            propertyref(name: 'password')
            propertyref(name: 'src_main_java')
            propertyref(name: 'src_main_resources')
            propertyref(name: 'modelPackage')
            propertyref(name: 'daoPackage')
            propertyref(name: 'sqlMapperPackage')
        }
    }
}

task mybatisGenerateNM  {
    def properties = getDbProperties()
    ant.properties['targetProject'] = projectDir.path
    ant.properties['driverClass'] = properties.getProperty("ds2.jdbc_driver")
    ant.properties['connectionURL'] = properties.getProperty("ds2.jdbc_url")
    ant.properties['userId'] = properties.getProperty("ds2.jdbc_username")
    ant.properties['password'] = properties.getProperty("ds2.jdbc_password")
    ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
    ant.properties['modelPackage'] = properties.getProperty("ds2.model.package")
    ant.properties['daoPackage'] = properties.getProperty("ds2.dao.package")
    ant.properties['sqlMapperPackage'] = properties.getProperty("ds2.xml.sqlmapper.package")
    ant.taskdef(
            name: 'mbgenerator',
            classname: 'org.mybatis.generator.ant.GeneratorAntTask',
            classpath: configurations.mybatisGenerator.asPath
    )

    ant.mbgenerator(overwrite: true,
            configfile: 'src/main/resources/generatorConfig2.xml', verbose: true) {
        propertyset {
            propertyref(name: 'targetProject')
            propertyref(name: 'userId')
            propertyref(name: 'driverClass')
            propertyref(name: 'connectionURL')
            propertyref(name: 'password')
            propertyref(name: 'src_main_java')
            propertyref(name: 'src_main_resources')
            propertyref(name: 'modelPackage')
            propertyref(name: 'daoPackage')
            propertyref(name: 'sqlMapperPackage')
        }
    }
}

task mybatisGenerateDB2  {
    def properties = getDbProperties()
    ant.properties['targetProject'] = projectDir.path
    ant.properties['driverClass'] = properties.getProperty("ds3.jdbc_driver")
    ant.properties['connectionURL'] = properties.getProperty("ds3.jdbc_url")
    ant.properties['userId'] = properties.getProperty("ds3.jdbc_username")
    ant.properties['password'] = properties.getProperty("ds3.jdbc_password")
    ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
    ant.properties['modelPackage'] = properties.getProperty("ds3.model.package")
    ant.properties['daoPackage'] = properties.getProperty("ds3.dao.package")
    ant.properties['sqlMapperPackage'] = properties.getProperty("ds3.xml.sqlmapper.package")
    ant.taskdef(
            name: 'mbgenerator',
            classname: 'org.mybatis.generator.ant.GeneratorAntTask',
            classpath: configurations.mybatisGenerator.asPath
    )

    ant.mbgenerator(overwrite: true,
            configfile: 'src/main/resources/generatorConfig3.xml', verbose: true) {
        propertyset {
            propertyref(name: 'targetProject')
            propertyref(name: 'userId')
            propertyref(name: 'driverClass')
            propertyref(name: 'connectionURL')
            propertyref(name: 'password')
            propertyref(name: 'src_main_java')
            propertyref(name: 'src_main_resources')
            propertyref(name: 'modelPackage')
            propertyref(name: 'daoPackage')
            propertyref(name: 'sqlMapperPackage')
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值