配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| spring: datasource:
boot: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/boot username: qinnan password: 123456 type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true
filters: stat maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
app: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/app username: qinnan password: 123456 type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true
filters: stat maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
jpa: database-platform: org.hibernate.dialect.MySQL5Dialect show-sql: true hibernate: ddl-auto: update # create 每次都重新创建表,update,表若存在则不重建
|
多数据源配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| package com.qn.config.DataSourceConfig;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary;
import java.util.Arrays; import java.util.HashMap; import java.util.Map;
@SpringBootConfiguration public class DruidConfig {
@Bean(name ="bootDataSource") @Primary @Qualifier("bootDataSource") @ConfigurationProperties(prefix = "spring.datasource.boot") public DruidDataSource bootDataSource() { return new DruidDataSource(); }
@Bean(name ="appDataSource") @Qualifier("appDataSource") @ConfigurationProperties(prefix = "spring.datasource.app") public DruidDataSource appDataSource() { return new DruidDataSource(); }
@Bean public ServletRegistrationBean statViewServlet() { ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet()); Map<String, String> initParams = new HashMap<>(); initParams.put("loginUsername", "admin"); initParams.put("loginPassword", "123"); initParams.put("allow", "127.0.0.1"); bean.setInitParameters(initParams); bean.setUrlMappings(Arrays.asList("/druid/*")); return bean; }
@Bean public FilterRegistrationBean webStatFilter() { FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>(new WebStatFilter()); HashMap<String, String> initParams = new HashMap<>(); initParams.put("exclusions", "/css,/druid/*"); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
|
boot数据源配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package com.qn.config.DataSourceConfig.jpa;
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties; import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings; 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.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;
@SpringBootConfiguration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactoryBoot", transactionManagerRef = "transactionManagerBoot", basePackages = {"com.qn.dao.boot"}) public class BootJpaConfig { @Autowired private JpaProperties jpaProperties;
@Autowired @Qualifier("bootDataSource") private DruidDataSource bootDataSource;
@Autowired private HibernateProperties hibernateProperties;
@Primary @Bean(name = "entityManagerBoot") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); }
@Primary @Bean(name = "entityManagerFactoryBoot") public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) { return builder .dataSource(bootDataSource) .properties(hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings())) .packages("com.qn.eneity") .persistenceUnit("primaryPersistenceUnit") .build(); }
@Primary @Bean(name = "transactionManagerBoot") public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); } }
|
app数据源配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package com.qn.config.DataSourceConfig.jpa;
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties; import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings; import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; 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;
@SpringBootConfiguration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactoryApp", transactionManagerRef = "transactionManagerApp", basePackages = {"com.qn.dao.app"}) public class AppJpaConfig { @Autowired private JpaProperties jpaProperties;
@Autowired @Qualifier("appDataSource") private DruidDataSource appDataSource;
@Autowired private HibernateProperties hibernateProperties;
@Bean(name = "entityManagerApp") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); }
@Bean(name = "entityManagerFactoryApp") public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) { return builder .dataSource(appDataSource) .properties(hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings())) .packages("com.qn.eneity") .persistenceUnit("primaryPersistenceUnit") .build(); }
@Bean(name = "transactionManagerApp") public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); } }
|
主从库dao类
1 2 3 4 5 6 7 8 9 10
| package com.qn.dao.app;
import com.qn.eneity.Person; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;
@Repository public interface AppPersonRepository extends JpaRepository<Person, Integer> {
}
|
1 2 3 4 5 6 7 8 9 10
| package com.qn.dao.boot;
import com.qn.eneity.Account; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;
@Repository public interface BootAccountRepository extends JpaRepository<Account,Integer> {
}
|
entity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package com.qn.eneity;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;
@Entity @Table(name = "account") public class Account { @Id @Column(nullable = false) private int id; private String name; private double money;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getMoney() { return money; }
public void setMoney(double money) { this.money = money; }
@Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package com.qn.eneity;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;
@Entity @Table(name = "person") public class Person { @Id @Column(nullable = false) private int user_id;
private String user_name;
private String password;
private String phone;
@Override public String toString() { return "Person{" + "user_id=" + user_id + ", user_name='" + user_name + '\'' + ", password='" + password + '\'' + ", phone='" + phone + '\'' + '}'; }
public int getUser_id() { return user_id; }
public void setUser_id(int user_id) { this.user_id = user_id; }
public String getUser_name() { return user_name; }
public void setUser_name(String user_name) { this.user_name = user_name; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.qn.dao.app;
import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest @RunWith(SpringRunner.class) class AppPersonRepositoryTest { @Autowired AppPersonRepository appPersonRepository;
@Test void getAllPersons() { appPersonRepository.findAll().forEach( person -> System.out.println(person.toString()) ); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.qn.dao.boot;
import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest @RunWith(SpringRunner.class) class BootAccountRepositoryTest {
@Autowired BootAccountRepository bootAccountRepository;
@Test void getAllUsers() { bootAccountRepository.findAll().forEach( user -> System.out.println(user.toString()) ); } }
|