mysql druid set names_SpringBoot配置多数据源(mysql+Mybatis+Druid+yml配置)

配置druid

spring-boot目前支持的连接池有dbcp,dbcp2,hikari,tomcat的jdbc(这也是默认使用的),并不默认支持druid。

Mybatis-Generator:

Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。 具体的Mybatis-Generator的介绍请各位自己找度娘。

yml配置

/server:

port:8081

spring:

datasource:

# 主数据源配置信息

primaryDataSource:

url: jdbc:mysql://XXX.XXX.XXX.XXX:3306/db1?characterEncoding=utf-8

username: username

password: password

# 使用druid数据源

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: com.mysql.jdbc.Driver

type-aliases-package: com.entity.database

mapper-locations: classpath:mapper/database/*.xml

configLocation: classpath:mapper/mybatis-config.xml

# 数据源二配置信息

secondDataSource:

url:jdbc:mysql://XXX.XXX.XXX.XXX:3306/db2?characterEncoding=utf-8

username: username

password: password

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: com.mysql.jdbc.Driver

filters: stat

maxActive: 20

initialSize: 1

maxWait: 60000

minIdle: 1

timeBetweenEvictionRunsMillis:60000

minEvictableIdleTimeMillis:300000

validationQuery: select 'x'

testWhileIdle:true

testOnBorrow: false

testOnReturn: false

poolPreparedStatements:true

maxOpenPreparedStatements:20

type-aliases-package: com.entity.second

mapper-locations: classpath:mapper//second/*.xml

configLocation: classpath:mapper/mybatis-config.xml

#pagehelper

pagehelper:

helperDialect:mysql

reasonable:true

supportMethodsArguments:true

params: count=countSql

增加两个配置文件

数据源配置一:PrimaryMybatisConfiguration

import javax.sql.DataSource;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

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.beans.factory.annotation.Value;

import org.springframework.boot.context.properties.ConfigurationProperties;

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 com.alibaba.druid.pool.DruidDataSource;

@Configuration

@MapperScan(basePackages = { "com.dao.database" },sqlSessionTemplateRef="primarySqlSessionTemplate")

public class PrimaryMybatisConfiguration {

private static Log logger = LogFactory.getLog(PrimaryMybatisConfiguration.class);

@Value("${spring.datasource.primaryDataSource.url}")

private String url;

@Value("${spring.datasource.primaryDataSource.username}")

private String username;

@Value("${spring.datasource.primaryDataSource.password}")

private String password;

@Bean(name = "primaryDataSource")

@Primary

public DataSource testDataSource() {

DruidDataSource dataSource = new DruidDataSource();

dataSource.setDriverClassName("com.mysql.jdbc.Driver");

dataSource.setUrl(url);

dataSource.setUsername(username);

dataSource.setPassword(password);

dataSource.setTestWhileIdle(true);

dataSource.setValidationQuery("select 1");

return dataSource;

}

@Bean(name = "primarySqlSessionFactory")

@Primary

public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

#如果生成的mybatis是xml形式,则下面需要加上.xml包所在的地址

#如果生成的mybatis使用的是ANNOTATEDMAPPER模式,则下面的一句可以注释掉

//bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/database/*.xml"));

return bean.getObject();

}

@Bean(name = "primaryTransactionManager")

@Primary

public DataSourceTransactionManager testTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

@Bean(name = "primarySqlSessionTemplate")

@Primary

public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

数据源配置二:

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.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration

@MapperScan(basePackages = "com.dao.second", sqlSessionTemplateRef = "assetSqlSessionTemplate")

public class SecondMybatisConfiguration {

@Value("${spring.datasource.secondDataSource.url}")

private String url;

@Value("${spring.datasource.secondDataSource.username}")

private String username;

@Value("${spring.datasource.secondDataSource.password}")

private String password;

@Bean(name = "secondDataSource")

public DataSource testDataSource() {

DruidDataSource dataSource = new DruidDataSource();

dataSource.setDriverClassName("com.mysql.jdbc.Driver");

dataSource.setUrl(url);

dataSource.setUsername(username);

dataSource.setPassword(password);

dataSource.setTestWhileIdle(true);

dataSource.setValidationQuery("select 1");

return dataSource;

}

@Bean(name = "secondSqlSessionFactory")

public SqlSessionFactory testSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

return bean.getObject();

}

@Bean(name = "secondTransactionManager")

public DataSourceTransactionManager testTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

@Bean(name = "secondSqlSessionTemplate")

public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

具体代码可以参考:

Mybatis是XML格式的多数据源配置:

Mybatis是java对象格式的多数据源配置:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值