**以上为项目目录
本次数据源一个在mysql,一个在sqlserver
一、数据源配置文件
com/shiro/configBean/DataSourceMysqlConfig:
package com.shiro.configBean; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.shiro.mapper.mysql",sqlSessionTemplateRef = "mysqlSqlSessionTemplate") public class DataSourceMysqlConfig { @Bean(name = "mysqlDataSource") @ConfigurationProperties(prefix = "spring.datasource.test2") public DataSource test2DataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "mysqlSqlSessionFactory") public SqlSessionFactory test2SqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:Mybatis/mysql/**/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean(name = "mysqlTransactionMananger") public DataSourceTransactionManager test2TransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } @Bean(name = "mysqlSqlSessionTemplate") public SqlSessionTemplate test2SqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory){ // sqlsessionTemplate:接受sqlsessionfactory中的数据来创建sqlsession return new SqlSessionTemplate(sqlSessionFactory); } }
com/shiro/configBean/DataSourceSqlServerConfig:
package com.shiro.configBean; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.shiro.mapper.sqlserver",sqlSessionTemplateRef = "sqlserverSqlSessionTemplate") public class DataSourceSqlServerConfig { // @Bean:将方法生命成对象交个spring来管理。spring只引用一次该方法,然后就交由springIOC来管理 @Bean(name="sqlserverDataSource") @ConfigurationProperties(prefix = "spring.datasource.test1") // 当一个类中有多个@Bean时,加@Primary注解的可以优先加载 @Primary public DataSource testDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "sqlserverSqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactroy(@Qualifier("sqlserverDataSource") DataSource dataSource) throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:Mybatis/sqlserver/**/*.xml")); return sqlSessionFactoryBean.getObject(); } @Bean(name = "sqlserverTransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("sqlserverDataSource") DataSource dataSource) throws Exception{ return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlserverSqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("sqlserverSqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } }
解释:
(1)、@MapperScan(basePackages=" ",sqlSessionTemplateRef=" ")
*该注解的作用是将该数据源配置和要对该数据源操作的dao层进行对应
(2)、@ConfigurationProperties(profix=" ")
*该注解表名配置的多个数据源要引用那一个
(3)、@Primary
*上边我们配置两个数据源,每个里边都有@Bean,加@Primary的会优先被spring管理,所以一般都加在主数据源
二、application.properties(项目配置文件)
#开发环境配置 #服务器的http端口号,默认为8080 server.port=8080 #应用的访问路径 server.servlet.context-path=/ #tomcat的url编码 server.tomcat.uri-encoding=UTF-8 #tomcat的最大线程数 server.tomcat.max-threads=800 #tomcat启动初始化的线程数,默认值 server.tomcat.min-spare-threads=30 #数据库连接信息 spring.datasource.test1.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.test1.jdbc-url=jdbc:sqlserver://localhost:1433;DatabaseName=material spring.datasource.test1.username=sa spring.datasource.test1.password=tp123+1 spring.datasource.test2.driver.class-name=com.mysql.cj.jdbc.Driver spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/powerplant?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 spring.datasource.test2.username=sa spring.datasource.test2.password=mike123456 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #数据库连接池配置 #初始连接数 spring.datasource.druid.initial-size=5 #最小连接池数量 spring.datasource.druid.min-idle=10 #最大连接池数量 spring.datasource.druid.max-active=20 #配置获取链接等待超时的时间 spring.datasource.druid.max-wait=60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.druid.time-between-eviction-runs-millis=60000 #配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.druid.min-evictable-idle-time-millis=300000 #配置一个连接在池中最大的生存时间,单位是毫秒 spring.datasource.druid.max-evictable-idle-time-millis=900000 #mybatis配置 ##搜索指定包别名(因为配置了两个数据源,暂时先不设置别名,直接在xml文件中设置实体类) #mybatis.type-aliases-package=com.shiro.entity #配置mapper的扫描,找到所有的mapper.xml映射文件 mybatis.mapper-locations=classpath*:Mybatis/**/*Mapper.xml
注意:开发的时候此文件需要注意的事情:
(1)、配置多数据源的时候,报错 jdbcUrl is required with driverClassName,原本使用的是spring.datasource.url,改为 spring.datasource.jdbc-url,具体原因还没找到
(2)、mybatis.type-aliases-package=com.shiro.entity,这个指定包别名的也不要用了,因为配置了多个数据源,就直接在xml文件中指定。(在application.properties中配置的方法还未找到)
三、mybatis文件
Mybatis/mysql/MUserTable/MUserTabelMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.shiro.mapper.mysql.MUserTableMapper"> <resultMap id="MUserTable" type="com.shiro.entity.mysql.MUserTable"> <id column="id" property="id"></id> <result column="username" property="username"></result> <result column="password" property="password"></result> </resultMap> <select id="selectAll" resultMap="MUserTable"> select * from mysql_usertable </select> </mapper>
Mybatis/sqlserver/UserTable/UserTabelMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.shiro.mapper.sqlserver.UserTableMapper"> <resultMap id="usertable" type="com.shiro.entity.sqlserver.UserTable"> <id property="id" column="id"></id> <result property="username" column="username"></result> <result property="password" column="password"></result> </resultMap> <select id="selectAll" resultMap="usertable"> select * from usertable </select> </mapper>
注意:
(1)、namespace要写正确
(2)、result中的type要写完整(因为还没有找到多数据源在application.properties文件配置文件别名的写法)
四、其他文件就按照正常springboot项目开发就好
五、DUAL表
DUAL表存在于oracle系的数据库中,是实际存在的表,任何用户都可读取,sqlserver中没有该表,当使用数据库连接池做数据库测试连接时,配置文件中的语法“validationQuery: SELECT 1 FROM DUAL”,mysql数据库可以使用select 1 from dual,sqlserver中没有该表,程序运行就会报该语法不适用于sqlserver,就手动建一个DUAL表,以解决该问题(本人测试是可以的,或许有些片面,请大家补充)
本文参考了 http://www.ityouknow.com/springboot/2016/11/25/spring-boot-multi-mybatis.html