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目录下,程序也能正常运行。 但与配置所表达完全不同了啊。
指定数据库错了???