springboot 双 sqlite 数据源配置
1.在配置资源文件中 配置如下
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #SQLLITE #SQLLITE spring.datasource.sqlite.driver-class-name=org.sqlite.JDBC spring.datasource.sqlite.url=jdbc:sqlite:db/personInfo.db #SQLLITE spring.datasource.sqlite2.driver-class-name=org.sqlite.JDBC spring.datasource.sqlite2.url=jdbc:sqlite:db/bug.db
2.创建配置类
import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; @Configuration public class BugcollectConfig { @Bean(name = "sqliteDataSource") @Primary @Qualifier("sqliteDataSource") @ConfigurationProperties(prefix="spring.datasource.sqlite") public DataSource sqliteDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "sqliteDataSource2") @Qualifier("sqliteDataSource2") @ConfigurationProperties(prefix="spring.datasource.sqlite2") public DataSource sqliteDataSource2() { return DataSourceBuilder.create().build(); } @Bean(name = "sqliteJdbcTemplate") public JdbcTemplate sqliteJdbcTemplate(@Qualifier("sqliteDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "sqliteJdbcTemplate2") public JdbcTemplate sqliteJdbcTemplate2(@Qualifier("sqliteDataSource2") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
3.dao层实现类中配置
@Repository public class BugcollectDaoImp implements BugcollectDao { protected UUIDHexGenerator uuid = UUIDHexGenerator.getInstance(); @Autowired @Qualifier("sqliteJdbcTemplate") private JdbcTemplate jdbcTemplate; @Autowired @Qualifier("sqliteJdbcTemplate2") private JdbcTemplate jdbcTemplate2;
相关推荐
yupi0 2020-10-10
spring 2020-08-18
编程点滴 2020-07-29
幸运小侯子 2020-07-05
itjavashuai 2020-07-04
qingjiuquan 2020-06-29
shushan 2020-06-25
小鱿鱼 2020-06-22
咻pur慢 2020-06-18
melonjj 2020-06-17
qingjiuquan 2020-06-13
neweastsun 2020-06-05
小鱿鱼 2020-06-05
mxcsdn 2020-05-31
吾日五省我身 2020-05-27
牧场SZShepherd 2020-05-27
sweetgirl0 2020-05-14