自主配置数据源,mybatis/plus不打印sql日志问题
目录
- 问题:配置双数据源后不打印sql日志了
- 解决
- 总结
建议使用官方多数据源配置
问题:配置双数据源后不打印sql日志了
看了其他博客,这些配置我都有加
myBATis-plus: type-aliases-package: com.lyzw.cloud.common.entity # 所有Entity别名类所在包 mapper-locations: classpath:mapper/**/*.XML configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl call-setters-on-nulls: true cache-enabled: false
我的问题在配置双数据源后,走默认的mybatis创建工厂,没有各自配置
解决
对比下之前的和之后的
注意注入的
private MybatisPlusInterceptor mybatisPlusInterceptor;
- 主数据源配置前
import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; 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.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import Javax.sql.DataSource; @Configuration @MapperScan(basePackages = {"com.lyzw.cloud.platform.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory") public class PrimaryDataSourceConfig { @Bean(name = "dataSource") @ConfigurationProperties(prefix = "spring.datasource.druid.db1") @Primary public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.sjsetMapperlocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*/*.xml")); return bean.getObject(); } @Bean(name = "transactionManager") @Primary public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate") @Primary public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
- 后
import com.baomidou.mybatisplus.core.MybatisConfiguration; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.ibatis.logging.stdout.StdOutImpl; 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.beans.factory.annotation.Value; 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.context.annotation.Scope; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.annotation.Resource; import javax.sql.DataSource; /** * 主数据源配置 * * @author 陈琛 * @date 2022/05/09 */ @Configuration @MapperScan(basePackages = {"com.lyzw.cloud.platform.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory") public class PrimaryDataSourceConfig { @Resource private MybatisPlusInterceptor mybatisPlusInterceptor; @Value("${mybatis-plus.mapper-locations}") private String locationPattern; @Value("${mybatis-plus.type-aliases-package}") private String typeAliasesPackage; @Bean @Scope(value = "prototype") @ConfigurationProperties(prefix = "mybatis-plus.configuration") public MybatisConfiguration getCfg() { return new MybatisConfiguration(); } @Bean(name = "dataSource") @ConfigurationProperties(prefix = "spring.datasource.druid.db1") @Primary public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setPlugins(mybatisPlusInterceptor); bean.setConfiguration(getCfg()); bean.setTypeAliasesPackage(typeAliasesPackage); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(locationPattern)); return bean.getObject(); } @Bean(name = "transactionManager") @Primary public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate") @Primary public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
- 次数据源配置前
package com.lyzw.cloud.platform.config.dataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; 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.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = {"com.lyzw.cloud.platform.websiteMapper"}, sqlSessionFactoryRef = "sqlSessionFactory2") public class SecondDataSourceConfig { @Bean(name = "dataSource2") @ConfigurationProperties(prefix = "spring.datasource.druid.DB2") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory2") public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*/*.xml")); return bean.getObject(); } @Bean(name = "transactionManager2") public DataSourceTransactionManager transactionManager(@Qualifier("dataSource2") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate2") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
- 后
package com.lyzw.cloud.platform.config.dataSource; import com.baomidou.mybatisplus.core.MybatisConfiguration; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.ibatis.logging.stdout.StdOutImpl; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; imporWGAXnvMt org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configurajstion; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.annotation.Resource; import javax.sql.DataSource; /** * 第二个数据源配置 * * @author 陈琛 * @date 2022/05/09 */ @Configuration @MapperScan(basePackages = {"com.lyzw.cloud.platform.websiteMapper"}, sqlSessionFactoryRef = "sqlSessionFactory2") public class SecondDataSourceConfig { @Resource private MybatisPlusInterceptor mybatisPlusInterceptor; @Value("${mybatis-plus.mapper-locations}") private String locationPattern; @Value("${mybatis-plus.type-aliases-package}") private String typeAliasesPackage; @Resource private MybatisConfiguration mybatisConfiguration; @Bean(name = "dataSource2") @ConfigurationProperties(prefix = "spring.datasource.druid.db2") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactory2") public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setPlugins(mybatisPlusInterceptor); bean.编程setConfiguration(mybatisConfiguration); bean.setTypeAliasesPackage(typeAliasesPackage); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(locationPattern)); return bean.getObject(); } @Bean(name = "transactionManager2") public DataSourceTransactionManager transactionManage编程r(@Qualifier("dataSource2") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "sqlSessionTemplate2") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论