spring boot 基础篇 -- 阿里多数据源
这块是比较基础的配置,阿里数据库配置还是比较好用的,并且可以用来监控数据源的情况。废话不多说,下面看代码。
基于maven项目,在pom.xml中添加引用:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
配置文件如下:
server.port=9090
logging.level.tk.mybatis=TRACE druid1.type=com.alibaba.druid.pool.DruidDataSource
druid1.url=jdbc:mysql://192.168.0.190:3306/bestPractices
druid1.driver-class=com.mysql.jdbc.Driver
druid1.username=root
druid1.password=youeDATA2016_
druid1.initial-size=1
druid1.min-idle=1
druid1.max-active=20
druid1.test-on-borrow=true
druid1.filters=stat,wall,log4j
druid1.poolPreparedStatements=true
druid1.datasource.maxPoolPreparedStatementPerConnectionSize=20
druid1.datasource.logAbandoned=true druid2.type=com.alibaba.druid.pool.DruidDataSource
druid2.url=jdbc:mysql://192.168.0.190:3306/bestPracticesTwo
druid2.driver-class=com.mysql.jdbc.Driver
druid2.username=root
druid2.password=youeDATA2016_
druid2.initial-size=1
druid2.min-idle=1
druid2.max-active=20
druid2.test-on-borrow=true
druid2.filters=stat,wall,log4j
druid2.poolPreparedStatements=true
druid2.datasource.maxPoolPreparedStatementPerConnectionSize=20
druid2.datasource.logAbandoned=true spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
mybatis.type-aliases-package=bp.model
mybatis.mapper-locations=classpath:mybatis/mybatis-config.xml
mapper.mappers=bp.util.MyMapper
#mapper.not-empty=false
#mapper.identity=MYSQL
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.dialect=com.github.pagehelper.dialect.helper.MySqlDialect
pagehelper.params=count=countSql
pagehelper.autoDialect=true
pagehelper.closeConn=false
pagehelper.offsetAsPageNum=true spring.http.encoding.force=true
spring.redis.database= 0
spring.redis.host= 192.168.237.128
spring.redis.port= 6379
spring.redis.pool.max-active= 8
spring.redis.pool.max-idle= 8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.timeout= 1500
配置文件:数据源1
package bp.config; import java.sql.SQLException; 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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.alibaba.druid.pool.DruidDataSource; /**
* 数据源1
*/
@Configuration
@EnableConfigurationProperties(DruidProperties1.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@ConditionalOnClass(DruidDataSource.class)
@MapperScan(basePackages = "bp.mapper.mapper1", sqlSessionTemplateRef = "SqlSessionTemplate1")
public class DataSource1Config {
@Autowired
private DruidProperties1 properties; @Bean(name = "DataSource1")
@ConditionalOnProperty(prefix = "druid1", name = "url")
@Primary
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setFilters(properties.getFilters());
if (properties.getInitialSize() > 0) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > 0) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > 0) {
dataSource.setMaxActive(properties.getMaxActive());
}
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataSource;
} @Bean(name = "SqlSessionFactory1")
@Primary
public SqlSessionFactory test1SqlSessionFactory(@Qualifier("DataSource1") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setDataSource(dataSource);
bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis-config.xml"));
bean.setMapperLocations(resolver.getResources("bp/mapper/mapper1/*.xml"));
return bean.getObject();
} @Bean(name = "TransactionManager1")
@Primary
public DataSourceTransactionManager test1TransactionManager(@Qualifier("DataSource1") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "SqlSessionTemplate1")
@Primary
public SqlSessionTemplate test1SqlSessionTemplate(@Qualifier("SqlSessionFactory1") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
} }
数据源2:
package bp.config; import java.sql.SQLException; 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.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
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.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor; import javax.annotation.Resource;
import javax.sql.DataSource; /**
* 数据源2
*/
@Configuration
@EnableConfigurationProperties(DruidProperties2.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@ConditionalOnClass(DruidDataSource.class)
@MapperScan(basePackages = "bp.mapper.mapper2", sqlSessionTemplateRef = "SqlSessionTemplate2")
public class DataSource2Config { @Autowired
private DruidProperties2 properties; @Bean(name = "DataSource2")
@ConditionalOnProperty(prefix = "druid2", name = "url")
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setFilters(properties.getFilters());
if (properties.getInitialSize() > 0) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > 0) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > 0) {
dataSource.setMaxActive(properties.getMaxActive());
}
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataSource;
} @Bean(name = "SqlSessionFactory2")
public SqlSessionFactory test2SqlSessionFactory(@Qualifier("DataSource2") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setDataSource(dataSource);
bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatis-config.xml"));
bean.setMapperLocations(resolver.getResources("bp/mapper/mapper2/*.xml"));
return bean.getObject();
} @Bean(name = "TransactionManager2")
public DataSourceTransactionManager test2TransactionManager(@Qualifier("DataSource2") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "SqlSessionTemplate2")
public SqlSessionTemplate test2SqlSessionTemplate(@Qualifier("SqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
} }
启动项目就发现是双数据源了
spring boot 基础篇 -- 阿里多数据源的更多相关文章
- spring boot 基础篇 -- 定时任务
在日常项目中,常常会碰到定时监控项目中某个业务的变化,下面是spring boot 集成的定时任务具体配置: @Component public class IndexWarningScheduled ...
- spring boot 基础篇 -- 集成接口测试Swagger
一.在pom.xml加入Swagger jar包引入 <dependency> <groupId>io.springfox</groupId> <artifa ...
- spring boot 基础篇 -- 自带图片服务器
我们平时在日常项目中经常会遇到图片的上传和访问的情景,平时我们可能习惯于把图片传到resource或者项项目中的某个位置,这样会有一个缺点,当我们重新项目打包时,这些图片会丢失.为了解决这一缺点,我们 ...
- spring boot基础学习教程
Spring boot 标签(空格分隔): springboot HelloWorld 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新 ...
- Spring Boot 基础
Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...
- Spring Boot 基础教程系列学习文档
Spring Boot基础教程1-Spring Tool Suite工具的安装 Spring Boot基础教程2-RESTfull API简单项目的快速搭建 Spring Boot基础教程3-配置文件 ...
- Spring Boot HikariCP 一 ——集成多数据源
其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的. 参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource ...
- Spring Boot数据访问之动态数据源切换之使用注解式AOP优化
在Spring Boot数据访问之多数据源配置及数据源动态切换 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中详述了如何配置多数据源及多数据源之间的动态切换.但是需要读数据库的地方,就 ...
- Spring boot 提高篇
Spring boot 提高篇 上篇文章介绍了Spring boot初级教程:构建微服务:Spring boot 入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续 ...
随机推荐
- Vue.js刷新当前页面
Vue.js的路由跳转很强大,类似ajax的局部刷新,路由跳转时候页面是不刷新的,刷新当前页面的功能不建议用,但是有的时候确实需要刷新当前页面来实现某些需求,这个时候,我们有三 种方法可以实现. 第一 ...
- Linux用户相关文件之组文件
组信息文件: 1.文件地址: /etc/group -rw-r--r--. 1 root root 492 10月 6 21:56 /etc/group 2.文件内容: xiaol:x:500: 3. ...
- (4.16)sql server迁移DB文件(同一DB内)
SQL Server 修改数据库物理文件存在位置 关键词:迁移文件,迁移temp库(这怎么迁移呢,用方法2即可,需要重启实例) 三种均需要离线: 一共分为(1)脱机迁移和(2)在线迁移. (1)迁移方 ...
- centos7 Mysql5.6 升级Mysql5.7
1 2. 卸载Mysql5.6 ,一共有三个包 要卸载: (1)先卸载mysql-server包 : 执行命令 yum remove mysql mysql-server (2)再卸载mysql-c ...
- Android学习笔记之AndroidManifest.xml文件解析(详解)
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...
- 【saltstack】saltstack执行结果和事件存储到mysql
前言 项目中使用saltstack有一段时间了,之前都是在控制台操作,后来感觉越来越不方便,每次操作需要登陆服务器,还需要记一堆命令.最重要的是,公司进新人之后,新人由于不熟悉saltstack,容易 ...
- django基本安装
一.web框架 1.什么是web框架? Web框架是一种开发框架,用来支持动态网站.网络应用程序及网络服务的开发.其类型有基于请求的和基于组件的两种框架. 本质上其实就是一个socket服务端,用户的 ...
- SpringMVC:学习笔记(4)——处理模型数据
SpringMVC—处理模型数据 说明 SpringMVC 提供了以下几种途径输出模型数据: – ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添 ...
- LDAP注入
理解LDAP与LDAP注入 0x01 LDAP简介 查阅了一些网上的资料,读了好久还是觉得理解起来很困难,感觉还是不够干,之后看到的一个博客http://www.chinaunix.net/old_j ...
- yii2-lock-form 也许这就是你想要的,阻止表单多次提交
是不是被用户的行为所困扰? 一个表单用户点击提交按钮了N次,这也导致了数据提交了N次. 为了此受到了测试的欺辱,受到了老板的批评? 不用怕,它就是来拯救你的. 第一步:打开命令行,敲入 compose ...