多数据源读库打印sql

本文介绍了在本地开发时如何在Mybatis的只读数据库配置中设置打印SQL的日志,提供了两种方案:一种通过`logImpl`设置,另一种使用`MybatisProperties`。
摘要由CSDN通过智能技术生成

在本地进行开发的时候,需要打印读库的sql,但是常规的配置是无法打印的,需要在读库的配置中进行设置。

第一种方案

通过设置logImpl

@Configuration
@MapperScan(basePackages = "com.xxx.readonly.dao", sqlSessionFactoryRef = "readonlySqlSessionFactory")
public class ReadDbConfig {
    @Bean(name = "readDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.readonly")
    public DataSource createDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "readonlySqlSessionFactory")
    public SqlSessionFactory createSqlSessionFactory(@Qualifier("readDataSource") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:readmapper/*.xml"));
		// 打印sql
         org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
         configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
         sessionFactory.setConfiguration(configuration);

        return sessionFactory.getObject();
    }

    @Bean(name = "readTransactionManager")
    public DataSourceTransactionManager createTransactionManager(@Qualifier("readDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

第二种方案

添加 MybatisProperties

@Configuration
@MapperScan(basePackages = "com.xxx.readonly.dao", sqlSessionFactoryRef = "readonlySqlSessionFactory")
public class ReadDbConfig {
    @Bean(name = "readDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.readonly")
    public DataSource createDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "readonlySqlSessionFactory")
    public SqlSessionFactory createSqlSessionFactory(@Qualifier("readDataSource") DataSource masterDataSource, MybatisProperties mybatisPropertie)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:readmapper/*.xml"));

//         org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
//         configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
//         sessionFactory.setConfiguration(configuration);
        sessionFactory.setConfiguration(mybatisPropertie.getConfiguration());

        return sessionFactory.getObject();
    }

    @Bean(name = "readTransactionManager")
    public DataSourceTransactionManager createTransactionManager(@Qualifier("readDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值