mybatis+druid+springboot 注解方式配置多个数据源
1\数据库配置
#test数据源
spring.datasource.test.url=jdbc:mysql://*:/db?useUnicode=true&characterEncoding=utf-
spring.datasource.test.username=root
spring.datasource.test.password=***
#线上环境
spring.datasource.online.url=jdbc:mysql://127.0.0.1:/db?useUnicode=true&characterEncoding=utf-
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=
spring.datasource.minIdle=
spring.datasource.maxActive=
# 配置获取连接等待超时的时间
spring.datasource.maxWait=
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=
spring.datasource.validationQuery=SELECT FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=false
#spring.datasource.maxPoolPreparedStatementPerConnectionSize=
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=
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";
}
}
mybatis+druid+springboot 注解方式配置多个数据源的更多相关文章
- MyBatis 及 MyBatis Plus 纯注解方式配置(Spring Boot + Postgresql)
说明 当前的版本为 MyBatis 3.5.9 MyBatis Plus 3.5.1 Spring Boot 2.6.4 Postgresql 42.3.3 与 Spring Boot 结合使用 My ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- atitit.ajax bp dwr 3.的注解方式配置使用流程总结 VO9o.....
atitit.ajax bp dwr 3.的注解方式配置使用流程总结 VO9o..... 1. 安装配置 1 1.1. 下载 dwr.jar 1M 1 1.2. 配置注解方式..web.xml 1 ...
- atitit.ajax bp dwr 3.的注解方式配置使用流程总结.....
atitit.ajax bp dwr 3.的注解方式配置使用流程总结..... 1. 下载 dwr.jar 1M 1 2. 配置注解方式..web.xml 1 3. Class 配置 2 4. 测试 ...
- SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...
- Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- SpringMVC的注解方式配置
SpringMVC支持使用注解方式配置,比配置文件方式更加灵活易用,是SpringMVC使用的主流模式. 1.在配置文件中开启SpringMVC的注解 <!-- 开启包扫描 --> < ...
- spring学习笔记 星球日two - 注解方式配置bean
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
随机推荐
- 更新CentOS 6.7源为阿里源
1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载新的CentOS-Base ...
- PHP超级全局变量、魔术变量和魔术函数的区别和联系
PHP超级全局变量.魔术变量和魔术函数的区别和联系 一.总结 一句话总结:PHP超级全局变量主要用于web开发,魔术变量主要用于输出当前对象的信息,魔术函数则是对象的常用方法 相同点: 1.PHP超级 ...
- Java NIO框架 Mina、Netty、Grizzly
Mina Mina(Multipurpose Infrastructure for Network Applications) 是 Apache组织一个较新的项目,它为开发高性能和高可用性的网络应用程 ...
- linux下{}的用法
在touch {a,b}.txt时,同时创建了a.txt,b.txt两个文件 而touch {1..10}.txt,同时创建了10个txt文件,从1.txt到10.txt 在linux通配符中,{n, ...
- nt":false,"tarball":"http://registry.npm.taobao.org/babel-preset-stag
npm ERR! Unexpected end of input at 1:12777 npm ERR! nt":false,"tarball":"http:/ ...
- 【scala】匿名函数和闭包
函数的类型和值 Scala是一种纯面向对象的语言,每个值都是对象.Java是一种不全面向对象的语言. Scala也是一种函数式语言,其函数也能当成值来使用.Java则是指令试编程. 但是Scala同时 ...
- 使用Spring实现MySQL读写分离
1. 为什么要进行读写分离 大量的JavaWeb应用做的是IO密集型任务, 数据库的压力较大, 需要分流 大量的应用场景, 是读多写少, 数据库读取的压力更大 一个很自然的思路是使用一主多从的数据库集 ...
- Javascript鼠标和滚轮事件
(转自:http://www.cnblogs.com/MrBackKom/archive/2012/06/25/2562920.html) a)鼠标事件 鼠标事件也许是web页面当中最常用到的事件,因 ...
- 201621123003《Java程序设计》第一周学习总结
#1. 本周学习总结 本周主要学习了Java的jdk.jvm.jre等基本概念,Java的发展史,知道Java语言的跨平台.面向对象等主要特点,简单了解了Java程序的编译和运行过程.对于学习Java ...
- poscms用法总结(非定制开发,不涉及后台代码)
这些天几个企业站仿下来,对poscms的用法多少有些了解了,在这个记录一下,好记性不如烂笔头嘛. 1.静态文件目录和模板文件目录 这两个目录分别放置css/js/image等静态文件和html模板文件 ...