注:多数据源同时也会引起 jdbc-type-for-null的配置失效,导致(无效的列类型: 1111,oracle数据库需配置JdbcType.NULL, 默认是Other),所以需要在配置Configuration的同时增加
sqlSessionFactory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
单数据源情况下可做如下配置解决:
【Spring Boot|MyBatis Plus】解决Oracle报错:无效的列类型1111_mybatisplus localdate 无效列-CSDN博客https://blog.csdn.net/kedaji/article/details/116151174mybatis整合oracle时对于空值报错 Error setting null for parameter # with JdbcType OTHER解决-CSDN博客
https://blog.csdn.net/whandgdh/article/details/99663656
简单介绍下多数据源实现,其实就是基于AbstractRoutingDataSource配置多个数据源,后续采取AOP+ThreadLocal方式动态控制每次SQL执行时的Datasource
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDatabaseType();
}
}
Springboot-整合多数据源配置 & AbstractRoutingDataSource详解,分析多数据源切换原理_springboot 2.1.8 版本 与 abstractroutingdatasource-CSDN博客https://blog.csdn.net/z69183787/article/details/119593499 但这样配置后,就会发现就算在logback xml文件中设置 druid sql 输出是debug模式,
<!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
<!-- 设置字符集 -->
<charset>UTF-8</charset>
</encoder>
</appender>
<logger name="druid.sql.Statement" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="DEBUG_FILE" />
</logger>
也无法看到sql日志,解决方案如下:
@Bean(name = "mysqlSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/dws/*Mapper.xml"));//扫描指定目录的xml
//此处创建一个Configuration 注意包不要引错了
org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
//设置jdbcType null
configuration.setJdbcTypeForNull(JdbcType.NULL);
//配置日志实现 输出到控制台
configuration.setLogImpl(StdOutImpl.class);
//此处可以添加其他mybatis配置 例如转驼峰命名
configuration.setMapUnderscoreToCamelCase(true);
//bena工厂装载上面配置的Configuration
bean.setConfiguration(configuration);
return bean.getObject();
}
注:需要打印到文件File中 setLogImpl 设置为 org.apache.ibatis.logging.slf4j.Slf4jImpl
简而言之:因为自定义了sqlSessionFactory,所以需要重新设置 mybatis的configuration,因为非默认的SqlSessionFactory无法自动读取 yaml中的 mybatis-configuration配置项。
可参考:
springboot 多数据源怎么配置在控制台的sql打印日志_sqlsessionfactory 配置sql打印-CSDN博客
SpringBoot配置多数据源后sql日志不打印解决方案_log-impl配置了不打印日志-CSDN博客
关于springboot+mybatis 配置多数据源打印sql语句失效的问题_creating a new sqlsession 不打印-CSDN博客