mybatis+druid+springboot 注解方式配置多个数据源
1\数据库配置
#test数据源 spring.datasource.test.url=jdbc:mysql://*:3306/db?useUnicode=true&characterEncoding=utf-8 spring.datasource.test.username=root spring.datasource.test.password=*** #线上环境 spring.datasource.online.url=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=utf-8 spring.datasource.online.username=root spring.datasource.online.password=*** spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=1 spring.datasource.maxActive=50 # 配置获取连接等待超时的时间 spring.datasource.maxWait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.poolPreparedStatements=false #spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.filters=stat,wall,log4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
2\数据源配置
package com.test.config; import javax.sql.DataSource; 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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; @Configuration @MapperScan(basePackages = "com.test.mapper.platform", sqlSessionTemplateRef = "testSqlSessionTemplate") public class PlatFormDataSourceConfig { @Bean(name = "testDataSource") @ConfigurationProperties(prefix = "spring.datasource.test") @Primary public DataSource testDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "testSqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/platform/*.xml")); return bean.getObject(); } @Bean(name = "testTransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("testDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "testSqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
3、service层
package com.test.service.test; import com.test.mapper.platform.UserMapper; import com.test.model.platform.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class test { @Autowired private UserMapper userMapper; public test() { } public void testtest(){ User user=userMapper.selectByPrimaryKey(1); System.out.println("///////////////////////////////"); System.out.println(user.getId()); } }
4\
package com.test.controller; import com.test.service.test.test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping(value = "/test") @Controller public class TestCase { @Autowired private test testtest; @RequestMapping(value = "/index",method = RequestMethod.GET) @ResponseBody public String index(){ testtest.testtest(); return "hello"; } }