根据项目功能需求,需要与第三方公共库对接,需要对公共库进行相关操作,由于不想使用原生jdbc,所以采用mybaits进行多数据配置。
单纯的使用mybaits进行多数据配置网上资料很多,但由于前期为了方便开发,采用了tk-mybaits。关于tk-mybaits多数据源配置没有相关材料,官方也没有提供相关功能说明,所以最终采用mybatis+tk-mybaits分别使用的策略进行多数据源配置功能实现。具体代码如下
一、配置连接信息
#开发运行环境数据库
spring.datasource.local.url=jdbc:mysql://0.0.0.0:3306/zhdd
spring.datasource.local.username=root
spring.datasource.local.password=
spring.datasource.local.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.local.druid.initial-size=1
spring.datasource.local.druid.min-idle=1
spring.datasource.local.druid.max-active=20
spring.datasource.local.druid.test-on-borrow=true
spring.datasource.local.druid.stat-view-servlet.allow=true
#第三方对接通用数据库
spring.datasource.public.url=jdbc:mysql://0.0.0.0:3306/zhdd
spring.datasource.public.username=root
spring.datasource.public.password=
spring.datasource.public.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.public.druid.initial-size=1
spring.datasource.public.druid.min-idle=1
spring.datasource.public.druid.max-active=20
spring.datasource.public.druid.test-on-borrow=true
spring.datasource.public.druid.stat-view-servlet.allow=true
二、配置DataSource数据源
注意两个数据源中有一个是为主数据源,需要增加一个@Primary注解,有且只能有一个需要。关于2个以上数据源没有额外再进行研究了,不过利用mybatis再额外配置多个数据源应该可以实现
1.数据源1配置
package com.hiynn.core.mybatis;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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 org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* 开发运行环境数据库
*/
@Configuration
public class LocalDataSourceConfig {
@Bean(name = "localDataSource")
@ConfigurationProperties(prefix = "spring.datasource.local")
@Primary
public DataSource localDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "localTransactionManager")
@Primary
public DataSourceTransactionManager mysqlTransactionManager() {
return new DataSourceTransactionManager(localDataSource());
}
@Bean(name = "localSqlSessionFactory")
@Primary
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//如果不使用xml的方式配置mapper,则可以省去下面这行mapper location的配置。
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
return sessionFactory.getObject();
}
}
2.数据源2配置
package com.hiynn.core.mybatis;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* 公共通用数据库
*/
@Configuration
public class PublicDataSourceConfig {
@Bean(name = "publicDataSource")
@ConfigurationProperties(prefix = "spring.datasource.public")
public DataSource publicDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "publicTransactionManager")
public DataSourceTransactionManager mysqlTransactionManager() {
return new DataSourceTransactionManager(publicDataSource());
}
@Bean(name = "publicSqlSessionFactory")
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("publicDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//如果不使用xml的方式配置mapper,则可以省去下面这行mapper location的配置。
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper2/*.xml"));
return sessionFactory.getObject();
}
}
三、Application配置
package com.hiynn.core;
//特别注意,下面的是 tk.MapperScan
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import tk.mybatis.spring.annotation.MapperScan;
/**
* @author zz
* @date 2018-7-2
*/
@ServletComponentScan
@SpringBootApplication
@EnableTransactionManagement
@EnableSwagger2
@MapperScan(basePackages = "com.hiynn.core.**.mapper",sqlSessionFactoryRef = "localSqlSessionFactory")
@org.mybatis.spring.annotation.MapperScan(basePackages = "com.hiynn.core.**.mapper2",sqlSessionFactoryRef = "publicSqlSessionFactory")
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
四、Mapper代码结构
补充:
一、解决mybatis数据库驼峰命名自动映射问题以及sql语句打印问题,通过读取配置文件实现。两个数据源都需要设置
@Bean(name = "localSqlSessionFactory")
@Primary
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//添加mybatis配置文件读取功能更
sessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("mybatis-config.xml"));
//如果不使用xml的方式配置mapper,则可以省去下面这行mapper location的配置。
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
return sessionFactory.getObject();
}
二、配置PageHelper分页插件,在application.properties统一配置
数据库方言 指定分页插件使用哪种数据库方言
pagehelper.helperDialect=mysql
#分页参数合理化 pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql