SpringBoot与JPA多数据源
SpringBoot与JPA多数据源
Demo
项目结构
项目源码
package com.supremepole.b03springbootmultijpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class B03SpringBootMultiJpaApplication {
public static void main(String[] args) {
SpringApplication.run(B03SpringBootMultiJpaApplication.class, args);
}
}
package com.supremepole.b03springbootmultijpa;
import com.supremepole.b03springbootmultijpa.model.Website;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author CodeCoderCoding
*/
@RestController
public class WebsiteController {
@Autowired
WebsiteService websiteService;
@GetMapping("/add-website")
public void addWebsite(){
Website website=new Website();
website.setId(1);
website.setName("supremepole");
website.setUrl("https://cs.supremepole.com");
websiteService.addWebsite(website);
Website website2=new Website();
website2.setId(2);
website2.setName("supremepole interview");
website2.setUrl("https://interview.supremepole.com");
websiteService.addWebsite(website2);
}
@GetMapping("/get-all-website")
public List<Website> getWebsite(){
return websiteService.getAllWebsites();
}
@GetMapping("/get-max-id-website")
public Website getMaxIdWebsite(){
return websiteService.getMaxIdWebsite();
}
}
package com.supremepole.b03springbootmultijpa;
import com.supremepole.b03springbootmultijpa.dao1.WebsiteDao;
import com.supremepole.b03springbootmultijpa.model.Website;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author CodeCoderCoding
*/
@Service
public class WebsiteService {
@Autowired
WebsiteDao websiteDao;
public void addWebsite(Website website) {
websiteDao.save(website);
}
public List<Website> getAllWebsites() {
return websiteDao.findAll();
}
public Website getMaxIdWebsite(){
return websiteDao.getMaxIdWebsite();
}
}
package com.supremepole.b03springbootmultijpa.dao1;
import com.supremepole.b03springbootmultijpa.model.Website;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
/**
* @author CodeCoderCoding
*/
public interface WebsiteDao extends JpaRepository<Website,Integer>{
@Query(value = "select * from website where id=(select max(id) from website)",nativeQuery = true)
Website getMaxIdWebsite();
}
package com.supremepole.b03springbootmultijpa.dao2;
import com.supremepole.b03springbootmultijpa.model.Website;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author CodeCoderCoding
*/
public interface WebsiteDao2 extends JpaRepository<Website,Integer>{
}
package com.supremepole.b03springbootmultijpa.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* @author CodeCoderCoding
*/
@Entity(name="website")
public class Website {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String url;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "id="+id+" ,name"+name+" ,url"+url;
}
}
package com.supremepole.b03springbootmultijpa.config;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
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 javax.sql.DataSource;
/**
* @author CodeCoderCoding
*/
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.one")
@Primary
DataSource dsOne() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.two")
DataSource dsTwo() {
return DruidDataSourceBuilder.create().build();
}
}
package com.supremepole.b03springbootmultijpa.config;
import org.springframework.beans.factory.annotation.Autowired;
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.annotation.Resource;
import javax.sql.DataSource;
/**
* @author CodeCoderCoding
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.supremepole.b03springbootmultijpa.dao1",
entityManagerFactoryRef = "entityManagerFactoryBeanOne",
transactionManagerRef = "platformTransactionManagerOne")
public class JpaConfigOne {
@Resource(name = "dsOne")
DataSource dsOne;
@Autowired
JpaProperties jpaProperties;
@Bean
@Primary
LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanOne(
EntityManagerFactoryBuilder builder) {
return builder.dataSource(dsOne)
.properties(jpaProperties.getProperties())
.packages("com.supremepole.b03springbootmultijpa.model")
.persistenceUnit("pu1")
.build();
}
@Bean
PlatformTransactionManager platformTransactionManagerOne(
EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean factoryOne = entityManagerFactoryBeanOne(builder);
return new JpaTransactionManager(factoryOne.getObject());
}
}
package com.supremepole.b03springbootmultijpa.config;
import org.springframework.beans.factory.annotation.Autowired;
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.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.annotation.Resource;
import javax.sql.DataSource;
/**
* @author CodeCoderCoding
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.supremepole.b03springbootmultijpa.dao2",
entityManagerFactoryRef = "entityManagerFactoryBeanTwo",
transactionManagerRef = "platformTransactionManagerTwo")
public class JpaConfigTwo {
@Resource(name = "dsTwo")
DataSource dsTwo;
@Autowired
JpaProperties jpaProperties;
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanTwo(
EntityManagerFactoryBuilder builder) {
return builder.dataSource(dsTwo)
.properties(jpaProperties.getProperties())
.packages("com.supremepole.b03springbootmultijpa.model")
.persistenceUnit("pu2")
.build();
}
@Bean
PlatformTransactionManager platformTransactionManagerTwo(
EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean factoryTwo = entityManagerFactoryBeanTwo(builder);
return new JpaTransactionManager(factoryTwo.getObject());
}
}
server.port=8081
# data source 1
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.one.url=jdbc:mysql://localhost:3306/spring-demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
spring.datasource.one.username=root
spring.datasource.one.password=123456
# data source 2
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.two.url=jdbc:mysql://localhost:3306/spring-demo2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
spring.datasource.two.username=root
spring.datasource.two.password=123456
spring.jpa.properties.database=mysql
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.show-sql= true