第一步:配置文件
注意:druid的配置应该再后调一格
conn1:
datasource:
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://ip:3306/table1?useAffectedRows=true&useUnicode=true&characterEncoding=UTF-8
username: xxxxx
password: xxxxx
conn2:
datasource:
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://ip:3306/table2?useAffectedRows=true&useUnicode=true&characterEncoding=UTF-8
username: xxxx
password: xxxx
filter:
config:
enabled: true
initialSize: 5 #初始建立连接数量
minIdle: 5 #最小连接数量
maxActive: 20 #最大连接数量
maxWait: 10000 #获取连接最大等待时间,毫秒
testOnBorrow: true #申请连接时检测连接是否有效
testOnReturn: false #归还连接时检测连接是否有效
timeBetweenEvictionRunsMillis: 60000 #配置间隔检测连接是否有效的时间(单位是毫秒)
minEvictableIdleTimeMillis: 300000 #连接在连接池的最小生存时间(毫秒)
stat-view-servlet: #开启web监控页面
url-pattern: /druid/* #web访问路径
allow: localhost #允许访问的ip,不配置默认所有地址都可以访问
login-username: admin #访问需要的用户名
login-password: 123654 #访问需要的密码
enabled: true
web-stat-filter: #配置web过滤器
url-pattern: /* #过滤url
#exclusions: /druid/* #排除过滤
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
enabled: true
profile-enable: true #监控单个url调用的sql列表
#connection-properties: config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKaWNv8bdl0VzRZDhspOazg+EtCqR3+2LnlllxSG2QtALDDSTeuHuf+jSyrNAXcwMV0suIvWLlkjBhWzC0Gab6sCAwEAAQ==
filters: stat,wall
第二部:编码载入配置
第一数据源载入
@Configuration
@MapperScan(basePackages = {"com.tgxun.service.impl","com.tgxun.mapper"},sqlSessionFactoryRef = "conn1SqlSessionFactory")
public class DataConn1Config {
@Value("${spring.conn1.datasource.druid.username}")
private String username;
@Value("${spring.conn1.datasource.druid.password}")
private String password;
@Value("${spring.conn1.datasource.druid.driver-class-name}")
private String driverClass;
@Value("${spring.conn1.datasource.druid.url}")
private String url;
@Bean
@ConfigurationProperties("mybatis.configuration")
public org.apache.ibatis.session.Configuration configuration(){
return new org.apache.ibatis.session.Configuration();
}
@Bean(name="conn1DataSource")
@Primary
public DataSource buildSource(){
DruidDataSource build = new DruidDataSource();
build.setUrl(url);
build.setDriverClassName(driverClass);
build.setUsername(username);
build.setPassword(password);
return build;
}
@Bean(name="conn1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("conn1DataSource") DataSource dataSource, org.apache.ibatis.session.Configuration configuration)throws Exception{
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//配置方式一
bean.setConfiguration(configuration);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml"));
Log.info("第一数据库连接完成");
return bean.getObject();
}
@Bean(name = "conn1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("conn1DataSource")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="conn1SqlSessionTemplate")
@Primary
public SqlSessionTemplate conn1SqlSessionTemplate(@Qualifier("conn1SqlSessionFactory")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第一数据源载入
@Configuration
@MapperScan(basePackages = {"com.tgxun.service.impl2","com.tgxun.mapper2"},sqlSessionFactoryRef = "conn2SqlSessionFactory")
@Slf4j
public class DataConn2Config {
@Value("${spring.conn2.datasource.druid.username}")
private String username;
@Value("${spring.conn2.datasource.druid.password}")
private String password;
@Value("${spring.conn2.datasource.druid.driver-class-name}")
private String driverClass;
@Value("${spring.conn2.datasource.druid.url}")
private String url;
@Bean(name="conn2DataSource")
public DataSource buildSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setPassword(password);
dataSource.setUsername(username);
dataSource.setUrl(url);
dataSource.setDriverClassName(driverClass);
return dataSource;
}
@Bean(name="conn2SqlSessionFactory")
public SqlSessionFactory connSqlSessionFactory(@Qualifier("conn2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//配置方式二
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/mapper2/*.xml"));
log.info("第二数据库连接完成");
return sqlSessionFactoryBean.getObject();
}
@Bean(name="conn2TransactionManager")
public DataSourceTransactionManager connTransactionManager(@Qualifier("conn2DataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="conn2SqlSessionTemplate")
public SqlSessionTemplate connSqlSessionTemplate(@Qualifier("conn2SqlSessionFactory") SqlSessionFactory sessionFactory){
return new SqlSessionTemplate(sessionFactory);
}
}
如上 对应建service+mapper+xml即可