MyBatisPlus多数据源配置以及分页

0

Mapper路径

# 数据源(全局):mysql
/mybatis/dao/mapper/mysql/serMapper.xml
com.acgist.dao.mapper.mysql.UserMapper
# 数据源:oracle
/mybatis/dao/mapper/oracle/OrderMapper.xml
com.acgist.dao.mapper.oracle.OrderMapper
# 数据库:mssql
/mybatis/dao/mapper/mssql/LogMapper.xml
com.acgist.dao.mapper.mssql.LogMapper

数据配置

Oracle

package com.acgist.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@Import({DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
@MapperScan(basePackages = "com.acgist.dao.mapper.oracle", sqlSessionFactoryRef = "oracleSqlSessionFactory", sqlSessionTemplateRef = "oracleSqlSessionTemplate")
public class OracleConfiguration {

	static final String MAPPER_LOCATION = "classpath:/mybatis/dao/mapper/oracle/*.xml";

	@Bean("oracleDataSource")
	public DataSource oracleDataSource() {
		final HikariDataSource dataSource = new HikariDataSource();
		// TODO:加载HikariConfig
		return dataSource;
	}

	@Bean(name = "oracleTransactionManager")
	public DataSourceTransactionManager oracleTransactionManager(@Qualifier("oracleDataSource") DataSource oracleDataSource) {
		return new DataSourceTransactionManager(oracleDataSource);
	}

	@Bean(name = "oracleSqlSessionFactory")
	public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource oracleDataSource) throws Exception {
		final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
		sessionFactory.setDataSource(oracleDataSource);
		sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(OracleConfiguration.MAPPER_LOCATION));
		return sessionFactory.getObject();
	}
	
	@Bean(name = "oracleSqlSessionTemplate")
	public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory oracleSqlSessionFactory) {
		return new SqlSessionTemplate(oracleSqlSessionFactory);
	}

}

MSSQL

package com.acgist.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@Import({DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
@MapperScan(basePackages = "com.acgist.dao.mapper.mssql", sqlSessionFactoryRef = "mssqlSqlSessionFactory", sqlSessionTemplateRef = "mssqlSqlSessionTemplate")
public class MssqlConfiguration {

	static final String MAPPER_LOCATION = "classpath:/mybatis/dao/mapper/mssql/*.xml";

	@Bean("mssqlDataSource")
	public DataSource mssqlDataSource() {
		final HikariDataSource dataSource = new HikariDataSource();
		// TODO:加载HikariConfig
		return dataSource;
	}

	@Bean(name = "mssqlTransactionManager")
	public DataSourceTransactionManager mssqlTransactionManager(@Qualifier("mssqlDataSource") DataSource mssqlDataSource) {
		return new DataSourceTransactionManager(mssqlDataSource);
	}

	@Bean(name = "mssqlSqlSessionFactory")
	public SqlSessionFactory mssqlSqlSessionFactory(@Qualifier("mssqlDataSource") DataSource mssqlDataSource) throws Exception {
		final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
		sessionFactory.setDataSource(mssqlDataSource);
		sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MssqlConfiguration.MAPPER_LOCATION));
		return sessionFactory.getObject();
	}
	
	@Bean(name = "mssqlSqlSessionTemplate")
	public SqlSessionTemplate mssqlSqlSessionTemplate(@Qualifier("mssqlSqlSessionFactory") SqlSessionFactory mssqlSqlSessionFactory) {
		return new SqlSessionTemplate(mssqlSqlSessionFactory);
	}

}

MySQL

主数据源直接配置Application:

package com.acgist.main

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan(basePackages = "com.acgist.dao.mapper.mysql", sqlSessionFactoryRef = "sqlSessionFactory", sqlSessionTemplateRef = "sqlSessionTemplate")
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

分页

# 单数据源
@Bean
@ConditionalOnMissingBean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
	final MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
	interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
	return interceptor;
}
# 多数据源
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
final MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
sessionFactory.setPlugins(new Interceptor[] { mybatisPlusInterceptor });
sessionFactory.setDataSource(mysqlDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MySQLApplicationConfiguration.MAPPER_LOCATION));
return sessionFactory.getObject();

MyBatisPlus动态数据源

当然上面的代码是动态注入,也可以使用下面代码更加方便。

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
	<version>${version}</version>
</dependency>

spring:
  datasource:
    dynamic:
      primary: master
      strict: false
      hikari:
        min-idle: 10
        idle-timeout: 60000
        max-pool-size: 20
        connection-timeout: 60000
        connection-test-query: SELECT 1
        max-lifetime: 1800000
      datasource:
        master:
          type: com.zaxxer.hikari.HikariDataSource
          url: 
          username: 
          password: 
          driverClassName: com.mysql.cj.jdbc.Driver
        slave:
          type: com.zaxxer.hikari.HikariDataSource
          url: 
          username: 
          password: 
          driverClassName: com.mysql.cj.jdbc.Driver
        oracle:
          hikari:
            min-idle: 10
            idle-timeout: 60000
            max-pool-size: 20
            connection-timeout: 60000
            connection-test-query: SELECT 1 from dual
            max-lifetime: 1800000
          url: 
          username: 
          password: 
          driverClassName: oracle.jdbc.driver.OracleDriver

@Service
@DS("slave")
public class UserServiceImpl implements UserService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@DS("master")
	public List<User> selectAll() {
		return jdbcTemplate.queryForList("select * from user");
	}

	@Override
	@DS("slave")
	public List<User> selectByCondition() {
		return jdbcTemplate.queryForList("select * from user where age >10");
	}

}

参考地址:
https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter