springboot配置多个数据源,导致分页插件失效,返回的total为0

直接在sqlSessionFactoryBean中指定分页插件

package com.gs.asset.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.gs.asset.common.utils.ThreadLocalContext;
import com.gs.asset.pojo.UserInfoSso;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.reflection.MetaObject;
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.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 org.springframework.transaction.PlatformTransactionManager;

import java.io.IOException;
import java.util.Date;

/**
 * DatasourceConfig 项目的数据源配置
 *
 * @author hyong
 * @since 2024/2/7
 */
@Configuration
@MapperScan(basePackages = "com.gs.asset.mapper")
public class DatasourceConfig {
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource dataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "sqlSessionFactory")
    @Primary
    public MybatisSqlSessionFactoryBean createSqlSessionFactoryBean(@Qualifier("dataSource") DruidDataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        setSqlSessionFactory(sqlSessionFactoryBean);
        //分页插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
        paginationInterceptor.setDbType(DbType.POSTGRE_SQL); // 设置数据库类型,根据实际情况修改
        interceptor.addInnerInterceptor(paginationInterceptor);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.addInterceptor(interceptor);
        sqlSessionFactoryBean.setConfiguration(configuration);

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        //配置填充器
        globalConfig.setMetaObjectHandler(new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                Date now = new Date();
                UserInfoSso ssoInfo = ThreadLocalContext.getSsoInfo();
                String currentUser = ssoInfo.getAccountGuid();
                if (metaObject.getValue("createTime") == null) {
                    setFieldValByName("createTime", now, metaObject);
                    setFieldValByName("updateTime", now, metaObject);
                }
                setFieldValByName("creator", currentUser, metaObject);
                setFieldValByName("updater", currentUser, metaObject);
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                UserInfoSso ssoInfo = ThreadLocalContext.getSsoInfo();
                String currentUser = ssoInfo.getAccountGuid();
                Date currentDate = new Date();
                setFieldValByName("updateTime", currentDate, metaObject);
                setFieldValByName("updater", currentUser, metaObject);
            }
        });
        sqlSessionFactoryBean.setGlobalConfig(globalConfig);

        return sqlSessionFactoryBean;
    }

    @Bean
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    @Primary
    public PlatformTransactionManager annotationDrivenTransactionManager(@Qualifier("dataSource") DruidDataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    private void setSqlSessionFactory(MybatisSqlSessionFactoryBean bean) throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources("classpath*:/mapper/*.xml"));
        bean.setVfs(SpringBootVFS.class);
        bean.setTypeAliasesPackage("com.gs.asset.entity");
    }
}

 

posted @ 2024-03-13 16:30  介寒食  阅读(207)  评论(0编辑  收藏  举报