diff --git a/.gitignore b/.gitignore index 6d23dd595e8bed5d8b357ba48818a623f97c7979..b886a4d5bf2bd6266390acae2de20b04391b8847 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ # Created by https://www.gitignore.io/api/java,maven,eclipse,java-web,intellij+all # Edit at https://www.gitignore.io/?templates=java,maven,eclipse,java-web,intellij+all + +gitignore + ### Eclipse ### .metadata bin/ diff --git a/generator-web/src/main/java/com/softdev/system/generator/controller/GeneratorController.java b/generator-web/src/main/java/com/softdev/system/generator/controller/GeneratorController.java index 12704ba0bb591ed6123a404ef08f8fdf11c85958..e8f456ae257b337cc2b13a26905027585e37c3d1 100644 --- a/generator-web/src/main/java/com/softdev/system/generator/controller/GeneratorController.java +++ b/generator-web/src/main/java/com/softdev/system/generator/controller/GeneratorController.java @@ -49,6 +49,9 @@ public class GeneratorController { String templates = generatorService.getTemplateConfig(); return ReturnT.ok().put("templates",templates); } + + + @PostMapping("/code/generate") @ResponseBody public ReturnT generateCode(@RequestBody ParamInfo paramInfo) throws Exception { diff --git a/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java b/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java index 51e318cc9080ca75fb32c3ce0d6e4b4918d59db4..4c560936646a60a95972ddb39087f58aa304af2e 100644 --- a/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java +++ b/generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java @@ -46,6 +46,7 @@ public class TableParseUtil { //deal with java string copy \n" tableSql = tableSql.trim().replaceAll("\\\\n`", "").replaceAll("\\+", "").replaceAll("``", "`").replaceAll("\\\\", ""); // table Name + String tableName = null; if (tableSql.contains("TABLE") && tableSql.contains("(")) { tableName = tableSql.substring(tableSql.indexOf("TABLE") + 5, tableSql.indexOf("(")); @@ -111,6 +112,7 @@ public class TableParseUtil { // 正常( ) 内的一定是字段相关的定义。 String fieldListTmp = tableSql.substring(tableSql.indexOf("(") + 1, tableSql.lastIndexOf(")")); + // 匹配 comment,替换备注里的小逗号, 防止不小心被当成切割符号切割 String commentPattenStr1 = "comment `(.*?)\\`"; Matcher matcher1 = Pattern.compile(commentPattenStr1).matcher(fieldListTmp); @@ -149,6 +151,7 @@ public class TableParseUtil { if (fieldLineList.length > 0) { int i = 0; //i为了解决primary key关键字出现的地方,出现在前3行,一般和id有关 +// 为什么id 不要呢,这里看起来把id略过了,还是说前后端传值的时候不用传id的吗 for (String columnLine : fieldLineList) { i++; columnLine = columnLine.replaceAll("\n", "").replaceAll("\t", "").trim(); @@ -164,7 +167,8 @@ public class TableParseUtil { && !columnLine.contains("pctincrease") && !columnLine.contains("buffer_pool") && !columnLine.contains("tablespace") && !(columnLine.contains("primary ") && i > 3)); - if (specialFlag) { + + if (specialFlag||columnLine.contains("id")) { //如果是oracle的number(x,x),可能出现最后分割残留的,x),这里做排除处理 if (columnLine.length() < 5) { continue; diff --git a/generator-web/src/main/resources/template.json b/generator-web/src/main/resources/template.json index 13c6cb218831e9468586816140f01016d158732d..34f2fcf5c405a159dacb306c85126e8f326ef85c 100644 --- a/generator-web/src/main/resources/template.json +++ b/generator-web/src/main/resources/template.json @@ -249,5 +249,44 @@ "description": "jpacontroller" } ] + }, + { + "group": "mybatis-starp", + "templates": [{ + "id": "110", + "name": "controller-starp", + "description": "controller" + }, + { + "id": "111", + "name": "service-starp", + "description": "service" + }, + { + "id": "112", + "name": "service_impl-starp", + "description": "service_impl" + }, + { + "id": "113", + "name": "mapper-starp", + "description": "mapper" + }, + { + "id": "114", + "name": "mybatis-starp", + "description": "mybatis" + }, + { + "id": "115", + "name": "model-starp", + "description": "model" + }, + { + "id": "116", + "name": "annotation-mapper-starp", + "description": "mapper annotation" + } + ] } ] diff --git a/generator-web/src/main/resources/templates/code-generator/mybatis-starp/annotation-mapper-starp.ftl b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/annotation-mapper-starp.ftl new file mode 100644 index 0000000000000000000000000000000000000000..46caea99cd428523815cebe46cdc8707ed5e11d1 --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/annotation-mapper-starp.ftl @@ -0,0 +1,98 @@ +<#if isWithPackage?exists && isWithPackage==true>package ${packageName}.mapper; +<#if isAutoImport?exists && isAutoImport==true> +import org.apache.ibatis.annotations.*; +import org.springframework.stereotype.Repository; +import java.util.List; + +/** + * @description ${classInfo.classComment}Mapper + * @author ${authorName} + * @date ${.now?string('yyyy-MM-dd')} + */ +@Mapper +@Repository +public interface ${classInfo.className}Mapper { + +//这种假如表是stu 那么id是 stu_id + // @Select("select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{id}") + // public ${classInfo.className} getById(Integer id); + +// @Delete(value = "delete from ${classInfo.tableName} where ${classInfo.tableName}_id=井{${classInfo.className?uncap_first}Id}") +// boolean delete(Integer ${classInfo.className?uncap_first}Id); + +//@Options(useGeneratedKeys=true,keyProperty="${classInfo.className?uncap_first}Id") +//@Insert("insert into ${classInfo.tableName}" + +//" (<#list classInfo.fieldList as fieldItem >${fieldItem.columnName}<#if fieldItem_has_next>,)" + +//" values(<#list classInfo.fieldList as fieldItem >${fieldItem.fieldName}<#if fieldItem_has_next>,<#else>)") +//public Integer insert(${classInfo.className} ${classInfo.className?uncap_first}); + + +// @Update(value = "update ${classInfo.tableName} set " +// <#list classInfo.fieldList as fieldItem > + // <#if fieldItem.columnName != "id">+" ${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next>," + // +// +" where ${classInfo.tableName}_id=井{${classInfo.className?uncap_first}Id} ") +// boolean update(${classInfo.className} ${classInfo.className?uncap_first}); + +//这种假如表是stu 那么id是 id, 两种选择都放着,想用某一种 只要把另外一种注释就行了 +@Select("select * from ${classInfo.tableName} where id=井{id}") +public ${classInfo.className} getById(Integer id); + +@Delete(value = "delete from ${classInfo.tableName} where id=井{id}") +Integer delete(Integer id); + + @Options(useGeneratedKeys=true,keyProperty="id") + @Insert("insert into ${classInfo.tableName}" + + " (<#list classInfo.fieldList as fieldItem >${fieldItem.columnName}<#if fieldItem_has_next>,)" + + " values(<#list classInfo.fieldList as fieldItem >井{${fieldItem.fieldName}}<#if fieldItem_has_next>,<#else>)") + public Integer insert(${classInfo.className} ${classInfo.className?uncap_first}); + + + + + @Update(value = "update ${classInfo.tableName} set " + <#list classInfo.fieldList as fieldItem > + <#if fieldItem.columnName != "id">+" ${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next>," + + +" where id=井{id} ") + Integer update(${classInfo.className} ${classInfo.className?uncap_first}); + + + @Results(value = { + <#list classInfo.fieldList as fieldItem > + @Result(property = "${fieldItem.fieldName}", column = "${fieldItem.columnName}")<#if fieldItem_has_next>, + + }) + @Select(value = "select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{queryParam}") + ${classInfo.className} selectOne(String queryParam); + + @Results(value = { + <#list classInfo.fieldList as fieldItem > + @Result(property = "${fieldItem.fieldName}", column = "${fieldItem.columnName}")<#if fieldItem_has_next>, + + }) + @Select(value = "select * from ${classInfo.tableName} where " + <#list classInfo.fieldList as fieldItem > + +" ${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next> or " + + ) + List<${classInfo.className}> selectList(${classInfo.className} ${classInfo.className?uncap_first}); + +@Results(value = { +<#list classInfo.fieldList as fieldItem > + @Result(property = "${fieldItem.fieldName}", column = "${fieldItem.columnName}")<#if fieldItem_has_next>, + +}) +@Select(value = "select * from ${classInfo.tableName} where " +<#list classInfo.fieldList as fieldItem > + +" ${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next> or " + ++" LIMIT 井{offset} , 井{pageSize}" +) +List<${classInfo.className}> pageList(int offset, int pageSize); + + @Select(value = "SELECT count(1)\n" + + " FROM ${classInfo.tableName} " + ) + Integer pageListCount(int offset, int pageSize); +} diff --git a/generator-web/src/main/resources/templates/code-generator/mybatis-starp/controller-starp.ftl b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/controller-starp.ftl new file mode 100644 index 0000000000000000000000000000000000000000..c5676001c0d8b11f0c8ec85c2666a73b5ec2f016 --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/controller-starp.ftl @@ -0,0 +1,89 @@ +<#if isAutoImport?exists && isAutoImport==true> +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.List; +import java.util.Map; + + +/** + * @description ${classInfo.classComment} + * @author ${authorName} + * @date ${.now?string('yyyy-MM-dd')} + */ +@CrossOrigin +@RestController +@RequestMapping(value = "/${classInfo.className?uncap_first}") +public class ${classInfo.className}Controller { + + @Resource + private ${classInfo.className}Service ${classInfo.className?uncap_first}Service; + +/** +* id 存在就更新 不然插入 +* @param ${classInfo.className?uncap_first} +* @return +*/ +@PostMapping("/save") +public Object save(@RequestBody ${classInfo.className} ${classInfo.className?uncap_first}){ +return ${classInfo.className?uncap_first}Service.save(${classInfo.className?uncap_first}); +} + + /** + * 新增 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + @PostMapping("/insert") + public Object insert(${classInfo.className} ${classInfo.className?uncap_first}){ + return ${classInfo.className?uncap_first}Service.insert(${classInfo.className?uncap_first}); + } + + /** + * 刪除 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + @PostMapping("/delete") + public Object delete(int id){ + return ${classInfo.className?uncap_first}Service.delete(id); + } + + /** + * 更新 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + @PostMapping("/update") + public Object update(${classInfo.className} ${classInfo.className?uncap_first}){ + return ${classInfo.className?uncap_first}Service.update(${classInfo.className?uncap_first}); + } + + /** + * 查询 根据主键 id 查询 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + @PostMapping("/load") + public Object load(int id){ + return ${classInfo.className?uncap_first}Service.load(id); + } + + /** + * 查询 分页查询 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + @PostMapping("/list") + public Map pageList(@RequestParam(required = false, defaultValue = "0") int pageNumber, + @RequestParam(required = false, defaultValue = "10") int pageSize) { + return ${classInfo.className?uncap_first}Service.pageList(pageNumber, pageSize); + //Map pageList = ${classInfo.className?uncap_first}Service.pageList(pageNumber, pageSize); +//return ReturnT.success(pageList); + } + +} diff --git a/generator-web/src/main/resources/templates/code-generator/mybatis-starp/mapper-starp.ftl b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/mapper-starp.ftl new file mode 100644 index 0000000000000000000000000000000000000000..fd8cf1d56025a319638a1f9ffc4e888fd45e8258 --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/mapper-starp.ftl @@ -0,0 +1,59 @@ +<#if isAutoImport?exists && isAutoImport==true> +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; +import java.util.List; + + +/** + * @description ${classInfo.classComment} + * @author ${authorName} + * @date ${.now?string('yyyy-MM-dd')} + */ +@Mapper +@Repository +public interface ${classInfo.className}Mapper { + + /** + * 新增 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + int insert(${classInfo.className} ${classInfo.className?uncap_first}); + + /** + * 刪除 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + int delete(int id); + + /** + * 更新 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + int update(${classInfo.className} ${classInfo.className?uncap_first}); + + /** + * 查询 根据主键 id 查询 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + ${classInfo.className} load(int id); + + /** + * 查询 分页查询 + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + List<${classInfo.className}> pageList(int offset,int pageSize); + + /** + * 查询 分页查询 count + * @author ${authorName} + * @date ${.now?string('yyyy/MM/dd')} + **/ + int pageListCount(int offset,int pageSize); + +} diff --git a/generator-web/src/main/resources/templates/code-generator/mybatis-starp/model-starp.ftl b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/model-starp.ftl new file mode 100644 index 0000000000000000000000000000000000000000..f3aed4be3279ad788b7fb27c5d0e524c9db4bdc2 --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/model-starp.ftl @@ -0,0 +1,43 @@ +<#if isAutoImport?exists && isAutoImport==true> +import java.io.Serializable; +import java.util.Date; +import java.util.List; + +/** + * @description ${classInfo.classComment} + * @author ${authorName} + * @date ${.now?string('yyyy-MM-dd')} + */ +@Data +public class ${classInfo.className} implements Serializable { + + private static final long serialVersionUID = 1L; + +private Integer id; + +<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> +<#list classInfo.fieldList as fieldItem > + <#if isComment?exists && isComment==true>/** + * ${fieldItem.fieldComment} + */ + private ${fieldItem.fieldClass} ${fieldItem.fieldName}; + + + + +<#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> + public ${classInfo.className}() { + } + +<#list classInfo.fieldList as fieldItem> + public ${fieldItem.fieldClass} get${fieldItem.fieldName?cap_first}() { + return ${fieldItem.fieldName}; + } + + public void set${fieldItem.fieldName?cap_first}(${fieldItem.fieldClass} ${fieldItem.fieldName}) { + this.${fieldItem.fieldName} = ${fieldItem.fieldName}; + } + + + +} diff --git a/generator-web/src/main/resources/templates/code-generator/mybatis-starp/mybatis-starp.ftl b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/mybatis-starp.ftl new file mode 100644 index 0000000000000000000000000000000000000000..8cfe1a807f02c4e4682f3bf46f68a4764b3ecb99 --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/mybatis-starp.ftl @@ -0,0 +1,91 @@ + + + + + + + <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> + <#list classInfo.fieldList as fieldItem > + + + + + + + id, + <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> + <#list classInfo.fieldList as fieldItem > + ${fieldItem.columnName}<#if fieldItem_has_next>, + + + + + + INSERT INTO ${classInfo.originTableName} + + <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> + <#list classInfo.fieldList as fieldItem > + <#if fieldItem.columnName != "id" > + and '' != ${fieldItem.fieldName}"> + ${fieldItem.columnName}<#if fieldItem_has_next>, + ${r""} + + + + + + <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0> + <#list classInfo.fieldList as fieldItem > + <#if fieldItem.columnName != "id" > + <#--<#if fieldItem.columnName="addtime" || fieldItem.columnName="updatetime" > + ${r""} + NOW()<#if fieldItem_has_next>, + ${r""} + <#else>--> + and '' != ${fieldItem.fieldName}"> + ${r"#{"}${fieldItem.fieldName}${r"}"}<#if fieldItem_has_next>, + ${r""} + <#----> + + + + + + + + DELETE FROM ${classInfo.originTableName} + WHERE id = ${r"#{id}"} + + + + UPDATE ${classInfo.originTableName} + + <#list classInfo.fieldList as fieldItem > + <#if fieldItem.columnName != "id" && fieldItem.columnName != "AddTime" && fieldItem.columnName != "UpdateTime" > + and '' != ${fieldItem.fieldName}">${fieldItem.columnName} = ${r"#{"}${fieldItem.fieldName}${r"}"}<#if fieldItem_has_next>,${r""} + + + + WHERE id = ${r"#{"}id${r"}"} + + + + + + + + + + diff --git a/generator-web/src/main/resources/templates/code-generator/mybatis-starp/service-starp.ftl b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/service-starp.ftl new file mode 100644 index 0000000000000000000000000000000000000000..6909bf8871b7e6ccf6fe88119a34533f0e662a75 --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/service-starp.ftl @@ -0,0 +1,41 @@ +<#if isAutoImport?exists && isAutoImport==true> +import java.util.Map; + +/** + * @description ${classInfo.classComment} + * @author ${authorName} + * @date ${.now?string('yyyy-MM-dd')} + */ +public interface ${classInfo.className}Service { + + /** + * 新增 + */ + public Object insert(${classInfo.className} ${classInfo.className?uncap_first}); + + /** + * 删除 + */ + public Object delete(int id); + + /** + * 更新 + */ + public Object update(${classInfo.className} ${classInfo.className?uncap_first}); + + /** + * 根据主键 id 查询 + */ + public ${classInfo.className} load(int id); + +/** +* 如果已经有这个id的,就更新,不然是插入 +*/ +Object save(${classInfo.className} ${classInfo.className?uncap_first}); + + /** + * 分页查询 + */ + public Map pageList(int offset, int pageSize); + +} diff --git a/generator-web/src/main/resources/templates/code-generator/mybatis-starp/service_impl-starp.ftl b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/service_impl-starp.ftl new file mode 100644 index 0000000000000000000000000000000000000000..c93f5eaf848055dc8ba7713d91aa0801ba671c15 --- /dev/null +++ b/generator-web/src/main/resources/templates/code-generator/mybatis-starp/service_impl-starp.ftl @@ -0,0 +1,86 @@ +<#if isAutoImport?exists && isAutoImport==true> +import org.springframework.stereotype.Service; +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @description ${classInfo.classComment} + * @author ${authorName} + * @date ${.now?string('yyyy-MM-dd')} + */ +@Service +public class ${classInfo.className}ServiceImpl implements ${classInfo.className}Service { + + @Resource + private ${classInfo.className}Mapper ${classInfo.className?uncap_first}Mapper; + + + @Override + public Object insert(${classInfo.className} ${classInfo.className?uncap_first}) { + + // valid + if (${classInfo.className?uncap_first} == null) { + return ${returnUtilFailure}("必要参数缺失"); + } + + ${classInfo.className?uncap_first}Mapper.insert(${classInfo.className?uncap_first}); + return ${returnUtilSuccess}(); + } + + + @Override + public Object delete(Integer id) { + int ret = ${classInfo.className?uncap_first}Mapper.delete(id); + return ret>0?${returnUtilSuccess}():${returnUtilFailure}(); + } + + + @Override + public Object update(${classInfo.className} ${classInfo.className?uncap_first}) { + int ret = ${classInfo.className?uncap_first}Mapper.update(${classInfo.className?uncap_first}); + return ret>0?${returnUtilSuccess}():${returnUtilFailure}(); + } + + + @Override + public ${classInfo.className} getById(Integer id) { + return ${classInfo.className?uncap_first}Mapper.getById(id); + } + +@Override +public Object save(${classInfo.className} ${classInfo.className?uncap_first}) { +Integer id = ${classInfo.className?uncap_first}.getId(); +if (id==null) { +int insert = ${classInfo.className?uncap_first}Mapper.insert(${classInfo.className?uncap_first}); +return ReturnT.success(); +} +${classInfo.className} load = ${classInfo.className?uncap_first}Mapper.getById(${classInfo.className?uncap_first}.getId()); +if (load==null) { +int insert = ${classInfo.className?uncap_first}Mapper.insert(${classInfo.className?uncap_first}); + +return ReturnT.success(); +} +${classInfo.className?uncap_first}Mapper.update(${classInfo.className?uncap_first}); +return ReturnT.success(); +} + + + @Override + public Map pageList(int offset, int pageSize) { + + List<${classInfo.className}> pageList = ${classInfo.className?uncap_first}Mapper.pageList(offset, pageSize); +Integer totalCount = ${classInfo.className?uncap_first}Mapper.pageListCount(offset, pageSize); + + // result + Map result = new HashMap(); + //result.put("pageList", pageList); + result.put("content", pageList); + result.put("totalCount", totalCount); + + return result; + } + +} diff --git a/generator-web/src/main/resources/templates/code-generator/ui/element-ui.ftl b/generator-web/src/main/resources/templates/code-generator/ui/element-ui.ftl index 9f9e224d2be8ecc9c8d8b921fd8997861fbf13ac..69ac289e43b76a6ff8db9101a10f879035b73d49 100644 --- a/generator-web/src/main/resources/templates/code-generator/ui/element-ui.ftl +++ b/generator-web/src/main/resources/templates/code-generator/ui/element-ui.ftl @@ -1,6 +1,8 @@ + + + + + +