|
|
|
|
@ -17,13 +17,13 @@ import javax.annotation.Resource;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* 背景
|
|
|
|
|
* 现在的数据层的开发,大多会使用如MyBatis或JPA之类的开发工具。这些开发工具给我们的开发过程中带来了极大的便利。
|
|
|
|
|
* 但是在一些极端的场景下往往原生的jdbc方式操作数据库更灵活,性能更高。由于部分场景下MyBatis或JPA之类无法满足我的需求,所以我打算自己封装一套查数据库的工具类。
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* 我们会用到fastjson,druid,mysql所以pom.xml增加依赖如下:
|
|
|
|
|
*
|
|
|
|
|
* <dependency>
|
|
|
|
|
@ -41,7 +41,6 @@ import java.util.List;
|
|
|
|
|
* <groupId>mysql</groupId>
|
|
|
|
|
* <artifactId>mysql-connector-java</artifactId>
|
|
|
|
|
* </dependency>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -66,6 +65,7 @@ public class JdbcDemoController {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static DataSource ds = new DataSource();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
//配置数据源
|
|
|
|
|
ds.setId("1");
|
|
|
|
|
@ -88,31 +88,30 @@ public class JdbcDemoController {
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/api/list")
|
|
|
|
|
public ResponseDto queryList()
|
|
|
|
|
{
|
|
|
|
|
String sql = "select * from temperature where id = ?";
|
|
|
|
|
public ResponseDto queryList() {
|
|
|
|
|
|
|
|
|
|
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
|
|
|
|
|
String sql = "select * from temperature where id = ?";
|
|
|
|
|
List<Object> jdbcParamValues = new ArrayList<>();
|
|
|
|
|
for(int i=0;i<2362254;i++){
|
|
|
|
|
for (int i = 0; i < 2362254; i++) {
|
|
|
|
|
// try {
|
|
|
|
|
// Thread.sleep(5);
|
|
|
|
|
// } catch (InterruptedException e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// }
|
|
|
|
|
jdbcParamValues.add(i+1);
|
|
|
|
|
jdbcParamValues.add(i + 1);
|
|
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
ResponseDto responseDto = JdbcUtil.executeSql(ds,sql,jdbcParamValues);
|
|
|
|
|
ResponseDto responseDto = JdbcUtil.executeSql(ds, sql, jdbcParamValues);
|
|
|
|
|
String dbQueryResult = gson.toJson(responseDto);
|
|
|
|
|
log.info(dbQueryResult);
|
|
|
|
|
jdbcParamValues.clear();
|
|
|
|
|
|
|
|
|
|
ResponseTemperatureDto responseTemperatureDto=gson.fromJson(dbQueryResult,ResponseTemperatureDto.class);
|
|
|
|
|
if(responseTemperatureDto.isSuccess()&&responseTemperatureDto.getData()!=null&&responseTemperatureDto.getData().size()>0){
|
|
|
|
|
ResponseTemperatureDto responseTemperatureDto = gson.fromJson(dbQueryResult, ResponseTemperatureDto.class);
|
|
|
|
|
if (responseTemperatureDto.isSuccess() && responseTemperatureDto.getData() != null && responseTemperatureDto.getData().size() > 0) {
|
|
|
|
|
log.info(gson.toJson(responseTemperatureDto.getData().get(0)));
|
|
|
|
|
|
|
|
|
|
TemperatureDto dto = responseTemperatureDto.getData().get(0);
|
|
|
|
|
|
|
|
|
|
Temperature temperature = new Temperature();
|
|
|
|
|
temperature.setCreateDate(dto.getCreateDate());
|
|
|
|
|
temperature.setDataDate(dto.getDataDate());
|
|
|
|
|
@ -122,11 +121,57 @@ public class JdbcDemoController {
|
|
|
|
|
temperature.setHumidity(String.valueOf(dto.getHumidity()));
|
|
|
|
|
temperature.setTemperature(String.valueOf(dto.getTemperature()));
|
|
|
|
|
temperature.setLocationDesc(dto.getLocationDesc());
|
|
|
|
|
|
|
|
|
|
temperatureService.saveTemperature(temperature);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
return "数据库同步成功";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
future.thenApply(result -> {
|
|
|
|
|
System.out.println("Result: " + result);
|
|
|
|
|
return result;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return ResponseDto.successWithMsg("数据库正在同步...");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void completableFutureExample() {
|
|
|
|
|
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
|
|
|
|
|
// 模拟耗时操作
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt(); // 重置中断状态
|
|
|
|
|
}
|
|
|
|
|
return 123;
|
|
|
|
|
});
|
|
|
|
|
// 非阻塞等待结果,但不返回结果,如果要处理结果,可以使用thenApply等
|
|
|
|
|
future.thenAccept(result -> System.out.println("Result: " + result)).join();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询测试
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/api/getResult")
|
|
|
|
|
public String getResult() {
|
|
|
|
|
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
|
|
|
|
|
// 模拟耗时操作
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
System.out.println("异步处理完成");
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt(); // 重置中断状态
|
|
|
|
|
}
|
|
|
|
|
return "数据库同步成功";
|
|
|
|
|
});
|
|
|
|
|
//注意,如果需要异步返回结果,再做后续操作,需要加入join()方法等待异步计算结果后回调,不然异步没有处理完直接主线程结束
|
|
|
|
|
future.thenApply(result -> {
|
|
|
|
|
System.out.println("Result: " + result);
|
|
|
|
|
return result;
|
|
|
|
|
}).join();
|
|
|
|
|
System.out.println("数据库正在同步...");
|
|
|
|
|
return "数据库正在同步...";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|