package com.example.demo.test.data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "test")
public class Test {
@Id
private Integer id;
public Test(){
}
public Integer getId() {
return this.id;
}
public void setId(Integer id){
this.id = id;
}
}
package com.example.demo.other.data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "other")
public class Other {
@Id
private Integer id;
public Integer getId() {
return this.id;
}
public void setId(Integer id){
this.id = id;
}
}
package com.example.demo.test.data;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TestRepository extends JpaRepository<Test, Integer> {
}
package com.example.demo.other.data;
import org.springframework.data.jpa.repository.JpaRepository;
public interface OtherRepository extends JpaRepository<Other, Integer> {
}
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.EntityManagerFactory;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = {"com.example.demo.test.data"}
)
public class TestDataConfig {
@Autowired
private JpaProperties jpaProperties;
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.demo.test.data")
.properties(jpaProperties.getHibernateProperties(dataSource))
.persistenceUnit("test")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.persistence.EntityManagerFactory;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "otherEntityManagerFactory",
transactionManagerRef = "otherTransactionManager",
basePackages = {"com.example.demo.other.data"}
)
public class OtherDataConfig {
@Autowired
private JpaProperties jpaProperties;
@Bean(name = "otherDataSource")
@ConfigurationProperties(prefix = "other.datasource")
public DataSource otherDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "otherEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean otherEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("otherDataSource") DataSource otherDataSource) {
return builder
.dataSource(otherDataSource)
.packages("com.example.demo.other.data")
.properties(jpaProperties.getHibernateProperties(otherDataSource))
.persistenceUnit("other")
.build();
}
@Bean(name = "otherTransactionManager")
public PlatformTransactionManager otherTransactionManager(
@Qualifier("otherEntityManagerFactory") EntityManagerFactory otherEntityManagerFactory) {
return new JpaTransactionManager(otherEntityManagerFactory);
}
}
package com.example.demo.service;
import com.example.demo.other.data.Other;
import com.example.demo.other.data.OtherRepository;
import com.example.demo.test.data.Test;
import com.example.demo.test.data.TestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class TestService {
@Autowired
private TestRepository testRepository;
@Autowired
private OtherRepository otherRepository;
@Value("${name:World}")
private String name;
public String getHelloMessage() {
Test test = new Test();
test.setId(1);
test = testRepository.save(test);
Other other = new Other();
other.setId(2);
other = otherRepository.save(other);
return "Hello " + this.name + " : test's value = " + test.getId() + " , other's value = " + other.getId();
}
}
spring: datasource: url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false testWhileIdle: true validationQuery: SELECT 1 from dual username: test password: 11111111 driverClassName: com.mysql.jdbc.Driver jpa: database: MYSQL show-sql: true hibernate: show-sql: true ddl-auto: create naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy properties: hibernate.dialect: org.hibernate.dialect.MySQL5Dialect other: datasource: url: jdbc:mysql://localhost:3306/other?characterEncoding=utf-8&useSSL=false testWhileIdle: true validationQuery: SELECT 1 username: other password: 11111111 driverClassName: com.mysql.jdbc.Driver jpa: database: MYSQL show-sql: true hibernate: show-sql: true ddl-auto: create naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy properties: hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
@ConfigurationProperties(prefix = "other.datasource")
CREATE USER 'test'@'localhost' IDENTIFIED BY '11111111'; GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost'; CREATE USER 'other'@'localhost' IDENTIFIED BY '11111111'; GRANT ALL PRIVILEGES ON *.* TO 'other'@'localhost'; create database test; create database other;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有