提交 4bc4738e authored 作者: ww1xhqc's avatar ww1xhqc

[数据模型] 新增了批量查询的方法,修改了缓存配置

上级 90346b4f
...@@ -105,8 +105,16 @@ ...@@ -105,8 +105,16 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- caffeine缓存 -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.tykj.workflowcore.model_layer.config; package com.tykj.workflowcore.model_layer.config;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager; import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -10,6 +13,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -10,6 +13,7 @@ import org.springframework.context.annotation.Configuration;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.TimeUnit;
/** /**
...@@ -23,9 +27,45 @@ import java.util.Collections; ...@@ -23,9 +27,45 @@ import java.util.Collections;
public class MyCacheConfig { public class MyCacheConfig {
@Bean @Bean
public CacheManager cacheManager() { public CaffeineCacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager(); //api + cache spring cache + hashmap
cacheManager.setCaches(Collections.singletonList(new ConcurrentMapCache("tableInfos"))); //mycode + redis
//spring cache + caffeine
// SimpleCacheManager cacheManager = new SimpleCacheManager();
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
Caffeine caffeine = Caffeine.newBuilder()
//cache的初始容量值
.initialCapacity(100)
//maximumSize用来控制cache的最大缓存数量,maximumSize和maximumWeight(最大权重)不可以同时使用,
.maximumSize(1000)
//最后一次写入或者访问后过久过期
.expireAfterAccess(500, TimeUnit.SECONDS)
//创建或更新之后多久刷新,需要设置cacheLoader
.refreshAfterWrite(10, TimeUnit.SECONDS);
cacheManager.setCaffeine(caffeine);
cacheManager.setCacheLoader(cacheLoader());
// cacheManager.setCacheNames();//根据名字可以创建多个cache,但是多个cache使用相同的策略
cacheManager.setAllowNullValues(false);//是否允许值为空
// cacheManager.setCaches(Collections.singletonList(new ConcurrentMapCache("tableInfos")));
return cacheManager; return cacheManager;
} }
@Bean
public CacheLoader<Object, Object> cacheLoader() {
return new CacheLoader<Object, Object>() {
@Override
public Object load(Object key) throws Exception {
return null;
}
// 重写这个方法将oldValue值返回回去,进而刷新缓存
@Override
public Object reload(Object key, Object oldValue) throws Exception {
System.out.println("--refresh--:" + key);
return oldValue;
}
};
}
} }
...@@ -11,7 +11,6 @@ import io.swagger.annotations.ApiOperation; ...@@ -11,7 +11,6 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -85,7 +84,7 @@ public class ModelController { ...@@ -85,7 +84,7 @@ public class ModelController {
} }
} }
modelService.newTable(tableVO); modelService.newTable(tableVO);
return ResultUtil.success("", "新建成功"); return ResultUtil.success("", "新建成功!");
} }
...@@ -123,7 +122,7 @@ public class ModelController { ...@@ -123,7 +122,7 @@ public class ModelController {
} catch (SQLException throwables) { } catch (SQLException throwables) {
throwables.printStackTrace(); throwables.printStackTrace();
} }
return ResultUtil.success(null, "获取失败"); return ResultUtil.success(null, "获取失败!");
} }
/** /**
* @param tableName * @param tableName
...@@ -149,7 +148,7 @@ public class ModelController { ...@@ -149,7 +148,7 @@ public class ModelController {
if (i==1){ if (i==1){
return ResultUtil.success("","修改成功!"); return ResultUtil.success("","修改成功!");
} }
return ResultUtil.success("未找到该表信息或者业务类型不支持修改!"); return ResultUtil.success("业务类型不支持修改!");
} }
@ApiOperation("删除操作") @ApiOperation("删除操作")
@DeleteMapping("/delete") @DeleteMapping("/delete")
...@@ -159,6 +158,6 @@ public class ModelController { ...@@ -159,6 +158,6 @@ public class ModelController {
if (i==1){ if (i==1){
return ResultUtil.success("", "删除成功!"); return ResultUtil.success("", "删除成功!");
} }
return ResultUtil.success("未找到该id的表信息或者业务类型不支持删除!"); return ResultUtil.success("业务类型不支持删除!");
} }
} }
package com.tykj.workflowcore.model_layer.entity.vo;
import com.tykj.workflowcore.model_layer.entity.ColumnInfo;
import com.tykj.workflowcore.model_layer.entity.TableInfo;
import lombok.Data;
import java.util.List;
/**
* @ClassName TableAndColumnInfoVO
* @Description TODO
* @Author WWW
* @Date 2021/3/29 9:59
* @Version 1.0
*/
@Data
public class TableAndColumnInfoVO {
private List<TableInfo> tableInfos;
private List<ColumnInfo> columnInfos;
}
...@@ -108,4 +108,12 @@ public interface ModelService { ...@@ -108,4 +108,12 @@ public interface ModelService {
int delTable(DelTableVO delTableVO); int delTable(DelTableVO delTableVO);
/**
* 根据id批量查询所有tableInfo和ColumnInfo
* @param ids
* @return
*/
TableAndColumnInfoVO getTableInfoAndColumnInfoByBatch(Integer[] ids);
} }
...@@ -571,4 +571,20 @@ public class ModelImpl implements ModelService { ...@@ -571,4 +571,20 @@ public class ModelImpl implements ModelService {
return 0; return 0;
} }
@Override
public TableAndColumnInfoVO getTableInfoAndColumnInfoByBatch(Integer[] ids) {
TableAndColumnInfoVO tableAndColumnInfoVO=new TableAndColumnInfoVO();
PredicateBuilder<TableInfo> builder1 = Specifications.and();
builder1.in(ids!=null,"id",ids);
List<TableInfo> allTableInfos = tableInfoDao.findAll(builder1.build());
PredicateBuilder<ColumnInfo> builder2 = Specifications.and();
builder2.in(ids!=null,"dbId",ids);
List<ColumnInfo> allColumnInfos = columnInfoDao.findAll(builder2.build());
tableAndColumnInfoVO.setTableInfos(allTableInfos);
tableAndColumnInfoVO.setColumnInfos(allColumnInfos);
return tableAndColumnInfoVO;
}
} }
...@@ -3,6 +3,7 @@ package com.tykj.workflowcore; ...@@ -3,6 +3,7 @@ package com.tykj.workflowcore;
import com.tykj.workflowcore.model_layer.controller.ModelController; import com.tykj.workflowcore.model_layer.controller.ModelController;
import com.tykj.workflowcore.model_layer.dao.TableInfoDao; import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
import com.tykj.workflowcore.model_layer.entity.vo.TableAndColumnInfoVO;
import com.tykj.workflowcore.model_layer.myEnum.ModelType; import com.tykj.workflowcore.model_layer.myEnum.ModelType;
import com.tykj.workflowcore.model_layer.service.ModelService; import com.tykj.workflowcore.model_layer.service.ModelService;
import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Before;
...@@ -63,6 +64,7 @@ class WorkflowCoreApplicationTests { ...@@ -63,6 +64,7 @@ class WorkflowCoreApplicationTests {
/** /**
* 测试表的条件查询 * 测试表的条件查询
*
* @throws Exception * @throws Exception
*/ */
@Test @Test
...@@ -87,6 +89,7 @@ class WorkflowCoreApplicationTests { ...@@ -87,6 +89,7 @@ class WorkflowCoreApplicationTests {
/** /**
* 测试列条件查询 * 测试列条件查询
*
* @throws Exception * @throws Exception
*/ */
@Test @Test
...@@ -113,6 +116,7 @@ class WorkflowCoreApplicationTests { ...@@ -113,6 +116,7 @@ class WorkflowCoreApplicationTests {
/** /**
* 测试新增对象 * 测试新增对象
*
* @throws Exception * @throws Exception
*/ */
@Test @Test
...@@ -144,11 +148,12 @@ class WorkflowCoreApplicationTests { ...@@ -144,11 +148,12 @@ class WorkflowCoreApplicationTests {
/** /**
* 测试插入数据 * 测试插入数据
*
* @throws Exception * @throws Exception
*/ */
@Test @Test
public void insertValuesByTableName() throws Exception { public void insertValuesByTableName() throws Exception {
String values_json="{\"techer\":{\"name\": \"zhangsan\",\"age\" :\"20\"}}"; String values_json = "{\"techer\":{\"name\": \"zhangsan\",\"age\" :\"20\"}}";
request = post("/model/insertValues/") request = post("/model/insertValues/")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
...@@ -165,12 +170,13 @@ class WorkflowCoreApplicationTests { ...@@ -165,12 +170,13 @@ class WorkflowCoreApplicationTests {
/** /**
* 测试查询全部 * 测试查询全部
*
* @throws Exception * @throws Exception
*/ */
@Test @Test
public void findAllByTableName() throws Exception { public void findAllByTableName() throws Exception {
request=get("/model/getAll/") request = get("/model/getAll/")
.param("tableName","techer") .param("tableName", "techer")
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE); .accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request) mockMvc.perform(request)
...@@ -186,10 +192,10 @@ class WorkflowCoreApplicationTests { ...@@ -186,10 +192,10 @@ class WorkflowCoreApplicationTests {
*/ */
@Test @Test
public void testDelTable() throws Exception { public void testDelTable() throws Exception {
String delete_JSON="{\n" + String delete_JSON = "{\n" +
"\"dbName\":\"testentity\"\n" + "\"dbName\":\"testentity\"\n" +
"}"; "}";
request=delete("/model/delete/") request = delete("/model/delete/")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(delete_JSON) .content(delete_JSON)
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE); .accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
...@@ -198,4 +204,13 @@ class WorkflowCoreApplicationTests { ...@@ -198,4 +204,13 @@ class WorkflowCoreApplicationTests {
.andExpect(content().string(not(""))) .andExpect(content().string(not("")))
.andDo(print()); .andDo(print());
} }
@Test
public void testAll() {
Integer[] ids = {2, 3, 22};
TableAndColumnInfoVO all = modelService.getTableInfoAndColumnInfoByBatch(ids);
System.out.println(all);
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论