Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchuangiie authored Aug 11, 2023
1 parent 35536cc commit 89285ae
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 14 deletions.
31 changes: 18 additions & 13 deletions APITemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ public RespValue insert(@RequestParam("name")String name,

@PostMapping(value = "insert1")
public RespValue insert1(HttpServletRequest request) throws JSQLParserException {
String sql = insertSQL("user","name,password,number,time");
//String sql = insertSQL("user","name,password,number,time");
String sql = insertSQL("user");
//List<Object> args = insertValue(sql,request);
//List<Object> args = fillValueByNameString(request,"name,password,Integer number");
List<Object> args = fillValueByTableForInsert(request,"user");

long start = System.currentTimeMillis(); //获取开始时间
List<Object> args = insertValue(sql,request);
long end = System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: " + (end - start) + "ms");

args.add(TimeUtil.getCurrentDateString());
args.set(3,TimeUtil.getCurrentDateString());
int result = baseMapper.insert(sql,args.toArray());
return new RespValue(0,"插入成功",result);
}
Expand Down Expand Up @@ -74,7 +73,7 @@ public RespValue delete(@RequestParam("id") Integer id){
}

@PostMapping(value="delete1")
public RespValue delete1(HttpServletRequest request) throws JSQLParserException {
public RespValue delete1(HttpServletRequest request) {
int result = baseMapper.delete(deleteSQL("user"),idValue(request));
//int result = baseMapper.delete(deleteSQL("user"),fillValueByName(request,"Integer id").toArray());
return new RespValue(0,"删除成功",result);
Expand All @@ -100,11 +99,11 @@ public RespValue update(@RequestParam("id") Integer id,

@PostMapping(value = "update1")
public RespValue update1(HttpServletRequest request) throws JSQLParserException {
String sqlStr = updateSQL("user","name,password,number");
long start = System.currentTimeMillis(); //获取开始时间
List<Object> args = updateValueById(sqlStr,request);
long end = System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: " + (end - start) + "ms");
//String sqlStr = updateSQL("user","name,password,number");
String sqlStr = updateSQL("user");
//List<Object> args = updateValueById(sqlStr,request);
//List<Object> args = fillValueByNameString(request,"name,password,Integer number,Long id");
List<Object> args = fillValueByTableForUpdate(request,"user");
int result = baseMapper.update(sqlStr,args.toArray());
return new RespValue(0,"修改成功",result);
}
Expand All @@ -116,6 +115,12 @@ public RespValue findObjectById(@RequestParam("id") Integer id){
return new RespValue(0,"",result);
}

@PostMapping(value = "findObjectById1")
public RespValue findObjectById1(HttpServletRequest request) {
LinkedHashMap<String, Object> result = baseMapper.get(getSQL("user"),idValue(request));
return new RespValue(0,"",result);
}

@PostMapping(value="findListByCondition")
public RespValue findListByCondition(@RequestParam(name="name",required = false)String name,
@RequestParam(name="number",required = false)Integer number,
Expand Down
25 changes: 24 additions & 1 deletion AutoValueFromSqlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.example.demo.util.SQLBuilderUtil.typeColumns;

public class AutoValueFromSqlUtil {

private static Pattern pattern = Pattern.compile("^\\w[-\\w.+]*$");
Expand Down Expand Up @@ -107,7 +109,7 @@ public static List<Object> updateValueById(String sql, HttpServletRequest reques
return args;
}

public static long idValue(HttpServletRequest request) throws JSQLParserException {
public static long idValue(HttpServletRequest request) {
RequestValue r = new RequestValue(request);
return r.getLong("id");
}
Expand Down Expand Up @@ -173,6 +175,27 @@ public static List<Object> fillValueByName(HttpServletRequest request,String ...
}


public static List<Object> fillValueByNameString(HttpServletRequest request,String namesString) {

String[] names = namesString.split("\\,");
List<Object> args = fillValueByName(request,names);
return args;
}

public static List<Object> fillValueByTableForInsert(HttpServletRequest request,String table) {
String namesString = typeColumns(table);
List<Object> args = fillValueByNameString(request,namesString);
return args;
}

public static List<Object> fillValueByTableForUpdate(HttpServletRequest request,String table) {
String namesString = typeColumns(table);
namesString = namesString + ",Long id";
List<Object> args = fillValueByNameString(request,namesString);
return args;
}


/**
* 遍历所有节点的{@link CCJSqlParserVisitor} 接口实现类
* @author
Expand Down
56 changes: 56 additions & 0 deletions GenerateFromDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ private String getInterPara() {
String type = "";
if(t.contains("bigint")){
type = "Long";
}else if(t.contains("smallint")){
type = "Short";
}else if(t.contains("int")){
type = "Integer";
}else if(t.contains("double")){
type = "Double";
}else if(t.contains("float")){
type = "Float";
}else if(t.contains("varchar")){
type = "String";
}else if(t.contains("text")){
type = "String";
}else if(t.contains("json")){
type = "String";
}else if(t.contains("datetime")){
type = "String";
}else{
Expand Down Expand Up @@ -70,6 +78,48 @@ private String getParaList() {
return reString.substring(0, reString.length()-1)+"\n";
}

private String getTypeParaList() {

//System.out.println("result:"+result);

String reString = "";

for (LinkedHashMap<String, Object> m : result) {
String f = (String) m.get("Field");
String t = (String) m.get("Type");
if(f.equals("id"))continue;

String type = "";
if(t.contains("bigint")){
type = "Long";
}else if(t.contains("smallint")){
type = "Short";
}else if(t.contains("int")){
type = "Integer";
}else if(t.contains("double")){
type = "Double";
}else if(t.contains("float")){
type = "Float";
}else if(t.contains("varchar")){
type = "String";
}else if(t.contains("text")){
type = "String";
}else if(t.contains("json")){
type = "String";
}else if(t.contains("datetime")){
type = "String";
}else{

}

reString += type+" "+f+",";
//System.out.println("@RequestParam(name=\""+f+"\",required = false)"+type+" "+f+",");
}


return reString.substring(0, reString.length()-1)+"\n";
}


private String getUptFilter() {
//System.out.println("result:"+result);
Expand Down Expand Up @@ -237,9 +287,15 @@ public static void main(String[] args) {
String para1String = new GenerateFromDB().getInterPara();
System.out.println("接口参数列表(区分了id非空) = " + "\n" + para1String);




String paraList = new GenerateFromDB().getParaList();
System.out.println("新增接口使用的参数列表串(过滤了id) = " + "\n" + paraList);

String typeParaList = new GenerateFromDB().getTypeParaList();
System.out.println("新增接口使用的参数列表串(过滤了id) = " + "\n" + typeParaList);

String uptFilter = new GenerateFromDB().getUptFilter();
System.out.println("修改接口使用的参数过滤语句(过滤了id) = " + "\n" + uptFilter);

Expand Down
73 changes: 73 additions & 0 deletions SQLBuilderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import org.apache.kafka.common.protocol.types.Field;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

public class SQLBuilderUtil {


static List<LinkedHashMap<String, Object>> result = JDBCUtil.select("desc user");
public static String pageAndOrderBuilder(String sql,String orderColumn,String orderDirection,
Integer pageNum,Integer pageSize) {

Expand Down Expand Up @@ -39,6 +43,62 @@ public static String pageAndOrderBuilder(String sql, HttpServletRequest request)
System.out.println("111pageSize = " + pageSize);
return pageAndOrderBuilder(sql,orderColumn,orderDirection,pageNum,pageSize);
}

public static String columns(String table) {

String sql = "";

for (LinkedHashMap<String, Object> m : result) {
String f = (String) m.get("Field");
String t = (String) m.get("Type");
if(f.equals("id"))continue;
sql += ""+f+",";
//System.out.println("@RequestParam(name=\""+f+"\",required = false)"+type+" "+f+",");
}

return sql.substring(0, sql.length()-1);

}

public static String typeColumns(String table) {

String sql = "";

for (LinkedHashMap<String, Object> m : result) {
String f = (String) m.get("Field");
String t = (String) m.get("Type");
if(f.equals("id"))continue;
System.out.println("t = " + t);
String type = "";
if(t.contains("bigint")){
type = "Long";
}else if(t.contains("smallint")){
type = "Short";
}else if(t.contains("int")){
type = "Integer";
}else if(t.contains("double")){
type = "Double";
}else if(t.contains("float")){
type = "Float";
}else if(t.contains("varchar")){
type = "String";
}else if(t.contains("text")){
type = "String";
}else if(t.contains("json")){
type = "String";
}else if(t.contains("datetime")){
type = "String";
}else{

}

sql += type+" "+f+",";
//System.out.println("@RequestParam(name=\""+f+"\",required = false)"+type+" "+f+",");
}

return sql.substring(0, sql.length()-1);
}

public static String deleteSQL(String table) {

String sql = "delete from "+table+" where id=?";
Expand All @@ -49,11 +109,24 @@ public static String insertSQL(String table, String columns) {
String sql = "INSERT INTO "+table+"("+columns+") VALUES("+columns.replaceAll("\\w[-\\w.+]*","?")+")";
return sql;
}

public static String insertSQL(String table) {

String columns = columns(table);
String sql = insertSQL(table, columns);
return sql;
}
public static String updateSQL(String table, String columns) {

String sql = "update user set "+columns.replaceAll(",","=?,")+"=? where id=?";
return sql;
}

public static String updateSQL(String table) {
String columns = columns(table);
String sql = updateSQL(table, columns);
return sql;
}
public static String getSQL(String table) {

String sql = "SELECT * FROM "+table+" where id=?";
Expand Down

0 comments on commit 89285ae

Please sign in to comment.