diff --git a/build.gradle b/build.gradle index e95fe76..55ddcc8 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ apply plugin: 'java' group 'com.mininglamp.km.nebula.generator' -version '1.0.4' +version '1.0.5' repositories { mavenCentral() diff --git a/src/main/resources/template/controller.java.btl b/src/main/resources/template/controller.java.btl new file mode 100644 index 0000000..92b5afa --- /dev/null +++ b/src/main/resources/template/controller.java.btl @@ -0,0 +1,39 @@ +package ${package.Controller}; + + +import org.springframework.web.bind.annotation.RequestMapping; + +<% if(restControllerStyle){ %> +import org.springframework.web.bind.annotation.RestController; +<% }else{ %> +import org.springframework.stereotype.Controller; +<% } %> +<% if(isNotEmpty(superControllerClassPackage)){ %> +import ${superControllerClassPackage}; +<% } %> + +/** + *

+ * ${table.comment!} 前端控制器 + *

+ * + * @author ${author} + * @since ${date} + */ +<% if(restControllerStyle){ %> +@RestController +<% }else{ %> +@Controller +<% } %> +@RequestMapping("<% if(isNotEmpty(package.ModuleName)){ %>/${package.ModuleName}<% } %>/<% if(isNotEmpty(controllerMappingHyphenStyle)){ %>${controllerMappingHyphen}<% }else{ %>${table.entityPath}<% } %>") +<% if(kotlin){ %> +class ${table.controllerName}<% if(isNotEmpty(superControllerClass)){ %> : ${superControllerClass}()<% } %> +<% }else{ %> + <% if(isNotEmpty(superControllerClass)){ %> +public class ${table.controllerName} extends ${superControllerClass} { + <% }else{ %> +public class ${table.controllerName} { + <% } %> + +} +<% } %> diff --git a/src/main/resources/template/entity.java.btl b/src/main/resources/template/entity.java.btl new file mode 100644 index 0000000..853a671 --- /dev/null +++ b/src/main/resources/template/entity.java.btl @@ -0,0 +1,58 @@ +package ${package.Entity}; +<% for(pkg in table.importPackages){ %> +import ${pkg}; +<% } %> + +<% if(entityLombokModel){ %> +import lombok.Data; +import lombok.EqualsAndHashCode; +<% if(chainModel){ %> +import lombok.experimental.Accessors; +<% } %> +<% } %> + +/** + *

+ * ${table.comment!} + *

+ * + * @author ${author} + * @since ${date} + */ + +@Data +@Accessors(chain = true) +public class ${entity} implements Serializable { + +<% if(entitySerialVersionUID){ %> + private static final long serialVersionUID = 1L; +<% } %> + +<% /** ----------点id---------- **/ %> +<% if(table.isTag){ %> + /** + * vid + */ + private ${table.idType} id; +<% } %> + +<% /** ----------边id---------- **/ %> +<% if(table.isEdge){ %> + /** + * 起始点 + */ + private ${table.idType} src; + + /** + * 终止点 + */ + private ${table.idType} dst; +<% } %> + +<% /** -----------BEGIN 字段循环遍历----------- **/ %> +<% for(field in table.fields){ %> + private ${field.propertyType} ${field.propertyName}; + +<% } %> +<% /** -----------END 字段循环遍历----------- **/ %> +} diff --git a/src/main/resources/template/entity.kt.btl b/src/main/resources/template/entity.kt.btl new file mode 100644 index 0000000..cf923c2 --- /dev/null +++ b/src/main/resources/template/entity.kt.btl @@ -0,0 +1,124 @@ +package ${package.Entity} +<% for(pkg in table.importPackages){ %> +import ${pkg} +<% } %> +<% if(swagger2){ %> +import io.swagger.annotations.ApiModel +import io.swagger.annotations.ApiModelProperty +<% } %> +/** + *

+ * ${table.comment!} + *

+ * + * @author ${author} + * @since ${date} + */ +<% if(table.convert){ %> +@TableName("${table.name}") +<% } %> +<% if(swagger2){ %> +@ApiModel(value="${entity}对象", description="${table.comment!''}") +<% } %> +<% if(isNotEmpty(superEntityClass)){ %> +class ${entity} : ${superEntityClass}<% if(activeRecord){ %><${entity}><%}%>{ +<% }else if(activeRecord){ %> +class ${entity} : Model<${entity}> { +<% }else{ %> +class ${entity} : Serializable { +<% } %> + +<% /** -----------BEGIN 字段循环遍历----------- **/ %> +<% for(field in table.fields){ %> + <% + if(field.keyFlag){ + var keyPropertyName = field.propertyName; + } + %> + + <% if(isNotEmpty(field.comment)){ %> + <% if(swagger2){ %> + @ApiModelProperty(value = "${field.comment}") + <% }else{ %> + /** + * ${field.comment} + */ + <% } %> + <% } %> + <% if(field.keyFlag){ %> + <% + /*主键*/ + %> + <% if(field.keyIdentityFlag){ %> + @TableId(value = "${field.annotationColumnName}", type = IdType.AUTO) + <% }else if(isNotEmpty(idType)){ %> + @TableId(value = "${field.annotationColumnName}", type = IdType.${idType}) + <% }else if(field.convert){ %> + @TableId("${field.columnName}") + <% } %> + <% + /*普通字段*/ + %> + <% }else if(isNotEmpty(field.fill)){ %> + <% if(field.convert){ %> + @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill}) + <% }else{ %> + @TableField(fill = FieldFill.${field.fill}) + <% } %> + <% }else if(field.convert){ %> + @TableField("${field.annotationColumnName}") + <% } %> + <% + /*乐观锁注解*/ + %> + <% if(versionFieldName!'' == field.name){ %> + @Version + <% } %> + <% + /*逻辑删除注解*/ + %> + <% if(logicDeleteFieldName!'' == field.name){ %> + @TableLogic + <% } %> + <% if(field.propertyType == 'Integer'){ %> + var ${field.propertyName}: Int ? = null + <% }else{ %> + var ${field.propertyName}: ${field.propertyType} ? = null + <% } %> +<% } %> +<% /** -----------END 字段循环遍历----------- **/ %> + +<% if(entityColumnConstant){ %> + companion object { + <% for(field in table.fields){ %> + const val ${strutil.toUpperCase(field.name)} : String = "${field.name}" + <% } %> + } +<% } %> +<% if(activeRecord){ %> + @Override + override fun pkVal(): Serializable? { + <% if(isNotEmpty(keyPropertyName)){ %> + return this.${keyPropertyName} + <% }else{ %> + return null; + <% } %> + } + +<% } %> + +<% if(!entityLombokModel){ %> + @Override + override fun toString(): String { + return "${entity}{" + + <% for(field in table.fields){ %> + <% if(fieldLP.index==0){ %> + "${field.propertyName}=" + ${field.propertyName} + + <% }else{ %> + ", ${field.propertyName}=" + ${field.propertyName} + + <% } %> + <% } %> + "}" + } +<% } %> +} diff --git a/src/main/resources/template/mapper.java.btl b/src/main/resources/template/mapper.java.btl new file mode 100644 index 0000000..41eb3d5 --- /dev/null +++ b/src/main/resources/template/mapper.java.btl @@ -0,0 +1,105 @@ +package ${package.Mapper}; + +import ${package.Entity}.${entity}; +import org.apache.ibatis.annotations.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; + +/** + *

+ * ${table.comment!} Mapper 接口 + *

+ * + * @author ${author} + * @since ${date} + */ +@Repository +public interface ${table.mapperName} { + + /** + * mapper 插入操作 + * @param entity 插入实体 + * @return 是否成功 + */ + int insert(${entity} entity); + + /** + * mapper 更新操作 + * @param entity 更新实体 + * @return 是否成功 + */ + int update(${entity} entity); + + /** + * 批量插入 + * @param batch 实体列表 + * @return i + */ + int insertBatch(List<${entity}> batch); + + <% if(table.isTag) { %> + /** + * 根据id查询实体 + * @param id 实体id + * @return entity + */ + ${entity} select(${table.idType} id); + + /** + * 根据id查询实体 + * @param batch 实体id列表 + * @return entity list + */ + List<${entity}> selectBatch(List<${table.idType}> batch); + + /** + * 删除 + * @param id 实体列表id + * @return i + */ + int delete(${table.idType} id); + + /** + * 批量删除 + * @param batch 实体列表id + * @return i + */ + int deleteBatch(List<${table.idType}> batch); + + <% } %> + + <% if(table.isEdge) { %> + /** + * 根据起始点查询边属性 + * @param src 起始点id + * @param dst 终止点id + * @return entity + */ + ${entity} select(${table.idType} src, ${table.idType} dst); + + /** + * 根据起始点列表查询边属性 + * @param batch 实体id列表 + * @return entity list + */ + List<${entity}> selectBatch(List<${entity}> batch); + + /** + * 批量删除 + * @param src 边起点 + * @param dst 边终点 + * @return i + */ + int delete(${table.idType} src, ${table.idType} dst); + + /** + * 批量删除 + * @param batch 边列表 + * @return i + */ + int deleteBatch(List<${entity}> batch); + <% } %> + + +} diff --git a/src/main/resources/template/mapper.xml.btl b/src/main/resources/template/mapper.xml.btl new file mode 100644 index 0000000..ce332e2 --- /dev/null +++ b/src/main/resources/template/mapper.xml.btl @@ -0,0 +1,206 @@ + + + + +<% if(baseResultMap){ %> + + + <% if (table.isTag) { %> + + <% } %> + <% if (table.isEdge) { %> + + + <% } %> +<% for(field in table.fields){ %> + <% /** 生成主键排在第一位 **/ %> + <% if(field.keyFlag){ %> + + <% } %> +<% } %> +<% for(field in table.commonFields){ %> + <% /** 生成公共字段 **/ %> + +<% } %> +<% for(field in table.fields){ %> + <% /** 生成普通字段 **/ %> + <% if(!field.keyFlag){ %> + + <% } %> +<% } %> + +<% } %> + +<% if(baseColumnList){ %> + + +<% for(field in table.commonFields){ %> + ${field.columnName}, +<% } %> + ${table.fieldNames} + +<% } %> + + + + insert ${table.tableClass} `${table.name}` ( + + <% for (field in table.fields){%> + + ${field.name}, + + <%}%> + + <%if(table.isTag){%> + ) values #{id} :( + <%}%> + <%if(table.isEdge){%> + ) values #{src} -> #{dst} :( + <%}%> + + <% for (field in table.fields){%> + + #{${field.propertyName}}, + + <% } %> + + ) + + + + + insert ${table.tableClass} `${table.name}` + + <% for (field in table.fields) { %> + ${field.name}, + <% } %> + + values + + <% if (table.isTag) { %> + #{item.id} : + <% } %> + <% if (table.isEdge) { %> + #{item.src} -> #{item.dst} : + <% } %> + + <% for (field in table.fields) { %> + #{item.${field.propertyName}}, + <% } %> + + + + + + + UPDATE ${table.tableClass} ON `${table.name}` + <%if(table.isTag){%> + #{id} + <%}%> + <%if(table.isEdge){%> + #{src} -> #{dst} + <%}%> + + <% for (field in table.fields){%> + + ${field.name} = #{${field.propertyName}}, + + <%}%> + + + + + <% if(table.isTag) { %> + + + + + + <% } %> + + + <% if (table.isEdge) { %> + + + + + + <% } %> + + + + + <% if (table.isTag) { %> + delete vertex #{id} + <% } %> + <% if (table.isEdge) { %> + delete edge `${table.name}` #{src} -> #{dst} + <% } %> + + + + + parameterType="java.lang.String"> + <% } %> + <% if (table.isEdge) { %> + parameterType="${package.Entity}.${entity}"> + <% } %> + <% if (table.isTag) { %> + delete vertex + + #{item} + + <% } %> + <% if (table.isEdge) { %> + delete edge `${table.name}` + + #{item.src} -> #{item.dst} + + <% } %> + + + + diff --git a/src/main/resources/template/path.entity.java.btl b/src/main/resources/template/path.entity.java.btl new file mode 100644 index 0000000..d010d2e --- /dev/null +++ b/src/main/resources/template/path.entity.java.btl @@ -0,0 +1,49 @@ +package ${a.packagePath}; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.List; + +/** + * 查询边路径 + * @author ${author} + * @since ${date} + */ + +@Data +@EqualsAndHashCode +@Accessors(chain = true) +public class ${a.fileName} implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 起始点 + */ + private String startNode; + + /** + * 终止点 + */ + private String endNode; + /** + * 所有节点 + */ + private List nodes; + /** + * 所有边关系 + */ + private List relationShips; + + + @Data + public static class RelationShip { + public String src; + public String dst; + public long ranking; + } + +} diff --git a/src/main/resources/template/path.mapper.java.btl b/src/main/resources/template/path.mapper.java.btl new file mode 100644 index 0000000..6d2f9ae --- /dev/null +++ b/src/main/resources/template/path.mapper.java.btl @@ -0,0 +1,22 @@ +package ${e.packagePath}; + +import org.springframework.stereotype.Repository; +import ${a.packagePath}.${a.fileName}; + +import java.util.List; + +/** + * 查询路径接口 + * + * @author ${author} + * @since ${date} + */ +@Repository +public interface ${e.fileName} { + + /** + * 查询两点之间的路径 + */ + List<${a.fileName}> selectPath(${idType} src, ${idType} dst); + +} diff --git a/src/main/resources/template/path.mapper.xml.btl b/src/main/resources/template/path.mapper.xml.btl new file mode 100644 index 0000000..2fb2273 --- /dev/null +++ b/src/main/resources/template/path.mapper.xml.btl @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/template/path.relation.type.handler.btl b/src/main/resources/template/path.relation.type.handler.btl new file mode 100644 index 0000000..a1725fe --- /dev/null +++ b/src/main/resources/template/path.relation.type.handler.btl @@ -0,0 +1,84 @@ +package ${c.packagePath}; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.TypeHandler; + +import ${a.packagePath}.${a.fileName}; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +/** + * @author ${author} + */ +public class RelationTypeHandler implements TypeHandler> { + private static final ObjectMapper OBJECT_MAPPER; + + static { + OBJECT_MAPPER = new ObjectMapper(); + //在反序列化时忽略在 json 中存在但 Java 对象不存在的属性 + OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + //在序列化时日期格式默认为 yyyy-MM-dd'T'HH:mm:ss.SSSZ + OBJECT_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); + //在序列化时忽略值为 null 的属性 + OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + + + @Override + public void setParameter(PreparedStatement ps, int i, List<${a.fileName}.RelationShip> parameter, JdbcType jdbcType) throws SQLException { + + } + + @Override + public List<${a.fileName}.RelationShip> getResult(ResultSet rs, String columnName) throws SQLException { + String object = rs.getString(columnName); + try { + ObjectMapper objectMapper = new ObjectMapper(); + List<${a.fileName}.RelationShip> relationShip = OBJECT_MAPPER.readValue(object, new TypeReference>() { + }); + return relationShip; + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return new ArrayList<>(); + } + + @Override + public List<${a.fileName}.RelationShip> getResult(ResultSet rs, int columnIndex) throws SQLException { + String object = rs.getString(columnIndex); + try { + List<${a.fileName}.RelationShip> relationShip = OBJECT_MAPPER.readValue(object, new TypeReference>() { + }); + return relationShip; + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return new ArrayList<>(); + } + + @Override + public List<${a.fileName}.RelationShip> getResult(CallableStatement cs, int columnIndex) throws SQLException { + String object = cs.getString(columnIndex); + try { + List<${a.fileName}.RelationShip> relationShip = OBJECT_MAPPER.readValue(object, new TypeReference>() { + }); + return relationShip; + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return new ArrayList<>(); + } +} diff --git a/src/main/resources/template/path.string.list.type.handler.btl b/src/main/resources/template/path.string.list.type.handler.btl new file mode 100644 index 0000000..60d8f67 --- /dev/null +++ b/src/main/resources/template/path.string.list.type.handler.btl @@ -0,0 +1,79 @@ +package ${d.packagePath}; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.TypeHandler; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +/** + * @author ${author} + */ +public class StringListTypeHandler implements TypeHandler> { + private static final ObjectMapper OBJECT_MAPPER; + + static { + OBJECT_MAPPER = new ObjectMapper(); + //在反序列化时忽略在 json 中存在但 Java 对象不存在的属性 + OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + //在序列化时日期格式默认为 yyyy-MM-dd'T'HH:mm:ss.SSSZ + OBJECT_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); + //在序列化时忽略值为 null 的属性 + OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + @Override + public void setParameter(PreparedStatement ps, int i, List parameter, JdbcType jdbcType) throws SQLException { + + } + + @Override + public List getResult(ResultSet rs, String columnName) throws SQLException { + String object = rs.getString(columnName); + try { + List relationShip = OBJECT_MAPPER.readValue(object, new TypeReference>() { + }); + return relationShip; + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return new ArrayList<>(); + } + + @Override + public List getResult(ResultSet rs, int columnIndex) throws SQLException { + String object = rs.getString(columnIndex); + try { + List relationShip = OBJECT_MAPPER.readValue(object, new TypeReference>() { + }); + return relationShip; + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return new ArrayList<>(); + } + + @Override + public List getResult(CallableStatement cs, int columnIndex) throws SQLException { + String object = cs.getString(columnIndex); + try { + List relationShip = OBJECT_MAPPER.readValue(object, new TypeReference>() { + }); + return relationShip; + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return new ArrayList<>(); + } +} diff --git a/src/main/resources/template/service.java.btl b/src/main/resources/template/service.java.btl new file mode 100644 index 0000000..c3f6388 --- /dev/null +++ b/src/main/resources/template/service.java.btl @@ -0,0 +1,20 @@ +package ${package.Service}; + +import ${package.Entity}.${entity}; +import ${superServiceClassPackage}; + +/** + *

+ * ${table.comment!} 服务类 + *

+ * + * @author ${author} + * @since ${date} + */ +<% if(kotlin){ %> +interface ${table.serviceName} : ${superServiceClass}<${entity}> +<% }else{ %> +public interface ${table.serviceName} extends ${superServiceClass}<${entity}> { + +} +<% } %> diff --git a/src/main/resources/template/serviceImpl.java.btl b/src/main/resources/template/serviceImpl.java.btl new file mode 100644 index 0000000..9a62911 --- /dev/null +++ b/src/main/resources/template/serviceImpl.java.btl @@ -0,0 +1,26 @@ +package ${package.ServiceImpl}; + +import ${package.Entity}.${entity}; +import ${package.Mapper}.${table.mapperName}; +import ${package.Service}.${table.serviceName}; +import ${superServiceImplClassPackage}; +import org.springframework.stereotype.Service; + +/** + *

+ * ${table.comment!} 服务实现类 + *

+ * + * @author ${author} + * @since ${date} + */ +@Service +<% if(kotlin){ %> +open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} { + +} +<% }else{ %> +public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} { + +} +<% } %>