SpringBoot 配置多数据源,注意 Mapper XML 的目录创建 Invalid bound statement (not found) idea 中创建目录时,不能直接 mappet.hospital
yml配置
spring: datasource: # MySQL java的new Date()时间插入数据库时差差8个小时问题 GMT%2b8 # driver-class-name: com.mysql.jdbc.Driver #com.mysql.jdbc.Driver和mysql-connector-java 5一起用。 iron: driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.cj.jdbc.Driver和mysql-connector-java 6 一起用。 url: jdbc:mysql://192.168.0.10:3306/iron_test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL username: root password: 123 validation-query: select 'x' #用来检测连接是否有效的sql 必须是一个查询语句( mysql中为 select 'x' oracle中为 select 1 from dual) mapper-locations: classpath*:mapper/*.xml #将MyBatis Mapper xml 放到 jar 包外面,发布时改成 file:mapper/*.xml his: #Oracle driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@192.168.0.11:1521/iron_s #jdbc:oracle:thin:@//<host>:<port>/<SERVICE_NAME> dbc:oracle:thin:@<host>:<port>:<SID> username: iron password: 123 validation-query: select 1 from dual #用来检测连接是否有效的sql 必须是一个查询语句( mysql中为 select 'x' oracle中为 select 1 from dual) mapper-locations: classpath*:mapper/hospital/*.xml #将MyBatis Mapper xml 放到 jar 包外面,发布时改成 file:mapper/hospital/*.xml # SQL SERVER 数据源基本配置 # driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver # url: jdbc:sqlserver://192.168.0.12:1433;DatabaseName=irondb # username: sa # password: 123
IronDruidConfig
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * Iron 自己的数据库 */ @Configuration @MapperScan(basePackages = IronDruidConfig.PACKAGE, sqlSessionFactoryRef = "ironSqlSessionFactory") public class IronDruidConfig { /** * 配置多数据源 关键就在这里 这里配置了不同的数据源扫描不同mapper */ static final String PACKAGE = "com.iron.his.mapper"; /** * 连接数据库信息 这个其实更好的是用配置中心完成 */ @Value("${spring.datasource.iron.url}") private String url; @Value("${spring.datasource.iron.username}") private String username; @Value("${spring.datasource.iron.password}") private String password; @Value("${spring.datasource.iron.driver-class-name}") private String driverClassName; /** * 不同的数据源扫描不同mapper, 放到配置文件里修改。因为发布时,需要改成file:mapper/*.xml 方便后期 SQL调整 */ @Value("${spring.datasource.iron.mapper-locations}") private String mapperLocations; @Bean public ServletRegistrationBean druidServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); // IP白名单 servletRegistrationBean.addInitParameter("allow", "");//默认就是允许所有访问 // IP黑名单(共同存在时,deny优先于allow) //servletRegistrationBean.addInitParameter("deny", "192.168.1.100"); //控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "123456"); //是否能够重置数据 禁用HTML页面上的“Reset All”功能 servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } // 注解@Primary表示是主数据源 @Bean("ironDataSource") @Primary public DataSource ironDataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); return dataSource; } @Bean(name = "ironTransactionManager") @Primary public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(ironDataSource()); } @Bean(name = "ironSqlSessionFactory") @Primary public SqlSessionFactory masterSqlSessionFactory(@Qualifier("ironDataSource") DataSource masterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); return sessionFactory.getObject(); } }
HisDruidConfig
import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * his 的数据库 多数据源时使用 */ @Configuration @MapperScan(basePackages = HisDruidConfig.PACKAGE, sqlSessionFactoryRef = "hisSqlSessionFactory") public class HisDruidConfig { /** * 配置多数据源 关键就在这里 这里配置了不同的数据源扫描不同mapper */ static final String PACKAGE = "com.iron.his.hospital.mapper"; /** * 连接数据库信息 这个其实更好的是用配置中心完成 */ @Value("${spring.datasource.his.driver-class-name}") private String driverClassName; @Value("${spring.datasource.his.url}") private String url; @Value("${spring.datasource.his.username}") private String username; @Value("${spring.datasource.his.password}") private String password; /** * 不同的数据源扫描不同mapper, 放到配置文件里修改。因为发布时,需要改成file:mapper/*.xml 方便后期 SQL调整 */ @Value("${spring.datasource.his.mapper-locations}") private String mapperLocations; // 注解@Primary表示是主数据源 @Bean("hisDataSource") public DataSource hisDataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); return dataSource; } @Bean(name = "hisTransactionManager") public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(hisDataSource()); } @Bean(name = "hisSqlSessionFactory") public SqlSessionFactory masterSqlSessionFactory(@Qualifier("hisDataSource") DataSource masterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations)); return sessionFactory.getObject(); } }