9 changed files with 313 additions and 9 deletions
@ -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<String, Object> 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; |
|||
} |
|||
} |
|||
@ -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<Point> { |
|||
|
|||
@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); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
@ -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<MiniStrayAnimal> { |
|||
|
|||
} |
|||
@ -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; |
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE mapper |
|||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
|||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
|
|||
<mapper namespace="com.youlai.boot.mini.mapper.MiniStrayAnimalMapper"> |
|||
|
|||
|
|||
</mapper> |
|||
@ -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}> { |
|||
|
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE mapper |
|||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
|||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
|
|||
<mapper namespace="${package.Mapper}.${table.mapperName}"> |
|||
|
|||
|
|||
</mapper> |
|||
Loading…
Reference in new issue