mirror of https://github.com/elunez/eladmin
代码生成同步功能完成
parent
fac8e2f51e
commit
5f41318b9d
|
@ -42,14 +42,14 @@ import java.util.Map;
|
||||||
public class RedisConfig extends CachingConfigurerSupport {
|
public class RedisConfig extends CachingConfigurerSupport {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置 redis 数据默认过期时间,默认6小时
|
* 设置 redis 数据默认过期时间,默认2小时
|
||||||
* 设置@cacheable 序列化方式
|
* 设置@cacheable 序列化方式
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public RedisCacheConfiguration redisCacheConfiguration(){
|
public RedisCacheConfiguration redisCacheConfiguration(){
|
||||||
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
|
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
|
||||||
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
|
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
|
||||||
configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofHours(6));
|
configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofHours(2));
|
||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,8 +55,6 @@ public class GeneratorController {
|
||||||
@GetMapping(value = "/columns")
|
@GetMapping(value = "/columns")
|
||||||
public ResponseEntity<Object> getTables(@RequestParam String tableName){
|
public ResponseEntity<Object> getTables(@RequestParam String tableName){
|
||||||
List<ColumnInfo> columnInfos = generatorService.getColumns(tableName);
|
List<ColumnInfo> columnInfos = generatorService.getColumns(tableName);
|
||||||
// 异步同步表信息
|
|
||||||
generatorService.sync(columnInfos);
|
|
||||||
return new ResponseEntity<>(PageUtil.toPage(columnInfos,columnInfos.size()), HttpStatus.OK);
|
return new ResponseEntity<>(PageUtil.toPage(columnInfos,columnInfos.size()), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,6 +65,15 @@ public class GeneratorController {
|
||||||
return new ResponseEntity<>(HttpStatus.OK);
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation("同步字段数据")
|
||||||
|
@PostMapping(value = "sync")
|
||||||
|
public ResponseEntity<HttpStatus> sync(@RequestBody List<String> tables){
|
||||||
|
for (String table : tables) {
|
||||||
|
generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
@ApiOperation("生成代码")
|
@ApiOperation("生成代码")
|
||||||
@PostMapping(value = "/{tableName}/{type}")
|
@PostMapping(value = "/{tableName}/{type}")
|
||||||
public ResponseEntity<Object> generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
|
public ResponseEntity<Object> generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
|
||||||
|
|
|
@ -32,9 +32,10 @@ public interface GeneratorService {
|
||||||
/**
|
/**
|
||||||
* 同步表数据
|
* 同步表数据
|
||||||
* @param columnInfos /
|
* @param columnInfos /
|
||||||
|
* @param columnInfoList
|
||||||
*/
|
*/
|
||||||
@Async
|
@Async
|
||||||
void sync(List<ColumnInfo> columnInfos);
|
void sync(List<ColumnInfo> columnInfos, List<ColumnInfo> columnInfoList);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保持数据
|
* 保持数据
|
||||||
|
@ -71,4 +72,11 @@ public interface GeneratorService {
|
||||||
* @param response /
|
* @param response /
|
||||||
*/
|
*/
|
||||||
void download(GenConfig genConfig, List<ColumnInfo> columns, HttpServletRequest request, HttpServletResponse response);
|
void download(GenConfig genConfig, List<ColumnInfo> columns, HttpServletRequest request, HttpServletResponse response);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据库的表字段数据数据
|
||||||
|
* @param table /
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
List<ColumnInfo> query(String table);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Zheng Jie
|
* @author Zheng Jie
|
||||||
|
@ -86,6 +87,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public List<ColumnInfo> query(String tableName){
|
public List<ColumnInfo> query(String tableName){
|
||||||
// 使用预编译防止sql注入
|
// 使用预编译防止sql注入
|
||||||
String sql = "select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns " +
|
String sql = "select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns " +
|
||||||
|
@ -111,8 +113,35 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sync(List<ColumnInfo> columnInfos) {
|
public void sync(List<ColumnInfo> columnInfos, List<ColumnInfo> columnInfoList) {
|
||||||
|
// 第一种情况,数据库类字段改变或者新增字段
|
||||||
|
for (ColumnInfo columnInfo : columnInfoList) {
|
||||||
|
// 根据字段名称查找
|
||||||
|
List<ColumnInfo> columns = new ArrayList<ColumnInfo>(columnInfos.stream().filter(c-> c.getColumnName().equals(columnInfo.getColumnName())).collect(Collectors.toList()));
|
||||||
|
// 如果能找到,就修改部分可能被字段
|
||||||
|
if(CollectionUtil.isNotEmpty(columns)){
|
||||||
|
ColumnInfo column = columns.get(0);
|
||||||
|
column.setColumnType(columnInfo.getColumnType());
|
||||||
|
column.setExtra(columnInfo.getExtra());
|
||||||
|
column.setKeyType(columnInfo.getKeyType());
|
||||||
|
if(StringUtils.isBlank(column.getRemark())){
|
||||||
|
column.setRemark(columnInfo.getRemark());
|
||||||
|
}
|
||||||
|
columnInfoRepository.save(column);
|
||||||
|
} else {
|
||||||
|
// 如果找不到,则保存新字段信息
|
||||||
|
columnInfoRepository.save(columnInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 第二种情况,数据库字段删除了
|
||||||
|
for (ColumnInfo columnInfo : columnInfos) {
|
||||||
|
// 根据字段名称查找
|
||||||
|
List<ColumnInfo> columns = new ArrayList<ColumnInfo>(columnInfoList.stream().filter(c-> c.getColumnName().equals(columnInfo.getColumnName())).collect(Collectors.toList()));
|
||||||
|
// 如果找不到,就代表字段被删除了,则需要删除该字段
|
||||||
|
if(CollectionUtil.isEmpty(columns)){
|
||||||
|
columnInfoRepository.delete(columnInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue