springboot 双 sqlite 数据源配置

时间:2022-03-20 03:55:54

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;