Skip to content

Commit

Permalink
代码生成同步功能完成
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Dec 21, 2019
1 parent fac8e2f commit 5f41318
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
public class RedisConfig extends CachingConfigurerSupport {

/**
* 设置 redis 数据默认过期时间,默认6小时
* 设置 redis 数据默认过期时间,默认2小时
* 设置@cacheable 序列化方式
*/
@Bean
public RedisCacheConfiguration redisCacheConfiguration(){
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public ResponseEntity<Object> getTables(@RequestParam(defaultValue = "") String
@GetMapping(value = "/columns")
public ResponseEntity<Object> getTables(@RequestParam String tableName){
List<ColumnInfo> columnInfos = generatorService.getColumns(tableName);
// 异步同步表信息
generatorService.sync(columnInfos);
return new ResponseEntity<>(PageUtil.toPage(columnInfos,columnInfos.size()), HttpStatus.OK);
}

Expand All @@ -67,6 +65,15 @@ public ResponseEntity<HttpStatus> save(@RequestBody List<ColumnInfo> columnInfos
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("生成代码")
@PostMapping(value = "/{tableName}/{type}")
public ResponseEntity<Object> generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public interface GeneratorService {
/**
* 同步表数据
* @param columnInfos /
* @param columnInfoList
*/
@Async
void sync(List<ColumnInfo> columnInfos);
void sync(List<ColumnInfo> columnInfos, List<ColumnInfo> columnInfoList);

/**
* 保持数据
Expand Down Expand Up @@ -71,4 +72,11 @@ public interface GeneratorService {
* @param response /
*/
void download(GenConfig genConfig, List<ColumnInfo> columns, HttpServletRequest request, HttpServletResponse response);

/**
* 查询数据库的表字段数据数据
* @param table /
* @return /
*/
List<ColumnInfo> query(String table);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Zheng Jie
Expand Down Expand Up @@ -86,6 +87,7 @@ public List<ColumnInfo> getColumns(String tableName) {
}
}

@Override
public List<ColumnInfo> query(String tableName){
// 使用预编译防止sql注入
String sql = "select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns " +
Expand All @@ -111,8 +113,35 @@ public List<ColumnInfo> query(String tableName){
}

@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
Expand Down

0 comments on commit 5f41318

Please sign in to comment.