master
parent
450bca1ad8
commit
26f4c57e01
|
|
@ -1,6 +1,7 @@
|
||||||
package com.njzscloud.common.gen;
|
package com.njzscloud.common.gen;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateTime;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.core.util.ZipUtil;
|
import cn.hutool.core.util.ZipUtil;
|
||||||
|
|
@ -19,6 +20,7 @@ import org.springframework.web.bind.annotation.*;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
@ -60,8 +62,8 @@ public class TplController {
|
||||||
return R.success();
|
return R.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/generate")
|
@PostMapping("/download")
|
||||||
public void generate(@RequestParam("tplNames") String[] tplNames,
|
public void download(@RequestParam("tplNames") String[] tplNames,
|
||||||
@RequestParam("tableName") String tableName,
|
@RequestParam("tableName") String tableName,
|
||||||
@RequestBody(required = false) Map<String, Object> data,
|
@RequestBody(required = false) Map<String, Object> data,
|
||||||
HttpServletResponse response) {
|
HttpServletResponse response) {
|
||||||
|
|
@ -89,6 +91,50 @@ public class TplController {
|
||||||
ServletUtil.download(response, out.toByteArray(), Mime.ZIP, DateTime.now() + ".zip");
|
ServletUtil.download(response, out.toByteArray(), Mime.ZIP, DateTime.now() + ".zip");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/preview")
|
||||||
|
public R<?> preview(@RequestParam("tplNames") String[] tplNames,
|
||||||
|
@RequestParam("tableName") String tableName,
|
||||||
|
@RequestBody(required = false) Map<String, Object> data) {
|
||||||
|
Map<String, Map<String, String>> map = new HashMap<>();
|
||||||
|
for (String tplName : tplNames) {
|
||||||
|
Tuple3<String, String, String> res = Generator.generate(tplName, tableName, data);
|
||||||
|
String res0 = res.get_0();
|
||||||
|
String res1 = res.get_1();
|
||||||
|
String res2 = res.get_2();
|
||||||
|
StringBuilder pathBuilder = new StringBuilder();
|
||||||
|
if (StrUtil.isNotBlank(res0)) {
|
||||||
|
pathBuilder.append(res0);
|
||||||
|
if (!res0.endsWith("/")) pathBuilder.append("/");
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotBlank(res1)) pathBuilder.append(res1);
|
||||||
|
else pathBuilder.append(IdUtil.fastSimpleUUID()).append(".txt");
|
||||||
|
map.put(tplName, MapUtil.<String, String>builder()
|
||||||
|
.put("path", pathBuilder.toString())
|
||||||
|
.put("content", res2)
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
return R.success(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取表信息
|
||||||
|
*/
|
||||||
|
@GetMapping("/list_table")
|
||||||
|
public R<List<Map<String, Object>>> listTable(@RequestParam(value = "tableName", required = false) String tableName) {
|
||||||
|
return R.success(tplService.listTable(tableName));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有模板
|
||||||
|
*/
|
||||||
|
@GetMapping("/list")
|
||||||
|
public R<?> listAll() {
|
||||||
|
return R.success(tplService.list());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 详情
|
* 详情
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,12 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.njzscloud.common.core.ex.Exceptions;
|
import com.njzscloud.common.core.ex.Exceptions;
|
||||||
|
import com.njzscloud.common.gen.support.DbMetaData;
|
||||||
import com.njzscloud.common.gen.support.TemplateEngine;
|
import com.njzscloud.common.gen.support.TemplateEngine;
|
||||||
import com.njzscloud.common.gen.support.Tpl;
|
import com.njzscloud.common.gen.support.Tpl;
|
||||||
import com.njzscloud.common.mp.support.PageParam;
|
import com.njzscloud.common.mp.support.PageParam;
|
||||||
import com.njzscloud.common.mp.support.PageResult;
|
import com.njzscloud.common.mp.support.PageResult;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
@ -22,6 +24,7 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class TplService extends ServiceImpl<TplMapper, TplEntity> implements IService<TplEntity> {
|
public class TplService extends ServiceImpl<TplMapper, TplEntity> implements IService<TplEntity> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -99,4 +102,9 @@ public class TplService extends ServiceImpl<TplMapper, TplEntity> implements ISe
|
||||||
return PageResult.of(this.page(pageParam.toPage(), Wrappers.query(tplEntity)));
|
return PageResult.of(this.page(pageParam.toPage(), Wrappers.query(tplEntity)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final DbMetaData dbMetaData;
|
||||||
|
|
||||||
|
public List<Map<String, Object>> listTable(String tableName) {
|
||||||
|
return dbMetaData.getTables(tableName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,8 @@ import javax.sql.DataSource;
|
||||||
public class GenAutoConfiguration {
|
public class GenAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TplService sysTplService() {
|
public TplService sysTplService(DbMetaData dbMetaData) {
|
||||||
return new TplService();
|
return new TplService(dbMetaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
||||||
|
|
@ -70,4 +70,24 @@ public class DbMetaData {
|
||||||
}
|
}
|
||||||
return tableInfos;
|
return tableInfos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Map<String, Object>> getTables(String tableName) {
|
||||||
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
|
List<Map<String, Object>> tableInfos = new ArrayList<>();
|
||||||
|
DatabaseMetaData metaData = connection.getMetaData();
|
||||||
|
try (ResultSet tablesData = metaData.getTables(null, null, StrUtil.isNotBlank(tableName) ? tableName : "%", new String[]{"TABLE"})) {
|
||||||
|
while (tablesData.next()) {
|
||||||
|
String tableName_ = tablesData.getString("TABLE_NAME");
|
||||||
|
tableInfos.add(MapUtil.<String, Object>builder()
|
||||||
|
.put("name", tableName_)
|
||||||
|
.put("comment", tablesData.getString("REMARKS"))
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tableInfos;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取数据库元数据失败", e);
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,11 @@ import java.util.Map;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class Generator {
|
public class Generator {
|
||||||
|
private static final DbMetaData dbMetaData = SpringUtil.getBean(DbMetaData.class);
|
||||||
|
private static final TplService tplService = SpringUtil.getBean(TplService.class);
|
||||||
|
|
||||||
private static Tuple3<String, String, String> generate0(String tplName, String tableName, Map<String, Object> data) {
|
private static Tuple3<String, String, String> generate0(String tplName, String tableName, Map<String, Object> data) {
|
||||||
DbMetaData dbMetaData = SpringUtil.getBean(DbMetaData.class);
|
|
||||||
TplService tplService = SpringUtil.getBean(TplService.class);
|
|
||||||
Assert.notBlank(tableName, () -> Exceptions.clierr("未指定表名称"));
|
Assert.notBlank(tableName, () -> Exceptions.clierr("未指定表名称"));
|
||||||
data.put("tableName", tableName);
|
data.put("tableName", tableName);
|
||||||
List<Map<String, Object>> table = dbMetaData.getMetaData(tableName);
|
List<Map<String, Object>> table = dbMetaData.getMetaData(tableName);
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue