首页 新闻 搜索 专区 学院

springboot mybatis多数据源配置问题

0
悬赏园豆:50 [待解决问题]

yml配置:
spring:
profiles: dev
datasource:
druid:
core:
url: jdbc:mysql:///kc_core?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
schedule:
url: jdbc:mysql:///kc_schedule?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource

mybatis:
configuration:
map-underscore-to-camel-case: true
jdbc-type-for-null: NULL

代码:

@Configuration
@EnableConfigurationProperties({MybatisProperties.class})
public class MybatisConfig {

private final static String coreScannerBasePackages = "com.happy.batch.server.dao.core";
private final static String scheduleScannerBasePackages = "com.happy.batch.server.dao.schedule";

private final static String coreSessionFactoryName = "coreSqlSessionFactory";
private final static String scheduleSessionFactoryName = "scheduleSqlSessionFactory";


@Bean(name = "coreDataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.core")
@Primary
public DataSource coreDataSource() {
    return DruidDataSourceBuilder.create().build();
}

@Bean(name = coreSessionFactoryName)
@Primary
public SqlSessionFactory coreSqlSessionFactory(@Qualifier("coreDataSource") DataSource coreDataSource, MybatisProperties mybatisProperties) throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(coreDataSource);
    sessionFactory.setConfiguration(mybatisProperties.getConfiguration());
    return sessionFactory.getObject();
}

@Bean(name = "coreSqlSessionTemplate")
@Primary
public SqlSessionTemplate coreSqlSessionTemplate(@Qualifier(coreSessionFactoryName) SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}

//=================================
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.schedule")
public DataSource dataSource() {
    return DruidDataSourceBuilder.create().build();
}

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

@Bean(name = "scheduleSqlSessionTemplate")
public SqlSessionTemplate scheduleSqlSessionTemplate(@Qualifier(scheduleSessionFactoryName) SqlSessionFactory scheduleSqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(scheduleSqlSessionFactory);
}

//================
@Bean
public MapperScannerConfigurer mapperScannerCore() {
    MapperScannerConfigurer configurer = new MapperScannerConfigurer();
    configurer.setBasePackage(coreScannerBasePackages);
    configurer.setSqlSessionFactoryBeanName(coreSessionFactoryName);
    return configurer;
}

@Bean
public MapperScannerConfigurer mapperScannerSchedule() {
    MapperScannerConfigurer configurer = new MapperScannerConfigurer();
    configurer.setBasePackage(scheduleScannerBasePackages);
    configurer.setSqlSessionFactoryBeanName(scheduleSessionFactoryName);
    return configurer;
}

}

core包下有public interface AssetDao
抛异常:
Caused by: java.sql.SQLSyntaxErrorException: Table 'kc_schedule.asset' doesn't exist

asset表在kc_core库。

AssetDao在core包下,按配置应该走kc_core.asset这样查询。

请问为什么配置没起作用???

问题补充:

如果我把assetDao迁移到schedule目录下,程序也能正常运行。 但与配置所表达完全不同了啊。

落孤秋叶的主页 落孤秋叶 | 初学一级 | 园豆:106
提问于:2020-03-26 17:31
< >
分享
所有回答(2)
0

指定数据库错了???

Mr_伍先生 | 园豆:6 (初学一级) | 2020-03-26 23:19
0

已经自己解决,主要是MapperProperties的配置影响。 具体解决:https://www.cnblogs.com/song27/p/12595603.html

落孤秋叶 | 园豆:106 (初学一级) | 2020-03-30 11:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册