springboot + mybatis+多数据源 + 分页插件 集成

1. 引入依赖


        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.5
            
                
                    mybatis-spring-boot-starter
                    org.mybatis.spring.boot
                
            
        
    

2. 定义数据源

package com.slodon.b2c.starter.datasource;

import com.github.pagehelper.PageInterceptor;
import com.slodon.b2c.core.constant.StarterConfigConst;
import org.apache.ibatis.plugin.Interceptor;
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.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 数据库工厂bean配置
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ReadDataSourceProperties.class)
//数据源对象dao所在包-read
@MapperScan(basePackages = StarterConfigConst.DATASOURCE_MAPPER_SCAN_BASE_PACKAGE_READ, sqlSessionFactoryRef = "sqlSessionFactoryRead")
public class ReadDataSourceAutoConfiguration {

    @Autowired
    private ReadDataSourceProperties readDataSourceProperties;

    /*-------------多数据源配置开始,配合properties文件配置数据源------------------*/

    /**
     * read数据源
     *
     * @return
     */
    @Bean(value = "dataSourceRead")
    public DataSource dataSource0() {
        return DataSourceBuilder.create()
                .driverClassName(readDataSourceProperties.getDriverClassName())
                .url(readDataSourceProperties.getUrl())
                .username(readDataSourceProperties.getUsername())
                .password(readDataSourceProperties.getPassword())
                .build();
    }

    @Bean(value = "sqlSessionFactoryRead")
    public SqlSessionFactory sqlSessionFactory0(@Qualifier("dataSourceRead") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(StarterConfigConst.DATASOURCE_MAPPER_XML_LOCATIONS_READ));
        bean.setTypeAliasesPackage(StarterConfigConst.DATASOURCE_TYPE_ALIASES_PACKAGE_READ);
        //分页插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //数据库
        properties.setProperty("helperDialect", "mysql");
        //是否将参数offset作为PageNum使用
        properties.setProperty("offsetAsPageNum", "true");
        //是否进行count查询
        properties.setProperty("rowBoundsWithCount", "true");
        //是否分页合理化
        properties.setProperty("reasonable", "false");
        interceptor.setProperties(properties);
        bean.setPlugins(new Interceptor[] {interceptor});
        return bean.getObject();
    }
}
package com.slodon.b2c.starter.datasource;

import com.github.pagehelper.PageInterceptor;
import com.slodon.b2c.core.constant.StarterConfigConst;
import org.apache.ibatis.plugin.Interceptor;
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.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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 javax.sql.DataSource;
import java.util.Properties;

/**
 * 数据库工厂bean配置
 */
@Configuration
//数据源对象dao所在包-write
@EnableConfigurationProperties(WriteDataSourceProperties.class)
@MapperScan(basePackages = StarterConfigConst.DATASOURCE_MAPPER_SCAN_BASE_PACKAGE_WRITE, sqlSessionFactoryRef = "sqlSessionFactoryWrite")
public class WriteDataSourceAutoConfiguration {

    @Autowired
    private WriteDataSourceProperties writeDataSourceProperties;

    /*-------------多数据源配置开始,配合properties文件配置数据源------------------*/

    /**
     * write数据源,默认使用此数据源 {@link Primary}
     *
     * @return
     */
    @Bean(value = "dataSourceWrite")
    @Primary
    public DataSource dataSource0() {
        return DataSourceBuilder.create()
                .driverClassName(writeDataSourceProperties.getDriverClassName())
                .url(writeDataSourceProperties.getUrl())
                .username(writeDataSourceProperties.getUsername())
                .password(writeDataSourceProperties.getPassword())
                .build();
    }

    @Bean(value = "sqlSessionFactoryWrite")
    @Primary
    public SqlSessionFactory sqlSessionFactory0(@Qualifier("dataSourceWrite") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(StarterConfigConst.DATASOURCE_MAPPER_XML_LOCATIONS_WRITE));
        bean.setTypeAliasesPackage(StarterConfigConst.DATASOURCE_TYPE_ALIASES_PACKAGE_WRITE);
        //分页插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //数据库
        properties.setProperty("helperDialect", "mysql");
        //是否将参数offset作为PageNum使用
        properties.setProperty("offsetAsPageNum", "true");
        //是否进行count查询
        properties.setProperty("rowBoundsWithCount", "true");
        //是否分页合理化
        properties.setProperty("reasonable", "false");
        interceptor.setProperties(properties);
        bean.setPlugins(new Interceptor[] {interceptor});
        return bean.getObject();
    }
}

3. 启动类需要排除自动配置

springboot + mybatis+多数据源 + 分页插件 集成_第1张图片

 

你可能感兴趣的:(学习记录,spring,boot,java,spring)