1. 注册分页插件(不注册分页插件total也是0):
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*
如果不注册分页插件,那么查询数据时,数据能正常查询,但是total是0
* */@Configuration
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() { //注册插件MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //new一个mybatisplus插件对象,再下面注入各插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL)); // 分页插件//interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 乐观锁插件//乐观锁插件机制:Plus是在数据表中加上一个数据版本号 version 字段,表示数据被修改的次数。当数据被修改时,version 值会+1。当事务A要更新数据值时,在读取数据的同时也会读取 version 值,在提交更新时,会校验刚才读取到的 version 值与当前数据库中的 version 值相等return interceptor;}
}
注意:如果是单数据源此时分页插件能正常使用,而如果是多数据源,那么就必须添加以下内容,否则分页时查询总数total还是0
2. 多数据源定义:
@Bean(name = "sqlSessionFactoryPop")public SqlSessionFactory sqlSessionFactoryPop(@Qualifier("dataSourcePop") DataSource dataSource, MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean ();// 创建SqlSessionFactoryBean实例factory.setDataSource(dataSource);// 设置数据源factory.setTypeAliasesPackage("com.epson.vipwp.domain");Interceptor[] interceptors = new Interceptor[] { mybatisPlusInterceptor };factory.setPlugins(interceptors);// 添加 SqlSessionTemplate Bean 的定义return factory.getObject();// 返回SqlSessionFactory实例,用于创建SqlSession,使用SqlSession调用sql时不需要指定数据源,因为就在此处指定了}
重点就是以下两行:
Interceptor[] interceptors = new Interceptor[] { mybatisPlusInterceptor };
factory.setPlugins(interceptors); //也就是多数据源必须在定义数据源时添加以上2句,否则分页插件无效
我自己4个数据源的完整定义:
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
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.context.properties.ConfigurationProperties;
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.jdbc.core.JdbcTemplate;import javax.sql.DataSource;/*** 数据源配置类*/
@Configuration
@MapperScan(basePackages = {"com.epson.vipwp.dao", "com.epson.vipwp.config"}) //直接MyBatisPlusConfig定义
public class DataSourceConfig {
//以下四个DataSource不能定义在这,否则将循环引用,springboot无法启动
// @Autowired private DataSource dataSourceAssy; // 装配ASSY数据源 ---//省略后面几个,反正也不能定义// 定义ASSY数据源相关的Bean// 使用@Primary注解表示当存在多个同类型的Bean时,优先选择该Bean@Primary@Bean(name = "dataSourceAssy")@ConfigurationProperties(prefix = "spring.datasource.assy")public DataSource dataSourceAssy() {return DataSourceBuilder.create().build();} // 使用DataSourceBuilder创建数据源@Primary@Bean(name = "jdbcTemplateAssy")public JdbcTemplate jdbcTemplateAssy(@Qualifier("dataSourceAssy") DataSource dataSource) {return new JdbcTemplate(dataSource); // 创建JdbcTemplate实例,用于执行SQL操作}@Primary // 指定为默认SqlSessionFactory@Bean(name = "sqlSessionFactory") //默认数据源不能乱起名字,必须叫sqlSessionFactorypublic SqlSessionFactory sqlSessionFactoryAssy(@Qualifier("dataSourceAssy") DataSource dataSource, MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean ();// 创建SqlSessionFactoryBean实例factory.setDataSource(dataSource);// 设置数据源Interceptor[] interceptors = new Interceptor[] { mybatisPlusInterceptor };factory.setPlugins(interceptors);// 加载mybatis的相关配置文件,如果有的话// factoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));// 设置实体类别名包扫描factory.setTypeAliasesPackage("com.epson.vipwp.domain");// 添加 SqlSessionTemplate Bean 的定义return factory.getObject();// 返回SqlSessionFactory实例,用于创建SqlSession,使用SqlSession调用sql时不需要指定数据源,因为就在此处指定了}//如果要将 SqlSessionTemplate 作为单独的 Bean 注入,可以这样:@Primary@Bean(name = "sqlSessionTemplate")public SqlSessionTemplate sqlSessionTemplateAssy(SqlSessionFactory sqlSessionFactoryAssy) {return new SqlSessionTemplate(sqlSessionFactoryAssy);}//POP@Bean(name = "dataSourcePop")@ConfigurationProperties(prefix = "spring.datasource.pop")public DataSource dataSourcePop() {return DataSourceBuilder.create().build();}// 使用DataSourceBuilder创建POP数据源@Bean(name = "jdbcTemplatePop")public JdbcTemplate jdbcTemplatePop(@Qualifier("dataSourcePop") DataSource dataSource) {return new JdbcTemplate(dataSource);// 创建POP JdbcTemplate实例,用于执行SQL操作}@Bean(name = "sqlSessionFactoryPop")public SqlSessionFactory sqlSessionFactoryPop(@Qualifier("dataSourcePop") DataSource dataSource, MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean ();// 创建SqlSessionFactoryBean实例factory.setDataSource(dataSource);// 设置数据源factory.setTypeAliasesPackage("com.epson.vipwp.domain");Interceptor[] interceptors = new Interceptor[] { mybatisPlusInterceptor };factory.setPlugins(interceptors);// 添加 SqlSessionTemplate Bean 的定义return factory.getObject();// 返回SqlSessionFactory实例,用于创建SqlSession,使用SqlSession调用sql时不需要指定数据源,因为就在此处指定了}//如果要将 SqlSessionTemplate 作为单独的 Bean 注入,可以这样:@Bean(name = "sqlSessionTemplatePop")public SqlSessionTemplate sqlSessionTemplatePop(SqlSessionFactory sqlSessionFactoryPop) {return new SqlSessionTemplate(sqlSessionFactoryPop);}//OQC@Bean(name = "dataSourceOqc")@ConfigurationProperties(prefix = "spring.datasource.oqc")public DataSource dataSourceOqc() {return DataSourceBuilder.create().build();}@Bean(name = "jdbcTemplateOqc")public JdbcTemplate jdbcTemplateOqc(@Qualifier("dataSourceOqc") DataSource dataSource) {return new JdbcTemplate(dataSource);}@Bean(name = "sqlSessionFactoryOqc")public SqlSessionFactory sqlSessionFactoryOqc(@Qualifier("dataSourceOqc") DataSource dataSource, MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean ();// 创建SqlSessionFactoryBean实例factory.setDataSource(dataSource);// 设置数据源factory.setTypeAliasesPackage("com.epson.vipwp.domain");Interceptor[] interceptors = new Interceptor[] { mybatisPlusInterceptor };factory.setPlugins(interceptors);// 添加 SqlSessionTemplate Bean 的定义return factory.getObject();// 返回SqlSessionFactory实例,用于创建SqlSession,使用SqlSession调用sql时不需要指定数据源,因为就在此处指定了}//如果要将 SqlSessionTemplate 作为单独的 Bean 注入,可以这样:@Bean(name = "sqlSessionTemplateOqc")public SqlSessionTemplate sqlSessionTemplateOqc(SqlSessionFactory sqlSessionFactoryOqc) {return new SqlSessionTemplate(sqlSessionFactoryOqc);}//qpost@Bean(name = "dataSourceQpost")@ConfigurationProperties(prefix = "spring.datasource.qpost")public DataSource dataSourceQpost() {return DataSourceBuilder.create().build();}@Bean(name = "jdbcTemplateQpost")public JdbcTemplate jdbcTemplateQpost(@Qualifier("dataSourceQpost") DataSource dataSource) {return new JdbcTemplate(dataSource);}@Bean(name = "sqlSessionFactoryQpost")public SqlSessionFactory sqlSessionFactoryQpost(@Qualifier("dataSourceQpost") DataSource dataSource, MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean ();// 创建SqlSessionFactoryBean实例factory.setDataSource(dataSource);// 设置数据源factory.setTypeAliasesPackage("com.epson.vipwp.domain");Interceptor[] interceptors = new Interceptor[] { mybatisPlusInterceptor };factory.setPlugins(interceptors);// 添加 SqlSessionTemplate Bean 的定义return factory.getObject();// 返回SqlSessionFactory实例,用于创建SqlSession,使用SqlSession调用sql时不需要指定数据源,因为就在此处指定了}//如果要将 SqlSessionTemplate 作为单独的 Bean 注入,可以这样:@Bean(name = "sqlSessionTemplateQpost")public SqlSessionTemplate sqlSessionTemplateQpost(SqlSessionFactory sqlSessionFactoryQpost) {return new SqlSessionTemplate(sqlSessionFactoryQpost);}}