启动类
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Springboot配置文件
数据源配置
spring:
datasource:
ds1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://ip:port/table?useSSL=false&useUnicode=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: 用户名
password: 密码
ds2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://ip:port/table?useSSL=false&useUnicode=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: 用户名
password: 密码
mybatis全局配置
mybatis:
type-aliases-package: com.nit310.store.entity
configuration:
log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl
map-underscore-to-camel-case: true
配置类
数据源配置
@Configuration
@MapperScan(basePackages = "com.nit310.store.mapper.ds1", sqlSessionFactoryRef = "ds1SqlSessionFactory")
public class DataSourceConfig1 {
@Bean(name = "ds1DataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.ds1")
public DataSource getDateSource1()
{
return DataSourceBuilder.create().build();
}
@Bean(name = "ds1SqlSessionFactory")
@Primary
public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource datasource)
throws Exception
{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
return bean.getObject();
}
@Bean("ds1SqlSessionTemplate")
@Primary
public SqlSessionTemplate ds1SqlSessionTemplate(
@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sessionFactory)
{
return new SqlSessionTemplate(sessionFactory);
}
}
mybatis全局配置
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/ds1/*.xml"));
bean.setTypeAliasesPackage("com.nit310.store.entity");
bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
bean.getObject().getConfiguration().setLogImpl(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
参考文章