一、Spring Boot配置双数据源报错
在实际应用中,经常会遇到需要配置多个数据源的情况。在Spring Boot中,配置多个数据源的方式不同于传统的Spring MVC,因此很容易出现配置错误的情况。下面介绍一些出错原因和解决方法。
1、错误原因:没有指定主数据源
@Configuration
@MapperScan(basePackages = "com.example.mapper1", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class DataSource1Config {
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource1")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource1());
return bean.getObject();
}
@Bean
@Primary
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory1());
}
}
@Configuration
@MapperScan(basePackages = "com.example.mapper2", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class DataSource2Config {
@Bean
@ConfigurationProperties(prefix = "spring.datasource2")
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource2());
return bean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory2());
}
}
在这个例子中,我们定义了两个数据源dataSource1和dataSource2,并指定了相应的SqlSessionFactory和SqlSessionTemplate。在这个过程中,我们使用了Primary注解来指定主数据源。如果没有指定主数据源,会抛出如下错误信息:
No qualifying bean of type 'com.example.mapper1.AppMapper' available: expected at least 1 bean which qualifies as autowire candidate.
2、错误原因:没有指定MapperScan
在配置多数据源时,我们需要指定每个数据源所使用的MapperScan,如下所示:
@Configuration
@MapperScan(basePackages = "com.example.mapper1", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class DataSource1Config {
...
}如果没有指定MapperScan,会提示找不到该Mapper的信息。因此,我们需要注意这一点。
二、Spring Boot配置JNDI数据源
除了使用配置文件配置数据源之外,我们还可以使用JNDI数据源,在Spring Boot中,使用JNDI数据源的方式与传统的Spring MVC相同。下面是一个使用JNDI数据源的例子:
@Configuration
public class JndiDataSourceConfig {
@Bean(destroyMethod="")
public DataSource dataSource() throws Exception {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/myAppDataSource");
}
}三、Spring Boot配置双数据源查询报错
在使用双数据源查询时,需要注意事项:
1、在DAO层中指定数据源
在使用MyBatis进行多数据源操作时,默认情况下会使用主数据源对应的SqlSessionTemplate,因此需要在DAO层中指定所使用的数据源,如下所示:
@Repository
public interface AppMapper {
@Select("SELECT * FROM app WHERE id = #{id}")
@DataSource("dataSource2")
App findById(@Param("id") Long id);
}2、在配置文件中指定MapperScan
核心代码如下:
@MapperScan(basePackages = "com.example.mapper1", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class DataSource1Config {
...
}3、在具体查询语句中指定表名
由于不同的数据源可能访问了不同的表,因此需要在具体的查询语句中指定表名,如下所示:
@Select("SELECT * FROM app_2 WHERE id = #{id}")
App findById(@Param("id") Long id);四、Spring Boot连接池配置
连接池是指程序和数据库之间的连接插座,每个连接池都是一个单独的连接对象,与其他连接池之间独立,相互不影响。在Spring Boot中,常用的连接池有Apache DBCP和HikariCP。
1、使用Apache DBCP连接池
在使用Apache DBCP连接池时,需要添加以下依赖:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>9.0.19</version>
</dependency>同时,在配置文件中添加以下代码:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root # DBCP连接池配置 spring.datasource.tomcat.initial-size=5 spring.datasource.tomcat.max-idle=10 spring.datasource.tomcat.min-idle=5 spring.datasource.tomcat.max-wait=20000 spring.datasource.tomcat.test-on-borrow=true spring.datasource.tomcat.test-on-return=true spring.datasource.tomcat.validation-query=SELECT 1
2、使用HikariCP连接池
在使用HikariCP连接池时,需要添加以下依赖:
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.1</version>
</dependency>同时,在配置文件中添加以下代码:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root # HikariCP连接池配置 spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.maximum-pool-size=10 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=MyHikariCP spring.datasource.hikari.connection-timeout=20000
五、完整代码示例
完整的Spring Boot配置双数据源的代码示例:
1、数据源配置
@Configuration
@MapperScan(basePackages = "com.example.mapper1", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class DataSource1Config {
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource1")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource1());
return bean.getObject();
}
@Bean
@Primary
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory1());
}
}
@Configuration
@MapperScan(basePackages = "com.example.mapper2", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class DataSource2Config {
@Bean
@ConfigurationProperties(prefix = "spring.datasource2")
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource2());
return bean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
return new SqlSessionTemplate(sqlSessionFactory2());
}
}2、查询语句
@Repository
public interface AppMapper {
@Select("SELECT * FROM app WHERE id = #{id}")
@DataSource("dataSource2")
App findById(@Param("id") Long id);
}3、连接池配置
使用HikariCP连接池的示例代码如下:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root # HikariCP连接池配置 spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.maximum-pool-size=10 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=MyHikariCP spring.datasource.hikari.connection-timeout=20000
原创文章,作者:GRJUI,如若转载,请注明出处:https://www.506064.com/n/334530.html
微信扫一扫
支付宝扫一扫