美文网首页
SpringBoot之JPA多数据源

SpringBoot之JPA多数据源

作者: 不浪漫的阳光 | 来源:发表于2019-01-22 10:59 被阅读0次

    依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    

    单数据源

    properties:

    spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    

    直接template注入就能使用

    @Repository
    public class UserRepositoryImpl implements UserRepository {
        @Autowired
        private JdbcTemplate jdbcTemplate;
    }
    

    多数据源

    properties:

    spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    spring.datasource.primary.username=root
    spring.datasource.primary.password=root
    spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
    
    spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    spring.datasource.secondary.username=root
    spring.datasource.secondary.password=root
    spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
    

    注意点:第一行的url必须改为jdbc-url,即spring.datasource.*.jdbc-url

    配置类:

    @Configuration
    public class DataSourceConfig {
        @Primary
        @Bean(name = "primaryDataSource")
        @Qualifier("primaryDataSource")
        @ConfigurationProperties(prefix="spring.datasource.primary")
        public DataSource primaryDataSource() {
                return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "secondaryDataSource")
        @Qualifier("secondaryDataSource")
        @ConfigurationProperties(prefix="spring.datasource.secondary")
        public DataSource secondaryDataSource() {
            return DataSourceBuilder.create().build();      
        }
    
        @Bean(name="primaryJdbcTemplate")
        public JdbcTemplate primaryJdbcTemplate (
            @Qualifier("primaryDataSource")  DataSource dataSource ) {
            return new JdbcTemplate(dataSource);
        }
    
        @Bean(name="secondaryJdbcTemplate")
        public JdbcTemplate  secondaryJdbcTemplate(
                @Qualifier("secondaryDataSource") DataSource dataSource) {
            return new JdbcTemplate(dataSource);
        }
    }
    

    为repository配上默认的template

    @Repository
    public class UserRepositoryImpl implements UserRepository {
        @Autowired
        private JdbcTemplate primaryJdbcTemplate;
    
        @Override
        public int save(User user,JdbcTemplate jdbcTemplate) {
            if(jdbcTemplate == null){
                jdbcTemplate= primaryJdbcTemplate;
            }
            return jdbcTemplate.update("INSERT INTO users(name, password, age) values(?, ?, ?)",
                  user.getName(), user.getPassword(), user.getAge());
        }
    }
    

    使用时传入template做参数

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class UserRepositoryTests {
        @Autowired
        private UserRepository userRepository;
        @Autowired
        private JdbcTemplate primaryJdbcTemplate;
        @Autowired
        private JdbcTemplate secondaryJdbcTemplate;
    
        @Test
        public void testSave() {
            User user =new User("smile","123456",30);
            userRepository.save(user,primaryJdbcTemplate);
            userRepository.save(user,secondaryJdbcTemplate);
        }
    }
    

    转自CSDN图文课--作者:纯洁的微笑

    相关文章

      网友评论

          本文标题:SpringBoot之JPA多数据源

          本文链接:https://www.haomeiwen.com/subject/hdhljqtx.html