3.spring boot mybatis druidマルチデータソース
11190 ワード
1.pom.xml
2.application.yml
同じデータ・ソースで複数の環境を構成する場合は、を参照してください.
2.spring boot ymlマルチ環境
3.java config形式でデータソースとmybatisを構成する
config/dsでのデータソースの構成
MasterDataSourceConfig:
ClusterDataSourceConfig
utils:xmlパスが配列であるという問題を解決する
MasterDataSourcePropertiesとClusterDataSourceProperties(同)
daoインタフェース:
これでサービスでdaoを呼び出すことができます.xmlとdaoインタフェースを1つのパッケージの下に置けばいいです.
4.0.0
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.2.0
cn.easyproject
ojdbc7
12.1.0.2.0
com.alibaba
druid
1.0.29
org.springframework.boot
spring-boot-maven-plugin
true
src/main/java
**/*.xml
true
2.application.yml
同じデータ・ソースで複数の環境を構成する場合は、を参照してください.
2.spring boot ymlマルチ環境
3.java config形式でデータソースとmybatisを構成する
config/dsでのデータソースの構成
MasterDataSourceConfig:
package com.yunchuang.config.ds;
import com.yunchuang.config.properties.MasterDataSourceProperties;
import com.yunchuang.utils.MyUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author
* @create 2017-04-07 9:57
*/
@Configuration
// Mapper
@MapperScan(basePackages = {MasterDataSourceConfig.PACKAGE1, MasterDataSourceConfig.PACKAGE2}, sqlSessionFactoryRef = MasterDataSourceConfig.NAME + "SqlSessionFactory")
public class MasterDataSourceConfig {
@Autowired
private MasterDataSourceProperties masterDataSourceProperties;
// master ,
//dao
static final String PACKAGE1 = "com.yunchuang.dao.master";
static final String PACKAGE2 = "com.yunchuang.dao.other";
//xml
private static final String mapperLocation1 = "classpath:com/yunchuang/dao/master/*.xml";
private static final String mapperLocation2 = "classpath:com/yunchuang/dao/other/*.xml";
private static final String[] mapperLocations = {mapperLocation1, mapperLocation2};
//
static final String NAME = "master";
//
@Bean(name = NAME + "DataSource")
@Primary
public DataSource dataSource() {
String driverClassName = masterDataSourceProperties.getDriverClassName();
String url = masterDataSourceProperties.getUrl();
String username = masterDataSourceProperties.getUsername();
String password = masterDataSourceProperties.getPassword();
return MyUtils.getDruidDataSource(driverClassName, url, username, password);
}
//
@Bean(name = NAME + "TransactionManager")
@Primary
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
//
@Bean(name = NAME + "SqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier(NAME + "DataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(MyUtils.resolveMapperLocations(mapperLocations));
return sessionFactory.getObject();
}
}
ClusterDataSourceConfig
package com.yunchuang.config.ds;
import com.yunchuang.config.properties.ClusterDataSourceProperties;
import com.yunchuang.utils.MyUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
*
*
* @author
* @create 2017-04-07 9:57
*/
@Configuration
@MapperScan(basePackages = {ClusterDataSourceConfig.PACKAGE1}, sqlSessionFactoryRef = ClusterDataSourceConfig.NAME + "SqlSessionFactory")
public class ClusterDataSourceConfig {
@Autowired
private ClusterDataSourceProperties clusterDataSourceProperties;
static final String PACKAGE1 = "com.yunchuang.dao.cluster";
//xml
private static final String mapperLocation1 = "classpath:com/yunchuang/dao/cluster/*.xml";
private static final String[] mapperLocations = {mapperLocation1};
//
static final String NAME = "cluster";
@Bean(name = NAME + "DataSource")
public DataSource dataSource() {
String driverClassName = clusterDataSourceProperties.getDriverClassName();
String url = clusterDataSourceProperties.getUrl();
String username = clusterDataSourceProperties.getUsername();
String password = clusterDataSourceProperties.getPassword();
return MyUtils.getDruidDataSource(driverClassName, url, username, password);
}
@Bean(name = NAME + "TransactionManager")
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean(name = NAME + "SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier(NAME + "DataSource") DataSource dataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(MyUtils.resolveMapperLocations(mapperLocations));
return sessionFactory.getObject();
}
}
utils:xmlパスが配列であるという問題を解決する
package com.yunchuang.utils;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
*
* @author
* @create 2017-04-07 16:59
*/
public class MyUtils {
public static DruidDataSource getDruidDataSource(String driverClassName, String url, String username, String password) {
DruidDataSource dataSource = new DruidDataSource();
// , druid url dbType, driverClassName
dataSource.setDriverClassName(driverClassName);
// url
dataSource.setUrl(url);
//
dataSource.setUsername(username);
//
dataSource.setPassword(password);
// 。 init , getConnection
dataSource.setInitialSize(1);
//
dataSource.setMinIdle(1);
//
dataSource.setMaxActive(20);
// , 。 maxWait , , , useUnfairLock true 。
dataSource.setMaxWait(1000);
return dataSource;
}
/**
* org.mybatis.spring.boot.autoconfigure MybatisProperties
*
* @param mapperLocations xml
* @return
*/
public static Resource[] resolveMapperLocations(String[] mapperLocations) {
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List resources = new ArrayList();
if (mapperLocations != null) {
String[] var3 = mapperLocations;
int var4 = var3.length;
for (int var5 = 0; var5 < var4; ++var5) {
String mapperLocation = var3[var5];
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException var8) {
;
}
}
}
return resources.toArray(new Resource[resources.size()]);
}
}
MasterDataSourcePropertiesとClusterDataSourceProperties(同)
package com.yunchuang.config.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
*
*
* @author
* @create 2017-04-07 10:57
*/
@Component
@ConfigurationProperties(prefix = "spring.datasource.master")
public class MasterDataSourceProperties {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
daoインタフェース:
package com.yunchuang.dao.master;
import com.yunchuang.domain.Person;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
/**
* dao
*
* @author
* @create 2017-04-07 9:38
*/
@Mapper
//@Component , idea
@Component
public interface IPersonDao {
Person loadById(Integer id);
}
これでサービスでdaoを呼び出すことができます.xmlとdaoインタフェースを1つのパッケージの下に置けばいいです.