mirror of
https://github.com/drtrang/druid-spring-boot.git
synced 2026-10-29 20:43:53 +00:00
jpa
This commit is contained in:
+3
-1
@@ -2,6 +2,7 @@ package com.github.trang.druid.example.jpa;
|
||||
|
||||
import com.github.trang.druid.example.jpa.repository.CityRepository;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -13,6 +14,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
* @author trang
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@Slf4j
|
||||
public class DruidJpaApplication implements CommandLineRunner {
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -28,7 +30,7 @@ public class DruidJpaApplication implements CommandLineRunner {
|
||||
Gson gson = new Gson();
|
||||
cityRepository.findAll().stream()
|
||||
.map(gson::toJson)
|
||||
.forEach(System.out::println);
|
||||
.forEach(log::info);
|
||||
}
|
||||
|
||||
}
|
||||
+5
-6
@@ -1,6 +1,7 @@
|
||||
package com.github.trang.druid.example.jpa.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
||||
@@ -17,14 +18,12 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
* @author trang
|
||||
*/
|
||||
@Slf4j
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class DynamicDataSource extends AbstractRoutingDataSource {
|
||||
|
||||
private AtomicInteger count = new AtomicInteger();
|
||||
@Getter private Map<String, DataSource> targetDataSources;
|
||||
|
||||
public DynamicDataSource(Map<String, DataSource> targetDataSources) {
|
||||
this.targetDataSources = targetDataSources;
|
||||
}
|
||||
private final AtomicInteger count = new AtomicInteger();
|
||||
private final Map<String, DataSource> targetDataSources;
|
||||
|
||||
/**
|
||||
* 基于轮询算法指定实际的数据源
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ public class CityController {
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public City get(@PathVariable Long id) {
|
||||
return cityRepository.findOne(id);
|
||||
return cityRepository.getOne(id);
|
||||
}
|
||||
|
||||
}
|
||||
+1
@@ -26,6 +26,7 @@ spring:
|
||||
password: 123456
|
||||
schema: classpath:import.sql
|
||||
druid:
|
||||
max-wait: 30000
|
||||
query-timeout: 10
|
||||
validation-query: SELECT 1
|
||||
use-global-data-source-stat: true
|
||||
|
||||
+6
@@ -2,7 +2,10 @@ package com.github.trang.druid.example.jpa.test.coveralls;
|
||||
|
||||
import com.github.trang.druid.example.jpa.DruidJpaApplication;
|
||||
import com.github.trang.druid.example.jpa.repository.CityRepository;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
@@ -16,6 +19,9 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
@SpringBootTest(classes = DruidJpaApplication.class)
|
||||
public abstract class BaseTest {
|
||||
|
||||
protected static final Logger log = LoggerFactory.getLogger(BaseTest.class.getSimpleName());
|
||||
protected Gson gson = new Gson();
|
||||
|
||||
@Autowired(required = false)
|
||||
protected CityRepository cityRepository;
|
||||
|
||||
|
||||
+4
-4
@@ -32,8 +32,8 @@ public class DynamicDataSourceTests extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void testMasterDataSource() {
|
||||
assertEquals(1, masterDataSource.getInitialSize());
|
||||
assertEquals(1, masterDataSource.getMinIdle());
|
||||
assertEquals(0, masterDataSource.getInitialSize());
|
||||
assertEquals(0, masterDataSource.getMinIdle());
|
||||
assertEquals(50, masterDataSource.getMaxActive());
|
||||
assertEquals(30000, masterDataSource.getMaxWait());
|
||||
assertEquals(60000, masterDataSource.getTimeBetweenEvictionRunsMillis());
|
||||
@@ -49,8 +49,8 @@ public class DynamicDataSourceTests extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void testSlaveDataSource() {
|
||||
assertEquals(1, slaveDataSource.getInitialSize());
|
||||
assertEquals(1, slaveDataSource.getMinIdle());
|
||||
assertEquals(0, slaveDataSource.getInitialSize());
|
||||
assertEquals(0, slaveDataSource.getMinIdle());
|
||||
assertEquals(25, slaveDataSource.getMaxActive());
|
||||
assertEquals(30000, slaveDataSource.getMaxWait());
|
||||
assertEquals(60000, slaveDataSource.getTimeBetweenEvictionRunsMillis());
|
||||
|
||||
+6
-3
@@ -5,6 +5,7 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* MyBatis 测试
|
||||
@@ -15,14 +16,16 @@ public class JpaTests extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void testOne() {
|
||||
City city = cityRepository.findOne(1L);
|
||||
System.out.println(city);
|
||||
Optional.ofNullable(cityRepository.getOne(1L))
|
||||
.ifPresent(gson::toJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
List<City> cities = cityRepository.findAll();
|
||||
System.out.println(cities);
|
||||
cities.stream()
|
||||
.map(gson::toJson)
|
||||
.forEach(log::info);
|
||||
Assert.assertEquals(5, cities.size());
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -20,9 +20,9 @@ public class SingleDataSourceTests extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void testSingleDataSource() {
|
||||
assertEquals(1, dataSource.getInitialSize());
|
||||
assertEquals(1, dataSource.getMinIdle());
|
||||
assertEquals(10, dataSource.getMaxActive());
|
||||
assertEquals(0, dataSource.getInitialSize());
|
||||
assertEquals(0, dataSource.getMinIdle());
|
||||
assertEquals(8, dataSource.getMaxActive());
|
||||
assertEquals(30000, dataSource.getMaxWait());
|
||||
assertEquals(60000, dataSource.getTimeBetweenEvictionRunsMillis());
|
||||
assertEquals(1800000, dataSource.getMinEvictableIdleTimeMillis());
|
||||
@@ -31,7 +31,7 @@ public class SingleDataSourceTests extends BaseTest {
|
||||
assertEquals(true, dataSource.isTestWhileIdle());
|
||||
assertEquals(false, dataSource.isTestOnBorrow());
|
||||
assertEquals(false, dataSource.isTestOnReturn());
|
||||
assertEquals(20, dataSource.getMaxPoolPreparedStatementPerConnectionSize());
|
||||
assertEquals(10, dataSource.getMaxPoolPreparedStatementPerConnectionSize());
|
||||
assertEquals(true, dataSource.isUseGlobalDataSourceStat());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user