diff --git a/src/main/java/com/youlai/boot/codegen/freemarker/CustomFreemarkerTemplateEngine.java b/src/main/java/com/youlai/boot/codegen/freemarker/CustomFreemarkerTemplateEngine.java new file mode 100644 index 0000000..053eeda --- /dev/null +++ b/src/main/java/com/youlai/boot/codegen/freemarker/CustomFreemarkerTemplateEngine.java @@ -0,0 +1,75 @@ +package com.youlai.boot.codegen.freemarker; + +import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder; +import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine; +import freemarker.template.Configuration; +import freemarker.template.Template; + +import java.io.*; +import java.util.Map; + +public class CustomFreemarkerTemplateEngine extends AbstractTemplateEngine { + + private final Configuration cfg = new Configuration(Configuration.VERSION_2_3_34); + + /** + * ⭐ 模板基础路径(核心) + */ + private final String templateBasePath; + + /** + * ⭐ 构造器注入(你想要的方式) + */ + public CustomFreemarkerTemplateEngine(String templateBasePath) { + if (templateBasePath.endsWith("/")) { + this.templateBasePath = templateBasePath; + } else { + this.templateBasePath = templateBasePath + "/"; + } + } + + @Override + public CustomFreemarkerTemplateEngine init(ConfigBuilder configBuilder) { + + cfg.setClassLoaderForTemplateLoading( + getClass().getClassLoader(), + "templates" + ); + + cfg.setDefaultEncoding("UTF-8"); + cfg.setNumberFormat("0.#################"); + + return this; + } + + @Override + public void writer(Map objectMap, + String templatePath, + File outputFile) throws Exception { + + // MP 默认带 /templates/ + String realTemplate = templatePath; + + if (realTemplate.startsWith("/templates/")) { + realTemplate = realTemplate.substring("/templates/".length()); + } + + // ⭐ 拼接最终路径 + String finalTemplate = templateBasePath + realTemplate + ".ftl"; + + Template template = cfg.getTemplate(finalTemplate); + + if (outputFile.getParentFile() != null) { + outputFile.getParentFile().mkdirs(); + } + + try (Writer writer = new OutputStreamWriter(new FileOutputStream(outputFile))) { + template.process(objectMap, writer); + } + } + + @Override + public String templateFilePath(String fileName) { + return fileName; + } +} diff --git a/src/main/java/com/youlai/boot/codegen/MyBatisPlusGenerator.java b/src/main/java/com/youlai/boot/codegen/freemarker/MyBatisPlusGenerator.java similarity index 95% rename from src/main/java/com/youlai/boot/codegen/MyBatisPlusGenerator.java rename to src/main/java/com/youlai/boot/codegen/freemarker/MyBatisPlusGenerator.java index 764812c..fc76305 100644 --- a/src/main/java/com/youlai/boot/codegen/MyBatisPlusGenerator.java +++ b/src/main/java/com/youlai/boot/codegen/freemarker/MyBatisPlusGenerator.java @@ -1,4 +1,4 @@ -package com.youlai.boot.codegen; +package com.youlai.boot.codegen.freemarker; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.generator.FastAutoGenerator; @@ -199,15 +199,14 @@ public class MyBatisPlusGenerator { }) // 模板(只保留 entity) + // 关键点1:只指定文件名 .templateConfig(builder -> builder - .entity("templates/codegen/ftl/entity.java") - .disable( - com.baomidou.mybatisplus.generator.config.TemplateType.SERVICE, - com.baomidou.mybatisplus.generator.config.TemplateType.SERVICE_IMPL, - com.baomidou.mybatisplus.generator.config.TemplateType.CONTROLLER - ) + .entity("entity.java") ) + // 使用你的自定义引擎 + .templateEngine(new CustomFreemarkerTemplateEngine("codegen/ftl")) + .execute(); } diff --git a/src/main/java/com/youlai/boot/common/handler/PointTypeHandler.java b/src/main/java/com/youlai/boot/common/handler/PointTypeHandler.java new file mode 100644 index 0000000..ad694c2 --- /dev/null +++ b/src/main/java/com/youlai/boot/common/handler/PointTypeHandler.java @@ -0,0 +1,59 @@ +package com.youlai.boot.common.handler; + +import org.apache.ibatis.type.BaseTypeHandler; +import org.apache.ibatis.type.JdbcType; +import org.locationtech.jts.geom.Point; +import org.locationtech.jts.io.ParseException; +import org.locationtech.jts.io.WKBReader; +import org.locationtech.jts.io.WKBWriter; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class PointTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, Point parameter, JdbcType jdbcType) + throws SQLException { + if (parameter.getSRID() == 0) { + parameter.setSRID(4326); // 非常关键! + } + WKBWriter writer = new WKBWriter(); + byte[] bytes = writer.write(parameter); + ps.setBytes(i, bytes); + } + + @Override + public Point getNullableResult(ResultSet rs, String columnName) throws SQLException { + byte[] bytes = rs.getBytes(columnName); + return toPoint(bytes); + } + + @Override + public Point getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + byte[] bytes = rs.getBytes(columnIndex); + return toPoint(bytes); + } + + @Override + public Point getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + byte[] bytes = cs.getBytes(columnIndex); + return toPoint(bytes); + } + + private Point toPoint(byte[] bytes) { + if (bytes == null) { + return null; + } + try { + WKBReader reader = new WKBReader(); + return (Point) reader.read(bytes); + } catch (ParseException e) { + throw new RuntimeException("Failed to parse WKB for Point", e); + } + } +} + + diff --git a/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalMapper.java b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalMapper.java new file mode 100644 index 0000000..9ee8c74 --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/mapper/MiniStrayAnimalMapper.java @@ -0,0 +1,14 @@ +package com.youlai.boot.mini.mapper; + +import com.youlai.boot.mini.model.entity.MiniStrayAnimal; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** +* 流浪动物基础信息表 Mapper 接口 +* +* @author jwy +* @since +*/ +public interface MiniStrayAnimalMapper extends BaseMapper { + +} diff --git a/src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimal.java b/src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimal.java new file mode 100644 index 0000000..dcf4e9e --- /dev/null +++ b/src/main/java/com/youlai/boot/mini/model/entity/MiniStrayAnimal.java @@ -0,0 +1,125 @@ +package com.youlai.boot.mini.model.entity; + +import com.baomidou.mybatisplus.annotation.*; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; +import com.fasterxml.jackson.annotation.JsonFormat; +import org.locationtech.jts.geom.Point; +import com.youlai.boot.common.handler.PointTypeHandler; + +@Getter +@Setter +@ToString +@Accessors(chain = true) +@TableName("mini_stray_animal") +@Schema(description = "流浪动物基础信息表") +public class MiniStrayAnimal implements Serializable { + + @TableId(value = "id", type = IdType.AUTO) + @Schema(description = "动物ID") + private Long id; + + + @TableField("uuid") + @Schema(description = "uuid唯一标识,前后端用这个进行数据交互") + private String uuid; + + @TableField("mini_user_id") + @Schema(description = "发布用户ID") + private Long miniUserId; + + @TableField("animal_type") + @Schema(description = "动物类型,cat-猫,dog-狗,other-其他") + private String animalType; + + @TableField("color") + @Schema(description = "颜色") + private String color; + + @TableField("size") + @Schema(description = "体型大小,small-小,medium-中等,large-大") + private String size; + + @TableField("status") + @Schema(description = "状态,found-发现,adopted-已被领养,missing-失踪,reviewing-审核中") + private String status; + + @TableField("adopted_by") + @Schema(description = "领养人ID") + private Long adoptedBy; + + @TableField("adopted_at") + @Schema(description = "领养时间") + private Long adoptedAt; + + @TableField("audit_status") + @Schema(description = "审核状态:0-通过,1-审核中,2-审核未通过") + private Integer auditStatus; + + @TableField("province") + @Schema(description = "所在省") + private String province; + + @TableField("city") + @Schema(description = "所在市") + private String city; + + @TableField("district") + @Schema(description = "所在区、县") + private String district; + + @TableField("address") + @Schema(description = "完整详细地址,含省市区") + private String address; + + @TableField(value = "gd_location_point", typeHandler = PointTypeHandler.class) + @Schema(description = "发现地点坐标 [高德地图(GCJ-02坐标系)]") + private Point gdLocationPoint; + + @TableField(value = "wgs84_location_point", typeHandler = PointTypeHandler.class) + @Schema(description = "发现地点坐标 [转化成WGS-84原始坐标系],用于附近功能(计算距离)查找") + private Point wgs84LocationPoint; + + @TableField("wgs84_geohash") + @Schema(description = "用于区域快速查询") + private String wgs84Geohash; + + @TableField("create_time") + @Schema(description = "创建时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createTime; + + @TableField("create_timestamp") + @Schema(description = "创建时间毫秒级时间戳") + private Long createTimestamp; + + @TableField("create_by") + @Schema(description = "创建人ID") + private Long createBy; + + @TableField("update_time") + @Schema(description = "更新时间") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updateTime; + + @TableField("update_timestamp") + @Schema(description = "更新时间毫秒级时间戳") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date updateTimestamp; + + @TableField("update_by") + @Schema(description = "修改人ID") + private Long updateBy; + + @TableField("is_deleted") + @Schema(description = "逻辑删除标识(0-未删除 1-已删除)") + private Boolean deleted; + + +} diff --git a/src/main/resources/mapper/mini/MiniStrayAnimalMapper.xml b/src/main/resources/mapper/mini/MiniStrayAnimalMapper.xml new file mode 100644 index 0000000..11e5c3e --- /dev/null +++ b/src/main/resources/mapper/mini/MiniStrayAnimalMapper.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/src/main/resources/templates/codegen/ftl/entity.java.ftl b/src/main/resources/templates/codegen/ftl/entity.java.ftl index 82d1658..1fcdaa4 100644 --- a/src/main/resources/templates/codegen/ftl/entity.java.ftl +++ b/src/main/resources/templates/codegen/ftl/entity.java.ftl @@ -20,7 +20,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; <#list table.fields as field> <#if field.propertyType == "Point"> import org.locationtech.jts.geom.Point; -import com.pet.map.back.handler.PointTypeHandler; +import com.youlai.boot.common.handler.PointTypeHandler; <#break> @@ -48,7 +48,7 @@ public class ${entity} implements Serializable { @TableId(value = "${field.name}", type = IdType.${idType!"ASSIGN_ID"}) @Schema(description = "${field.comment!}") <#if field.propertyType == "LocalDate"> - JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") + @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") private Date ${field.propertyName}; <#elseif field.propertyType == "LocalDateTime"> @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") diff --git a/src/main/resources/templates/codegen/ftl/mapper.java.ftl b/src/main/resources/templates/codegen/ftl/mapper.java.ftl new file mode 100644 index 0000000..dc86b12 --- /dev/null +++ b/src/main/resources/templates/codegen/ftl/mapper.java.ftl @@ -0,0 +1,14 @@ +package ${package.Mapper}; + +import ${package.Entity}.${entity}; +import ${superMapperClassPackage}; + +/** +* ${table.comment!} Mapper 接口 +* +* @author ${author} +* @since ${date} +*/ +public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { + +} diff --git a/src/main/resources/templates/codegen/ftl/mapper.xml.ftl b/src/main/resources/templates/codegen/ftl/mapper.xml.ftl new file mode 100644 index 0000000..a3aebb9 --- /dev/null +++ b/src/main/resources/templates/codegen/ftl/mapper.xml.ftl @@ -0,0 +1,9 @@ + + + + + + +