About springboot + mybatis configure multiple data sources to print problem sql statement failure

When we springboot integration mybatis, you can print sql statement

When we springboot integration mybatis, in the case of a single data source, you can

#打印sql语句
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

But when we configure multiple data sources, using mybatis.configuration.log-impl not print sql statement

Large print information to the console

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c204c23] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1363932663 wrapping com.mysql.cj.jdbc.ConnectionImpl@3f4319cd] will not be managed by Spring
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c204c23]

The reason: because (mybatis.configuration.log-impl) tk.mybatis SqlSessionFactory when there is no defined time defined SqlSessionFactory; and when we configure multiple data sources, redefined SqlSessionFactory; therefore, if you want to print the sql statement, then we need to SqlSessionFactory configure multiple data sources when added MybatisProperties;

When you configure the database to solve

MybatisProperties simply add a configuration in a configuration of multiple data sources can

    @Bean(name = "SecondarySessionFactory")
    public SqlSessionFactory SecondarySessionFactory(@Qualifier("SecondaryDataSource") DataSource dataSource , MybatisProperties mybatisProperties) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setConfiguration(mybatisProperties.getConfiguration());
        return bean.getObject();
    }

The results (perfect solution)

When connecting the main database test http: // localhost: sql statement 8080 / test / getListOfTuser print

Here Insert Picture Description

When connecting the main database cbh http: // localhost: 8080 / cbh / getListOfTuserByCBH print sql statement

Here Insert Picture Description

Published 15 original articles · won praise 4 · Views 1451

Guess you like

Origin blog.csdn.net/qq_40791070/article/details/100665502