项目中有2个数据源,1个是单独的数据库A,1个是sharding配置的分表数据源B;
本来是想保存数据到A数据库,结果是执行的sharding的分表数据库B,其实是因为 SqlSessionFactory 错误导致的。
之前的配置文件信息,这是mybatis的配置信息
@Bean(name = "dictSqlSessionFactory")
public SqlSessionFactory rdsSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(rdsDataSource());
sqlSessionFactoryBean.setMapperLocations(new
PathMatchingResourcePatternResolver().getResources(mybatisMapperLocations));
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
GlobalConfig globalConfig=new GlobalConfig();
mybatisConfiguration.setMapUnderscoreToCamelCase(true);
mybatisConfiguration.setCacheEnabled(false);
mybatisConfiguration.setCallSettersOnNulls(true);
mybatisConfiguration.setJdbcTypeForNull( JdbcType.NULL);
sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
return sqlSessionFactoryBean.getObject();
}
修改成mybatis-plus的配置信息
@Bean(name = "dictSqlSessionFactory")
public SqlSessionFactory rdsSqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(rdsDataSource());
sqlSessionFactoryBean.setMapperLocations(new
PathMatchingResourcePatternResolver().getResources("classpath:mapper/dict/*.xml"));
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
GlobalConfig globalConfig=new GlobalConfig();
mybatisConfiguration.setMapUnderscoreToCamelCase(true);
mybatisConfiguration.setCacheEnabled(false);
mybatisConfiguration.setCallSettersOnNulls(true);
mybatisConfiguration.setJdbcTypeForNull(JdbcType.NULL);
sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
return sqlSessionFactoryBean.getObject();
}
修改成这个即可成功