您当前的位置: 首页 >  spring

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot 配置多数据源(JDBCTemplate版)

梁云亮 发布时间:2020-06-12 23:39:02 ,浏览量:2

第一步:创建SpringBoot项目 Maven依赖

    org.springframework.boot
    spring-boot-starter-data-jdbc



    mysql
    mysql-connector-java
    runtime


    org.projectlombok
    lombok
    true


    org.springframework.boot
    spring-boot-starter-test
    test
    
        
            org.junit.vintage
            junit-vintage-engine
        
    

application.yml
spring:
  datasource:
    test1:
      driverClassName: com.mysql.cj.jdbc.Driver
      # url必须使用jdbc-url的名称
      jdbc-url: jdbc:mysql://localhost:3306/db_test?useSSL=false&serverTimezone=UTC&user=root&password=&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true
      password: root
      username: root
    test2:
      driverClassName: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&user=root&password=&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true
      password: root
      username: root
第二步:配置数据源
@Configuration
public class DataSourceConfig {
    @Primary  //表示当某一个类存在多个实例时,优先使用哪个实例
    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix="spring.datasource.test1")
    public DataSource test1DataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "test1JdbcTemplate")
    public JdbcTemplate test1JdbcTemplate(@Qualifier("test1DataSource") DataSource dataSource){
        return  new JdbcTemplate(dataSource);
    }
    /
    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix="spring.datasource.test2")
    public DataSource test2DataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "test2JdbcTemplate")
    public JdbcTemplate test2JdbcTemplate(@Qualifier("test2DataSource") DataSource dataSource){
        return  new JdbcTemplate(dataSource);
    }
}
第三步:创建实体类
@Data
public class Dept {
    private Integer deptno;
    private String dname;
    private String loc;
}
第四步:创建Dao DeptDao
public interface DeptDao {
    List selectAll();
}
DeptDaoImpl_Test1
@Repository
public class DeptDaoImpl_Test1 implements DeptDao {
    @Resource(name = "test1JdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    @Override
    public List selectAll() {
        String sql = "select * from tb_dept";
        List res = jdbcTemplate
.query(sql, new BeanPropertyRowMapper(Dept.class)); 
        return res;
    }
}
DeptDaoImpl_Test2
@Repository("deptDaoImplTest2")
public class DeptDaoImpl_Test2 implements DeptDao {
    @Resource(name = "test2JdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    @Override
    public List selectAll() {
        String sql = "select * from tb_dept";
        List res = jdbcTemplate
.query(sql, new BeanPropertyRowMapper(Dept.class));
        return res;
    }
}
第五步:测试代码 DeptDaoImpl_Test1Test
@SpringBootTest
class DeptDaoImpl_Test1Test {
    @Resource(name = "deptDaoImpl_Test1")
    private DeptDao deptDao;
    @Test
    void selectAll() {
        deptDao.selectAll().forEach(System.out::println);
    }
}
DeptDaoImpl_Test2Test
@SpringBootTest
class DeptDaoImpl_Test2Test {
    @Resource(name = "deptDaoImplTest2")
    private DeptDao deptDao;
    @Test
    void selectAll() {
        deptDao.selectAll().forEach(System.out::println);
    }
}
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0543s