main
黄海 9 months ago
parent 4349415608
commit cc6e055487

@ -7,15 +7,31 @@ import com.jfinal.kit.Kv;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.FilenameFilter;
public class PublishUtil {
/**
*
*
* @param path
* @throws IOException
* @throws InterruptedException
*/
public static void ExecBatchCmd(String path) throws IOException, InterruptedException {
String[] cmd = {path};
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
}
public static void doLogin(SSHUtil ssh, String username, String password) throws Exception {
String cmd = "docker login --username=" + username + " --password=" + password + " registry.cn-hangzhou.aliyuncs.com";
ssh.exec(cmd);
@ -48,6 +64,11 @@ public class PublishUtil {
* docker push registry.cn-hangzhou.aliyuncs.com/yltcharge/jdk:21
*/
public static Kv publish(String projectName, boolean isStatic, String workingPath, String localLibPath, Kv choiceWarehouse, String choiceConfig) throws Exception {
//判断是不是有需要批处理的生成JAR的过程
String cmdPath = workingPath + "生成依赖jar.bat";
if (FileUtil.exist(cmdPath)) {
ExecBatchCmd(cmdPath);
}
System.out.println("正在生成" + projectName + "的镜像...");
//配置文件
String path = PathKit.getRootClassPath() + "\\publishImage.json";

@ -48,6 +48,14 @@
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.33</version>
</dependency>
<!--日志logback-->
<dependency>
<groupId>ch.qos.logback</groupId>

@ -1,50 +0,0 @@
/* 下面代码开始维护 */
/**增加
*
* @param type_name 类型名称
*/
@Before({POST.class})
@JwtCheckInterface({})
public void addWxFaultType(String type_name ){
ym.addWxFaultType(type_name );
renderJson(RetKit.renderSuccess());
}
//按ID删除
@Before({POST.class})
@JwtCheckInterface({})
@IsNumericInterface({"type_id"})
public void delWxFaultTypeById(int type_id){
ym.delWxFaultTypeById(type_id);
renderJson(RetKit.renderSuccess());
}
/**按ID修改
*
* @param type_name 类型名称
*/
@Before({POST.class})
@JwtCheckInterface({})
//@IsNumericInterface({"type_id"})
public void updateWxFaultTypeById(int type_id,String type_name ){
ym.updateWxFaultTypeById(type_id,type_name );
renderJson(RetKit.renderSuccess());
}
//按ID查询
@Before({GET.class})
@JwtCheckInterface({})
@IsNumericInterface({"type_id"})
public void getWxFaultTypeById(int type_id){
renderJson(RetKit.renderSuccess(ym.getWxFaultTypeById(type_id)));
}
//分页查询
@Before({GET.class})
@JwtCheckInterface({})
public void getWxFaultTypeList(int pageNum, int pageSize){
Page<Record> listPage = ym.getWxFaultTypeList(pageNum, pageSize);
renderJson(RetKit.renderSuccess(listPage));
}

@ -1,38 +0,0 @@
/**增加
*
* @param type_name 类型名称
*/
public void addWxFaultType(String type_name ){
Record record = new Record();
record.set("type_name",type_name);
Db.save("t_wx_fault_type","type_id",record);
}
//删除
public void delWxFaultTypeById(int type_id){
String sql="delete from t_wx_fault_type where type_id=?";
Db.update(sql,type_id);
}
/**修改
*
* @param type_name 类型名称
*/
public void updateWxFaultTypeById(int type_id,String type_name ){
String sql="update t_wx_fault_type set type_name= ? where type_id=?";
Db.update(sql,type_name , type_id);
}
//单条查询
public Record getWxFaultTypeById(int type_id){
String sql="select * from t_wx_fault_type where type_id=?";
return Db.findFirst(sql,type_id);
}
//分页查询
public Page<Record> getWxFaultTypeList(int pageNum, int pageSize){
String sql1="select * ";
String sql2=" from t_wx_fault_type ";
return Db.paginate(pageNum, pageSize, sql1,sql2);
}

@ -1,76 +0,0 @@
/* 下面代码开始维护维修记录表 */
/**增加维修记录表
*
* @param station_id 场站ID
* @param station_name 场站名称
* @param equipmentId 桩ID
* @param equipmentName 桩名称
* @param connectorId 枪ID
* @param connectorNo 枪名称
* @param wx_openid 维修人员的微信open_id
* @param status_id 维修单状态: 1正在维修2维修完成3无法维修
* @param type_id 故障类型id
* @param type_name 故障类型名称
* @param memo 维修情况说明
*/
@Before({POST.class})
//@JwtCheckInterface({})
//@EmptyInterface({""})
//@IsNumericInterface({})
public void addWxRecord(int station_id , String station_name , int equipmentId , String equipmentName , int connectorId , String connectorNo , String wx_openid , int status_id , int type_id , String type_name , String memo ){
ym.addWxRecord(station_id , station_name , equipmentId , equipmentName , connectorId , connectorNo , wx_openid , status_id , type_id , type_name , memo );
renderJson(RetKit.renderSuccess());
}
//按ID删除维修记录表
@Before({POST.class})
//@JwtCheckInterface({})
//@EmptyInterface({""})
//@IsNumericInterface({"id"})
public void delWxRecordById(int id){
ym.delWxRecordById(id);
renderJson(RetKit.renderSuccess());
}
/**按ID修改维修记录表
*
* @param station_id 场站ID
* @param station_name 场站名称
* @param equipmentId 桩ID
* @param equipmentName 桩名称
* @param connectorId 枪ID
* @param connectorNo 枪名称
* @param wx_openid 维修人员的微信open_id
* @param status_id 维修单状态: 1正在维修2维修完成3无法维修
* @param type_id 故障类型id
* @param type_name 故障类型名称
* @param memo 维修情况说明
*/
@Before({POST.class})
//@JwtCheckInterface({})
//@EmptyInterface({""})
//@IsNumericInterface({"id"})
public void updateWxRecordById(int id,int station_id , String station_name , int equipmentId , String equipmentName , int connectorId , String connectorNo , String wx_openid , int status_id , int type_id , String type_name , String memo ){
ym.updateWxRecordById(id,station_id , station_name , equipmentId , equipmentName , connectorId , connectorNo , wx_openid , status_id , type_id , type_name , memo );
renderJson(RetKit.renderSuccess());
}
//按ID查询维修记录表
@Before({GET.class})
//@JwtCheckInterface({})
//@EmptyInterface({""})
//@IsNumericInterface({"id"})
public void getWxRecordById(int id){
renderJson(RetKit.renderSuccess(ym.getWxRecordById(id)));
}
//分页查询维修记录表
@Before({GET.class})
//@JwtCheckInterface({})
//@EmptyInterface({""})
public void getWxRecordList(int pageNum, int pageSize){
Page<Record> listPage = ym.getWxRecordList(pageNum, pageSize);
renderJson(RetKit.renderSuccess(listPage));
}

@ -1,69 +0,0 @@
/**增加维修记录表
*
* @param station_id 场站ID
* @param station_name 场站名称
* @param equipmentId 桩ID
* @param equipmentName 桩名称
* @param connectorId 枪ID
* @param connectorNo 枪名称
* @param wx_openid 维修人员的微信open_id
* @param status_id 维修单状态: 1正在维修2维修完成3无法维修
* @param type_id 故障类型id
* @param type_name 故障类型名称
* @param memo 维修情况说明
*/
public void addWxRecord(int station_id , String station_name , int equipmentId , String equipmentName , int connectorId , String connectorNo , String wx_openid , int status_id , int type_id , String type_name , String memo ){
Record record = new Record();
record.set("station_id",station_id);
record.set("station_name",station_name);
record.set("equipmentId",equipmentId);
record.set("equipmentName",equipmentName);
record.set("connectorId",connectorId);
record.set("connectorNo",connectorNo);
record.set("wx_openid",wx_openid);
record.set("status_id",status_id);
record.set("create_time", DateTime.now());
record.set("type_id",type_id);
record.set("type_name",type_name);
record.set("memo",memo);
Db.save("t_wx_record","id",record);
}
//删除维修记录表
public void delWxRecordById(int id){
String sql="delete from t_wx_record where id=?";
Db.update(sql,id);
}
/**修改维修记录表
*
* @param station_id 场站ID
* @param station_name 场站名称
* @param equipmentId 桩ID
* @param equipmentName 桩名称
* @param connectorId 枪ID
* @param connectorNo 枪名称
* @param wx_openid 维修人员的微信open_id
* @param status_id 维修单状态: 1正在维修2维修完成3无法维修
* @param type_id 故障类型id
* @param type_name 故障类型名称
* @param memo 维修情况说明
*/
public void updateWxRecordById(int id,int station_id , String station_name , int equipmentId , String equipmentName , int connectorId , String connectorNo , String wx_openid , int status_id , int type_id , String type_name , String memo ){
String sql="update t_wx_record set station_id= ?,station_name= ?,equipmentId= ?,equipmentName= ?,connectorId= ?,connectorNo= ?,wx_openid= ?,status_id= ?,create_time= DateTime.now(),type_id= ?,type_name= ?,memo= ? where id=?";
Db.update(sql,station_id , station_name , equipmentId , equipmentName , connectorId , connectorNo , wx_openid , status_id , type_id , type_name , memo , id);
}
//单条查询维修记录表
public Record getWxRecordById(int id){
String sql="select * from t_wx_record where id=?";
return Db.findFirst(sql,id);
}
//分页查询维修记录表
public Page<Record> getWxRecordList(int pageNum, int pageSize){
String sql1="select * ";
String sql2="fromt_wx_record ";
return Db.paginate(pageNum, pageSize, sql1,sql2);
}

@ -0,0 +1,22 @@
package com.dsideal.Tools;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FreeMarker {
public static void main(String[] args) throws IOException, TemplateException {
}
}

@ -0,0 +1,473 @@
package com.dsideal.Tools;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import com.dsideal.Utils.dsKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import com.jfinal.plugin.activerecord.Record;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
public class GenerateCodeWithFreeMaker {
// JavaBean保存的位置
public static String beanPath;
// mysql与java的数据类型映射
public static Map<String, String> dataTypeMap = new HashMap<>();
/**
*
*
* @return List<String>
* @throws Exception
*/
public static List<String> getTableNames(Connection conn) throws Exception {
List<String> tables = new ArrayList<>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("show tables");
while (rs.next()) {
String tableName = rs.getString(1);
if (tableName.equals(PropKit.get("tableName"))) {
tables.add(tableName);
}
}
rs.close();
stmt.close();
return tables;
}
/**
* +
*
* @param table
* @return
* @throws Exception
*/
public static JSONObject getStructure(Connection conn, String table) throws Exception {
//表结构的描述JSON对象
JSONObject jo = new JSONObject();
//字段,不包含主键
Map<String, String> dataTypeMap = new LinkedHashMap<>();
//字段描述
Map<String, String> commentMap = new LinkedHashMap<>();
//表注释
String comment = "";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("show create table " + table);
if (rs != null && rs.next()) {
String createDDL = rs.getString(2);
int index = createDDL.indexOf("COMMENT='");
if (index >= 0) {
comment = createDDL.substring(index + 9);
comment = comment.substring(0, comment.length() - 1);
}
}
//类名
String beanNameWithoutT = dsKit.toCamelCase(dsKit.capitalizeFirstLetter(table));
List<String> fields = new ArrayList<>();
//字段信息
rs = stmt.executeQuery("show full columns from " + table);
while (rs.next()) {
String Type = rs.getString("Type");
String Field = rs.getString("Field");
String Comment = rs.getString("Comment");
commentMap.put(Field, Comment);
fields.add(Field);
if (Type.contains("(")) {
Type = Type.substring(0, Type.indexOf("("));
}
Type = Type.toUpperCase();
//记录主键
if (rs.getString("Key").equals("PRI")) {
jo.put("key", Field);
jo.put("key_type", GenerateCodeWithFreeMaker.dataTypeMap.get(Type));
} else {//否则记录到map中
dataTypeMap.put(Field, GenerateCodeWithFreeMaker.dataTypeMap.get(Type));
}
}
//关闭连接
rs.close();
stmt.close();
jo.put("fields", dataTypeMap);
jo.put("fieldsComment", commentMap);
jo.put("beanNameWithoutT", beanNameWithoutT);
jo.put("comment", comment);
jo.put("table", table);
return jo;
}
/**
*
*/
public static void initMap() {
//mysql与java的数据类型映射
dataTypeMap.put("INT", "int");
dataTypeMap.put("BIGINT", "long");
dataTypeMap.put("DOUBLE", "double");
dataTypeMap.put("FLOAT", "float");
dataTypeMap.put("TINYINT", "int");
dataTypeMap.put("DATE", "java.util.Date");
dataTypeMap.put("TIMESTAMP", "java.util.Date");
dataTypeMap.put("BIGINT UNSIGNED", "long");
dataTypeMap.put("INT UNSIGNED", "long");
dataTypeMap.put("MEDIUMINT", "int");
dataTypeMap.put("MEDIUMINT UNSIGNED", "long");
dataTypeMap.put("SMALLINT", "int");
dataTypeMap.put("SMALLINT UNSIGNED", "long");
dataTypeMap.put("TINYINT UNSIGNED", "long");
dataTypeMap.put("LONGTEXT", "String");
dataTypeMap.put("VARCHAR", "String");
dataTypeMap.put("CHAR", "String");
dataTypeMap.put("TEXT", "String");
dataTypeMap.put("DATETIME", "java.util.Date");
dataTypeMap.put("DECIMAL", "BigDecimal");
dataTypeMap.put("BOOLEAN", "boolean");
}
public static void main(String[] args) throws Exception {
//加载配置文件
PropKit.use("application.properties");
beanPath = PropKit.get("beanPath");
if (!FileUtil.exist(beanPath)) {
FileUtil.mkdir(beanPath);
}
initMap();
// 配置Druid数据源插件
HikariCpPlugin hpPlugin = new HikariCpPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
PropKit.get("password").trim(), PropKit.get("driverClassName"));
hpPlugin.start();
// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin(hpPlugin);
hpPlugin.start();
arp.start();
DataSource dataSource = hpPlugin.getDataSource();
// 数据库连接
Connection conn = dataSource.getConnection();
//开始生成
List<String> tables = getTableNames(conn);
String tableNames = "";
//表描述
for (String table : tables) {
JSONObject jo = getStructure(conn, table);
tableNames += table + ",";
if (StrKit.isBlank(jo.getStr("key"))) {
dsKit.print("表" + table + "没有主键,请检查!");
}
//生成代码
autoCodeFreeMaker(jo);
}
if (StrKit.isBlank(tableNames)) {
dsKit.print("没有找到表,请检查!");
return;
}
//去掉最后的逗号
tableNames = tableNames.substring(0, tableNames.length() - 1);
//关闭数据库
conn.close();
arp.stop();
hpPlugin.stop();
System.out.println(dsKit.getCurrentTimeStr() + " 恭喜,表" + tableNames + "代码已成功生成!");
}
/**
*
*
* @param jo
*/
public static void autoCode(JSONObject jo) {
//不带主键的其它字段拼接成java的函数声明内容
// 迭代map将按照插入顺序
Map<String, String> fields = (Map<String, String>) jo.get("fields");
Map<String, String> fieldsComment = (Map<String, String>) jo.get("fieldsComment");
String fullParameters = "";//带数据类型+字段名称
String parameters = "";//不带数据类型,只有字段名称
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time") && !entry.getKey().equals("b_use")) {
fullParameters += entry.getValue() + " " + entry.getKey() + " , ";//这里必须是反着来的,否则数据类型会重复
parameters += entry.getKey() + " , ";
}
}
//移除最后一个逗号
fullParameters = fullParameters.substring(0, fullParameters.length() - 1);
if (fullParameters.endsWith(",")) {//去掉最后的逗号
fullParameters = fullParameters.substring(0, fullParameters.length() - 1);
}
parameters = parameters.substring(0, parameters.length() - 1);
if (parameters.endsWith(",")) {
parameters = parameters.substring(0, parameters.length() - 1);
}
//后缀名称
String beanNameWithoutT = jo.getStr("beanNameWithoutT");
if (beanNameWithoutT.startsWith("T")) {
beanNameWithoutT = beanNameWithoutT.substring(1);
}
//表的描述
String comment = jo.getStr("comment");
//主键
String key = jo.getStr("key");
String key_type = jo.getStr("key_type");
//表名
String table = jo.getStr("table");
StringBuilder sb = new StringBuilder();
//sb.append("//-------------------下面是Controller部分-------------------\n");
sb.append("/* 下面代码开始维护" + comment + " */\n");
sb.append("\n");
//1、增加
sb.append(" /**增加" + comment + "\n");
sb.append(" *\n");
//迭代
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time")) {
sb.append(" * @param " + entry.getKey() + " " + fieldsComment.get(entry.getKey()) + "\n");
}
}
sb.append(" */\n");
sb.append(" @Before({POST.class})\n");
sb.append(" @JwtCheckInterface({})\n");
sb.append(" public void add" + beanNameWithoutT + "(" + fullParameters + "){\n");
sb.append(" " + PropKit.get("daoName") + ".add" + beanNameWithoutT + "(" + parameters + ");\n");
sb.append(" renderJson(RetKit.renderSuccess());\n");
sb.append(" }\n");
//2、删除
sb.append("\n");
sb.append(" //按ID删除" + comment + "\n");
sb.append(" @Before({POST.class})\n");
sb.append(" @JwtCheckInterface({})\n");
sb.append(" @IsNumericInterface({\"" + key + "\"})\n");
sb.append(" public void del" + beanNameWithoutT + "ById(" + key_type + " " + key + "){\n");
sb.append(" " + PropKit.get("daoName") + ".del" + beanNameWithoutT + "ById(" + key + ");\n");
sb.append(" renderJson(RetKit.renderSuccess());\n");
sb.append(" }\n");
//3、修改
sb.append("\n");
sb.append(" /**按ID修改" + comment + "\n");
sb.append(" *\n");
//迭代
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time")) {
sb.append(" * @param " + entry.getKey() + " " + fieldsComment.get(entry.getKey()) + "\n");
}
}
sb.append(" */\n");
sb.append(" @Before({POST.class})\n");
sb.append(" @JwtCheckInterface({})\n");
sb.append(" //@IsNumericInterface({\"" + key + "\"})\n");
sb.append(" public void update" + beanNameWithoutT + "ById(" + key_type + " " + key + "," + fullParameters + "){\n");
sb.append(" " + PropKit.get("daoName") + ".update" + beanNameWithoutT + "ById(" + key + "," + parameters + ");\n");
sb.append(" renderJson(RetKit.renderSuccess());\n");
sb.append(" }\n");
sb.append("\n");
//4、单条查询
sb.append(" //按ID查询" + comment + "\n");
sb.append(" @Before({GET.class})\n");
sb.append(" @JwtCheckInterface({})\n");
sb.append(" @IsNumericInterface({\"" + key + "\"})\n");
sb.append(" public void get" + beanNameWithoutT + "ById(" + key_type + " " + key + "){\n");
sb.append(" renderJson(RetKit.renderSuccess(" + PropKit.get("daoName") + ".get" + beanNameWithoutT + "ById(" + key + ")));\n");
sb.append(" }\n");
sb.append("\n");
//5、分页查询
sb.append(" //分页查询" + comment + "\n");
sb.append(" @Before({GET.class})\n");
sb.append(" @JwtCheckInterface({})\n");
sb.append(" public void get" + beanNameWithoutT + "List(int pageNum, int pageSize){\n");
sb.append(" Page<Record> listPage = " + PropKit.get("daoName") + ".get" + beanNameWithoutT + "List(pageNum, pageSize);\n");
sb.append(" renderJson(RetKit.renderSuccess(listPage));\n");
sb.append(" }\n");
sb.append("\n");
FileUtil.writeUtf8String(sb.toString(), beanPath + "/" + beanNameWithoutT + "Controller.txt");
sb.append("/* 下面代码开始维护" + comment + " */\n");
sb.append("\n");
sb = new StringBuilder();
//1、增加
sb.append(" /**增加" + comment + "\n");
sb.append(" *\n");
//迭代
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time")) {
sb.append(" * @param " + entry.getKey() + " " + fieldsComment.get(entry.getKey()) + "\n");
}
}
sb.append(" */\n");
sb.append(" public void add" + beanNameWithoutT + "(" + fullParameters + "){\n");
sb.append(" Record record = new Record();\n");
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time")) {
sb.append(" record.set(\"" + entry.getKey() + "\"," + entry.getKey() + ");\n");
} else {
sb.append(" record.set(\"" + entry.getKey() + "\", DateTime.now());\n");
}
}
sb.append(" Db.save(\"" + table + "\",\"" + key + "\",record);\n");
sb.append("}\n");
//2、删除
sb.append("\n");
sb.append(" //删除" + comment + "\n");
sb.append(" public void del" + beanNameWithoutT + "ById(" + key_type + " " + key + "){\n");
sb.append(" String sql=\"delete from " + table + " where " + key + "=?\";\n");
sb.append(" Db.update(sql," + key + ");\n");
sb.append(" }\n");
sb.append("\n");
//3、修改
sb.append(" /**修改" + comment + "\n");
sb.append(" *\n");
//迭代
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time")) {
sb.append(" * @param " + entry.getKey() + " " + fieldsComment.get(entry.getKey()) + "\n");
}
}
sb.append(" */\n");
sb.append(" public void update" + beanNameWithoutT + "ById(" + key_type + " " + key + "," + fullParameters + "){\n");
sb.append(" String sql=\"update " + table + " set ");
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time")) {
sb.append(entry.getKey() + "= ?,");
} else {
sb.append(entry.getKey() + "= DateTime.now(),");
}
}
sb = sb.deleteCharAt(sb.length() - 1);
sb.append(" where " + key + "=?\";\n");
sb.append(" Db.update(sql,");
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time")) {
sb.append(entry.getKey() + " , ");
}
}
sb.append(key);
sb.append(");\n");
sb.append(" }\n");
//4、单条查询
sb.append("\n");
sb.append(" //单条查询" + comment + "\n");
sb.append(" public Record get" + beanNameWithoutT + "ById(" + key_type + " " + key + "){\n");
sb.append(" String sql=\"select * from " + table + " where " + key + "=?\";\n");
sb.append(" return Db.findFirst(sql," + key + ");\n");
sb.append(" }\n");
sb.append("\n");
//5、分页查询
sb.append(" //分页查询" + comment + "\n");
sb.append(" public Page<Record> get" + beanNameWithoutT + "List(int pageNum, int pageSize){\n");
sb.append(" String sql1=\"select * \";\n");
sb.append(" String sql2=\" from " + table + " \";\n");
sb.append(" return Db.paginate(pageNum, pageSize, sql1,sql2);\n");
sb.append(" }\n");
sb.append("\n");
FileUtil.writeUtf8String(sb.toString(), beanPath + "/" + beanNameWithoutT + "Model.txt");
}
public static void autoCodeFreeMaker(JSONObject jo) throws IOException, TemplateException {
//不带主键的其它字段拼接成java的函数声明内容
// 迭代map将按照插入顺序
Map<String, String> fields = (Map<String, String>) jo.get("fields");
Map<String, String> fieldsComment = (Map<String, String>) jo.get("fieldsComment");
String fullParameters = "";//带数据类型+字段名称
String parameters = "";//不带数据类型,只有字段名称
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time") && !entry.getKey().equals("b_use")) {
fullParameters += entry.getValue() + " " + entry.getKey() + " , ";//这里必须是反着来的,否则数据类型会重复
parameters += entry.getKey() + " , ";
}
}
//移除最后一个逗号
fullParameters = fullParameters.substring(0, fullParameters.length() - 1);
if (fullParameters.endsWith(",")) {//去掉最后的逗号
fullParameters = fullParameters.substring(0, fullParameters.length() - 1);
}
parameters = parameters.substring(0, parameters.length() - 1);
if (parameters.endsWith(",")) {
parameters = parameters.substring(0, parameters.length() - 1);
}
//后缀名称
String beanNameWithoutT = jo.getStr("beanNameWithoutT");
if (beanNameWithoutT.startsWith("T")) {
beanNameWithoutT = beanNameWithoutT.substring(1);
}
//表的描述
String comment = jo.getStr("comment");
//主键
String key = jo.getStr("key");
String key_type = jo.getStr("key_type");
//表名
String table = jo.getStr("table");
Map<String, Object> dataModel = new HashMap<>();
dataModel.put("comment", comment);
List<Record> params = new ArrayList<>();
//迭代
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (!entry.getKey().equals("create_time")) {
Record record = new Record();
record.set("key", entry.getKey());
record.set("comment", fieldsComment.get(entry.getKey()));
params.add(record);
}
}
dataModel.put("params", params);
dataModel.put("daoName", PropKit.get("daoName"));
dataModel.put("beanNameWithoutT", beanNameWithoutT);
dataModel.put("fullParameters", fullParameters);
dataModel.put("parameters", parameters);
// 创建一个 Configuration 实例
Configuration cfg = new Configuration(new Version(2, 3, 33));
// 设置模板目录
String path = System.getProperty("user.dir") + "/dsUtils/src/main/resources";
cfg.setDirectoryForTemplateLoading(new File(path));
// 设置字符编码
cfg.setDefaultEncoding("UTF-8");
// 加载模板
Template template = cfg.getTemplate("Controller.ftl");
// 渲染模板到文件
Writer out = new FileWriter("c:/output_users.txt");
template.process(dataModel, out);
out.close();
}
}

@ -0,0 +1,12 @@
/**
* 增加${comment}
<#list params as param>
* @param ${param.key} ${param.comment}
</#list>
*/
@Before({POST.class})
@JwtCheckInterface({})
public void add${beanNameWithoutT}(${fullParameters}){
${daoName}.add${beanNameWithoutT}(${parameters});
renderJson(RetKit.renderSuccess());
}

@ -0,0 +1,7 @@
列表<br/>
<#list users as user>
<li>${user.name} - ${user.age}</li>
</#list>
变量<br/>
${title}

@ -0,0 +1,7 @@
列表<br/>
<#list users as user>
<li>${user.name} - ${user.age}</li>
</#list>
变量<br/>
${title}

@ -0,0 +1,12 @@
/**
* 增加${comment}
<#list params as param>
* @param ${param.key} ${param.comment}
</#list>
*/
@Before({POST.class})
@JwtCheckInterface({})
public void add${beanNameWithoutT}(${fullParameters}){
${daoName}.add${beanNameWithoutT}(${parameters});
renderJson(RetKit.renderSuccess());
}

@ -0,0 +1,7 @@
列表<br/>
<#list users as user>
<li>${user.name} - ${user.age}</li>
</#list>
变量<br/>
${title}

@ -0,0 +1,7 @@
列表<br/>
<#list users as user>
<li>${user.name} - ${user.age}</li>
</#list>
变量<br/>
${title}

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<ul>
<li>Alice - 25</li>
<li>Bob - 30</li>
<li>Charlie - 35</li>
</ul>
</body>
</html>
Loading…
Cancel
Save