最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問題

 更新時(shí)間:2023年12月29日 09:31:26   作者:陳賝  
這篇文章主要介紹了自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

建議使用官方多數(shù)據(jù)源配置

問題:配置雙數(shù)據(jù)源后不打印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

我的問題在配置雙數(shù)據(jù)源后,走默認(rèn)的mybatis創(chuàng)建工廠,沒有各自配置

解決

對比下之前的和之后的

注意注入的

private MybatisPlusInterceptor mybatisPlusInterceptor;
  • 主數(shù)據(jù)源配置前
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.setMapperLocations(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;

/**
 * 主數(shù)據(jù)源配置
 *
 * @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);
    }
}
  • 次數(shù)據(jù)源配置前
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;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 第二個(gè)數(shù)據(jù)源配置
 *
 * @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 transactionManager(@Qualifier("dataSource2") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplate2")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 分析并發(fā)編程之LongAdder原理

    分析并發(fā)編程之LongAdder原理

    LongAdder類是JDK1.8新增的一個(gè)原子性操作類。AtomicLong通過CAS算法提供了非阻塞的原子性操作,相比受用阻塞算法的同步器來說性能已經(jīng)很好了,但是JDK開發(fā)組并不滿足于此,因?yàn)榉浅8悴l(fā)的請求下AtomicLong的性能是不能讓人接受的
    2021-06-06
  • springboot移除nacos?yml無法加載的解決過程

    springboot移除nacos?yml無法加載的解決過程

    這篇文章主要介紹了springboot移除nacos?yml無法加載的解決過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • Java中的運(yùn)算符你知道多少

    Java中的運(yùn)算符你知道多少

    這篇文章主要為大家詳細(xì)介紹了Java中的運(yùn)算符,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • springboot?jpa?實(shí)現(xiàn)返回結(jié)果自定義查詢

    springboot?jpa?實(shí)現(xiàn)返回結(jié)果自定義查詢

    這篇文章主要介紹了springboot?jpa?實(shí)現(xiàn)返回結(jié)果自定義查詢方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 基于SpringBoot+Tess4j實(shí)現(xiàn)圖像文字識別功能

    基于SpringBoot+Tess4j實(shí)現(xiàn)圖像文字識別功能

    文章介紹了Tesseract?OCR技術(shù)及其Java封裝庫Tess4j,并詳細(xì)描述了如何在SpringBoot項(xiàng)目中集成和使用Tess4j進(jìn)行圖像識別,文章涵蓋了下載字體庫、配置SpringBoot項(xiàng)目、編寫測試代碼及圖片識別測試等步驟,并提供了中文、英文及中英混合識別的實(shí)戰(zhàn)示例,需要的朋友可以參考下
    2026-04-04
  • 如何將文件流轉(zhuǎn)換成byte[]數(shù)組

    如何將文件流轉(zhuǎn)換成byte[]數(shù)組

    這篇文章主要介紹了如何將文件流轉(zhuǎn)換成byte[]數(shù)組,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • SpringBoot 集成 MyBatis/MyBatis-Plus使用示例教程

    SpringBoot 集成 MyBatis/MyBatis-Plus使用示例教程

    本文詳細(xì)介紹了如何在Spring Boot項(xiàng)目中集成MyBatis和MyBatis-Plus,并通過Lombok簡化代碼,通過本文的學(xué)習(xí),開發(fā)者可以快速掌握Spring Boot集成MyBatis和MyBatis-Plus的完整流程,并利用Lombok提高開發(fā)效率,感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • Java8 HashMap遍歷方式性能探討

    Java8 HashMap遍歷方式性能探討

    JDK8之前,可以使用keySet或者entrySet來遍歷HashMap,JDK8中引入了map.foreach來進(jìn)行遍歷
    2021-09-09
  • Java數(shù)組常用排序算法實(shí)例小結(jié)

    Java數(shù)組常用排序算法實(shí)例小結(jié)

    這篇文章主要介紹了Java數(shù)組常用排序算法,結(jié)合實(shí)例形式總結(jié)分析了java數(shù)組常用的4種排序算法,包括冒泡排序、數(shù)組遞增排序、快速排序及選擇排序,需要的朋友可以參考下
    2017-12-12
  • Spring?Security中的CORS詳解

    Spring?Security中的CORS詳解

    CORS(Cross-Origin?Resource?Sharing)是一種允許不同源之間進(jìn)行資源共享的W3C標(biāo)準(zhǔn),它通過在服務(wù)器端設(shè)置特定的HTTP響應(yīng)頭,實(shí)現(xiàn)了跨域請求的功能,這種機(jī)制要求瀏覽器和服務(wù)器的支持,本文給大家介紹Spring?Security中的CORS,感興趣的朋友一起看看吧
    2024-10-10

最新評論

安远县| 平和县| 天全县| 交城县| 资源县| 四平市| 平山县| 双牌县| 广东省| 阿合奇县| 报价| 修武县| 彰武县| 凉城县| 神池县| 铁力市| 山丹县| 荆门市| 丹东市| 成武县| 长宁县| 嘉兴市| 延长县| 新安县| 罗平县| 新平| 大邑县| 兴安县| 广东省| 巴彦淖尔市| 武强县| 武山县| 黄骅市| 玛纳斯县| 都江堰市| 丹阳市| 龙游县| 大同市| 蓝田县| 进贤县| 清水县|