Spring Boot学习进阶笔记(四)-多数据源配置(JdbcTemplate、Spring-data-jpa)

发布时间:2017-7-9 7:17:01编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Spring Boot学习进阶笔记(四)-多数据源配置(JdbcTemplate、Spring-data-jpa) ",主要涉及到Spring Boot学习进阶笔记(四)-多数据源配置(JdbcTemplate、Spring-data-jpa) 方面的内容,对于Spring Boot学习进阶笔记(四)-多数据源配置(JdbcTemplate、Spring-data-jpa) 感兴趣的同学可以参考一下。

在实际开发过程中,往往我们需要链接多个数据库进行操作,所以多数据源的配置就在所难免了。

一、JdbcTemplate支持:

spring boot配置多数据源比较简单

1)修改配置文件“application.properties”

spring.datasource.primary.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false

spring.datasource.primary.username=root

spring.datasource.primary.password=123456

spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver

 

 

spring.datasource.secondary.url=jdbc:mysql://192.168.2.111:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false

spring.datasource.secondary.username=root

spring.datasource.secondary.password=123456

spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver

2)添加数据类“DataSourceConfig.java”

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 DataSourceConfig {

 

  @Bean(name = "primaryDataSource")

    @Qualifier("primaryDataSource")

    @ConfigurationProperties(prefix="spring.datasource.primary")

    public DataSource primaryDataSource() {

        return DataSourceBuilder.create().build();

    }

 

    @Bean(name = "secondaryDataSource")

    @Qualifier("secondaryDataSource")

    @Primary

    @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);

    }

 

}

3)在service调用的时候根据注解添加不同的JdbcTemplate

@Autowired

    @Qualifier("primaryJdbcTemplate")

    protected JdbcTemplate jdbcTemplate;

 

@Autowired

    @Qualifier("secondaryJdbcTemplate")

    protected JdbcTemplate jdbcTemplate2;

 

@Override

    public Integer getUserCount() {

        return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);

    }

    

    @Override

    public Integer getUserCount2() {

        return jdbcTemplate2.queryForObject("select count(1) from USER", Integer.class);

    }

 

 

二、Spring-data-jpa支持:

1)创建数据源配置类“PrimaryConfig.java”和“SecondConfig.java”,以“”为例子

 

 


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

 

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

 

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = { "com.zh.SpringBootDemo.domain.p" }) // 设置Repository所在位置
public class PrimaryConfig {

 

@Autowired
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;

 

@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}

 

@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
return builder.dataSource(primaryDataSource).properties(getVendorProperties(primaryDataSource)).packages("com.zh.SpringBootDemo.domain.p") // 设置实体类所在位置
.persistenceUnit("primaryPersistenceUnit").build();
}

 

@Autowired
private JpaProperties jpaProperties;

 

private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}

 

@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}

 

}

 

 

2)创建实体类domain和对应的respository

 

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

 

public interface UserRepository extends JpaRepository<User, Long> {

 

User findByName(String name);

 

User findByNameAndAge(String name, Integer age);

 

@Query("from User u where u.name=:name")
User findUser(@Param("name") String name);

 


}

 

 

3)根据注解调用对应的respository即可

 



上一篇:PILLOW图片中加入中文 曲线救国Opencv
下一篇:进入创业公司的前与后(二)

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款