refactor(database): 更新数据库配置和实体映射

- 修改数据库连接URL地址
- 为所有实体类添加@TableField注解映射数据库字段
- 使用反引号标识符包裹表名和字段名
- 更新SQL查询语句使用明确字段列表
- 在配置文件中启用MyBatis安全模式防止SQL注入
- 添加MarkdownFileVO中groupingName字段的exist = false标识
This commit is contained in:
ikmkj
2026-01-08 19:44:22 +08:00
parent 363918b3f7
commit 95393ab517
14 changed files with 93 additions and 38 deletions

View File

@@ -14,11 +14,12 @@ import java.util.Date;
@Data @Data
@Schema(name = "分组实体") @Schema(name = "分组实体")
@TableName("grouping") @TableName("`grouping`")
public class Grouping implements Serializable { public class Grouping implements Serializable {
@Schema(description = "分组id",implementation = Long.class) @Schema(description = "分组id",implementation = Long.class)
@TableId(type = IdType.ASSIGN_ID) @TableId(type = IdType.ASSIGN_ID)
@JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段 @JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段
@TableField("id")
private Long id; private Long id;
@Schema(description ="上级id",implementation = Long.class) @Schema(description ="上级id",implementation = Long.class)
@@ -27,15 +28,19 @@ public class Grouping implements Serializable {
private Long parentId; private Long parentId;
@Schema(description = "分组名称",implementation = String.class) @Schema(description = "分组名称",implementation = String.class)
@TableField("`grouping`")
private String grouping; private String grouping;
@Schema(description = "是否删除 0-未删除 1-已删除", implementation = Integer.class) @Schema(description = "是否删除 0-未删除 1-已删除", implementation = Integer.class)
@TableLogic @TableLogic
@TableField("is_deleted")
private Integer isDeleted; private Integer isDeleted;
@Schema(description = "删除时间", implementation = Date.class) @Schema(description = "删除时间", implementation = Date.class)
@TableField("deleted_at")
private Date deletedAt; private Date deletedAt;
@Schema(description = "删除人ID", implementation = Long.class) @Schema(description = "删除人ID", implementation = Long.class)
@TableField("deleted_by")
private Long deletedBy; private Long deletedBy;
} }

View File

@@ -12,13 +12,16 @@ import java.util.Date;
@Data @Data
@Schema(name = "图片实体") @Schema(name = "图片实体")
@TableName("image") @TableName("`image`")
public class Image { public class Image {
@Schema(description = "图片id",implementation = Long.class) @Schema(description = "图片id",implementation = Long.class)
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
@JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段 @JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段
@TableField("id")
private Long id; private Long id;
@Schema(description = " 外键关联Markdown文件ID标识图片所属文档",implementation = Long.class ) @Schema(description = " 外键关联Markdown文件ID标识图片所属文档",implementation = Long.class )
@TableField("markdown_id")
private Long markdownId; private Long markdownId;
@Schema(description = "原始文件名(用户上传时的文件名)",implementation = String.class ) @Schema(description = "原始文件名(用户上传时的文件名)",implementation = String.class )
@@ -30,8 +33,11 @@ public class Image {
private String storedName; private String storedName;
@Schema(description = " 图片访问URL",implementation = String.class ) @Schema(description = " 图片访问URL",implementation = String.class )
@TableField("`url`")
private String url; private String url;
@Schema(description = "图片大小(字节)",implementation = Long.class ) @Schema(description = "图片大小(字节)",implementation = Long.class )
@TableField("`size`")
private Long size; private Long size;
@Schema(description = "图片MIME类型如image/jpeg",implementation = String.class ) @Schema(description = "图片MIME类型如image/jpeg",implementation = String.class )
@@ -39,5 +45,6 @@ public class Image {
private String contentType; private String contentType;
@Schema(description = "图片上传时间",implementation = Date.class ) @Schema(description = "图片上传时间",implementation = Date.class )
@TableField("created_at")
private Date createdAt; private Date createdAt;
} }

View File

@@ -7,11 +7,12 @@ import lombok.Data;
@Data @Data
@Schema(name = "图片名称实体") @Schema(name = "图片名称实体")
@TableName("image_name") @TableName("`image_name`")
public class ImageName { public class ImageName {
@Schema(description = "图片名称id", implementation = Long.class) @Schema(description = "图片名称id", implementation = Long.class)
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
@JsonFormat(shape = JsonFormat.Shape.STRING) @JsonFormat(shape = JsonFormat.Shape.STRING)
@TableField("id")
private Long id; private Long id;
@Schema(description = "关联的Markdown文件ID", implementation = Long.class) @Schema(description = "关联的Markdown文件ID", implementation = Long.class)
@@ -21,4 +22,4 @@ public class ImageName {
@Schema(description = "文件名", implementation = String.class) @Schema(description = "文件名", implementation = String.class)
@TableField("file_name") @TableField("file_name")
private String fileName; private String fileName;
} }

View File

@@ -14,15 +14,20 @@ import java.util.Date;
@Data @Data
@Schema(name = "文本实体") @Schema(name = "文本实体")
@TableName("markdown_file") @TableName("`markdown_file`")
public class MarkdownFile implements Serializable { public class MarkdownFile implements Serializable {
@Schema(description = "文本id",implementation = Long.class) @Schema(description = "文本id",implementation = Long.class)
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
@JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段 @JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段
@TableField("id")
private Long id; private Long id;
@Schema(description = "分组表id",implementation = Long.class) @Schema(description = "分组表id",implementation = Long.class)
@TableField("grouping_id")
private Long groupingId; private Long groupingId;
@Schema(description = "文本标题",implementation = String.class) @Schema(description = "文本标题",implementation = String.class)
@TableField("`title`")
private String title; private String title;
@Schema(description = " 文件名(带.md后缀用于存储和识别",implementation = String.class) @Schema(description = " 文件名(带.md后缀用于存储和识别",implementation = String.class)
@@ -30,22 +35,31 @@ public class MarkdownFile implements Serializable {
private String fileName; private String fileName;
@Schema(description = "Markdown内容存储实际文档内容",implementation = String.class) @Schema(description = "Markdown内容存储实际文档内容",implementation = String.class)
@TableField("`content`")
private String content; private String content;
@Schema(description = "创建时间",implementation = Date.class) @Schema(description = "创建时间",implementation = Date.class)
@TableField("created_at")
private Date createdAt; private Date createdAt;
@Schema(description = "更新时间",implementation = Date.class) @Schema(description = "更新时间",implementation = Date.class)
@TableField("updated_at")
private Date updatedAt; private Date updatedAt;
@Schema(description = "是否删除 0-未删除 1-已删除", implementation = Integer.class) @Schema(description = "是否删除 0-未删除 1-已删除", implementation = Integer.class)
@TableLogic @TableLogic
@TableField("is_deleted")
private Integer isDeleted; private Integer isDeleted;
@Schema(description = "删除时间", implementation = Date.class) @Schema(description = "删除时间", implementation = Date.class)
@TableField("deleted_at")
private Date deletedAt; private Date deletedAt;
@Schema(description = "删除人ID", implementation = Long.class) @Schema(description = "删除人ID", implementation = Long.class)
@TableField("deleted_by")
private Long deletedBy; private Long deletedBy;
@Schema(description = "是否私密 0-公开 1-私密", implementation = Integer.class) @Schema(description = "是否私密 0-公开 1-私密", implementation = Integer.class)
@TableField("is_private")
private Integer isPrivate; private Integer isPrivate;
} }

View File

@@ -1,10 +1,12 @@
package com.test.bijihoudaun.entity; package com.test.bijihoudaun.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class MarkdownFileVO extends MarkdownFile { public class MarkdownFileVO extends MarkdownFile {
@TableField(exist = false)
private String groupingName; private String groupingName;
} }

View File

@@ -1,6 +1,7 @@
package com.test.bijihoudaun.entity; package com.test.bijihoudaun.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@@ -9,7 +10,7 @@ import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
@Data @Data
@TableName("registration_codes") @TableName("`registration_codes`")
@Schema(description = "注册码实体") @Schema(description = "注册码实体")
public class RegistrationCode implements Serializable { public class RegistrationCode implements Serializable {
@@ -17,17 +18,22 @@ public class RegistrationCode implements Serializable {
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
@Schema(description = "主键ID", name = "id") @Schema(description = "主键ID", name = "id")
@TableField("id")
private Long id; private Long id;
@Schema(description = "注册码", name = "code") @Schema(description = "注册码", name = "code")
@TableField("`code`")
private String code; private String code;
@Schema(description = "过期时间", name = "expiryTime") @Schema(description = "过期时间", name = "expiryTime")
@TableField("expiry_time")
private String expiryTime; private String expiryTime;
@Schema(description = "创建者", name = "createdBy") @Schema(description = "创建者", name = "createdBy")
@TableField("created_by")
private String createdBy; private String createdBy;
@Schema(description = "创建时间", name = "createdAt") @Schema(description = "创建时间", name = "createdAt")
@TableField("created_at")
private String createdAt; private String createdAt;
} }

View File

@@ -1,5 +1,6 @@
package com.test.bijihoudaun.entity; package com.test.bijihoudaun.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@@ -8,7 +9,7 @@ import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
@Data @Data
@TableName("system_settings") @TableName("`system_settings`")
@Schema(description = "系统设置实体") @Schema(description = "系统设置实体")
public class SystemSetting implements Serializable { public class SystemSetting implements Serializable {
@@ -16,11 +17,14 @@ public class SystemSetting implements Serializable {
@TableId @TableId
@Schema(description = "设置键", name = "settingKey") @Schema(description = "设置键", name = "settingKey")
@TableField("`setting_key`")
private String settingKey; private String settingKey;
@Schema(description = "设置值", name = "settingValue") @Schema(description = "设置值", name = "settingValue")
@TableField("`setting_value`")
private String settingValue; private String settingValue;
@Schema(description = "设置描述", name = "description") @Schema(description = "设置描述", name = "description")
@TableField("`description`")
private String description; private String description;
} }

View File

@@ -1,6 +1,7 @@
package com.test.bijihoudaun.entity; package com.test.bijihoudaun.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
@@ -11,24 +12,39 @@ import java.util.Date;
@Data @Data
@Schema(name = "用户实体") @Schema(name = "用户实体")
@TableName("user") @TableName("`user`")
public class User { public class User {
@Schema(description = "用户id",implementation = Long.class) @Schema(description = "用户id",implementation = Long.class)
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
@JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段 @JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段
@TableField("id")
private Long id; private Long id;
@Schema(description = "用户名",implementation = String.class) @Schema(description = "用户名",implementation = String.class)
@TableField("`username`")
private String username; private String username;
@Schema(description = "密码",implementation = String.class) @Schema(description = "密码",implementation = String.class)
@TableField("`password`")
private String password; private String password;
@Schema(description = "邮箱",implementation = String.class) @Schema(description = "邮箱",implementation = String.class)
@TableField("`email`")
private String email; private String email;
@Schema(description = "用户创建时间",implementation = Date.class) @Schema(description = "用户创建时间",implementation = Date.class)
@TableField("created_at")
private Date createdAt; private Date createdAt;
@Schema(description = "用户更新时间",implementation = Date.class) @Schema(description = "用户更新时间",implementation = Date.class)
@TableField("updated_at")
private Date updatedAt; private Date updatedAt;
@Schema(description = "用户token",implementation = String.class) @Schema(description = "用户token",implementation = String.class)
@TableField("`token`")
private String token; private String token;
@Schema(description = "用户token过期时间",implementation = Date.class) @Schema(description = "用户token过期时间",implementation = Date.class)
@TableField("token_enddata")
private Date tokenEnddata; private Date tokenEnddata;
} }

View File

@@ -4,8 +4,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.bijihoudaun.entity.Grouping; import com.test.bijihoudaun.entity.Grouping;
import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import org.apache.ibatis.annotations.Update;
@@ -15,14 +13,14 @@ import java.util.List;
@Mapper @Mapper
public interface GroupingMapper extends BaseMapper<Grouping> { public interface GroupingMapper extends BaseMapper<Grouping> {
@Select("SELECT * FROM grouping WHERE is_deleted = 1") @Select("SELECT id, parentId, `grouping`, is_deleted, deleted_at, deleted_by FROM `grouping` WHERE is_deleted = 1")
List<Grouping> selectDeleted(); List<Grouping> selectDeleted();
@Delete("DELETE FROM grouping WHERE id = #{id}") @Delete("DELETE FROM `grouping` WHERE id = #{id}")
void physicalDeleteById(@Param("id") Long id); void physicalDeleteById(@Param("id") Long id);
@Update("UPDATE grouping SET is_deleted = 0, deleted_at = NULL, deleted_by = NULL WHERE id = #{id}") @Update("UPDATE `grouping` SET is_deleted = 0, deleted_at = NULL, deleted_by = NULL WHERE id = #{id}")
void restoreById(@Param("id") Long id); void restoreById(@Param("id") Long id);
@Delete("DELETE FROM grouping WHERE is_deleted = 1") @Delete("DELETE FROM `grouping` WHERE is_deleted = 1")
void physicalDeleteByIsDeleted(); void physicalDeleteByIsDeleted();
} }

View File

@@ -18,6 +18,6 @@ public interface ImageMapper extends BaseMapper<Image> {
* 获取所有图片记录 * 获取所有图片记录
* @return 所有图片列表 * @return 所有图片列表
*/ */
@Select("SELECT * FROM image") @Select("SELECT id, markdown_id, original_name, stored_name, `url`, `size`, content_type, created_at FROM `image`")
List<Image> findAll(); List<Image> findAll();
} }

View File

@@ -14,6 +14,6 @@ public interface ImageNameMapper extends BaseMapper<ImageName> {
* @param fileName 文件名 * @param fileName 文件名
* @return 删除的记录数 * @return 删除的记录数
*/ */
@Delete("DELETE FROM image_name WHERE file_name = #{fileName}") @Delete("DELETE FROM `image_name` WHERE file_name = #{fileName}")
int deleteByFileName(String fileName); int deleteByFileName(String fileName);
} }

View File

@@ -15,39 +15,39 @@ import org.apache.ibatis.annotations.Update;
@Mapper @Mapper
public interface MarkdownFileMapper extends BaseMapper<MarkdownFile> { public interface MarkdownFileMapper extends BaseMapper<MarkdownFile> {
@Select("SELECT mf.*, g.grouping as groupingName " + @Select("SELECT mf.id, mf.grouping_id, mf.`title`, mf.file_name, mf.`content`, mf.created_at, mf.updated_at, mf.is_deleted, mf.deleted_at, mf.deleted_by, mf.is_private, g.`grouping` as groupingName " +
"FROM markdown_file mf " + "FROM `markdown_file` mf " +
"LEFT JOIN grouping g ON mf.grouping_id = g.id " + "LEFT JOIN `grouping` g ON mf.grouping_id = g.id " +
"WHERE mf.is_deleted = 0 " + "WHERE mf.is_deleted = 0 " +
"ORDER BY mf.updated_at DESC " + "ORDER BY mf.updated_at DESC " +
"LIMIT #{limit}") "LIMIT #{limit}")
List<MarkdownFileVO> selectRecentWithGrouping(@Param("limit") int limit); List<MarkdownFileVO> selectRecentWithGrouping(@Param("limit") int limit);
@Select("SELECT mf.*, g.grouping as groupingName " + @Select("SELECT mf.id, mf.grouping_id, mf.`title`, mf.file_name, mf.`content`, mf.created_at, mf.updated_at, mf.is_deleted, mf.deleted_at, mf.deleted_by, mf.is_private, g.`grouping` as groupingName " +
"FROM markdown_file mf " + "FROM `markdown_file` mf " +
"LEFT JOIN grouping g ON mf.grouping_id = g.id " + "LEFT JOIN `grouping` g ON mf.grouping_id = g.id " +
"WHERE mf.grouping_id = #{groupingId} AND mf.is_deleted = 0 " + "WHERE mf.grouping_id = #{groupingId} AND mf.is_deleted = 0 " +
"ORDER BY mf.updated_at DESC") "ORDER BY mf.updated_at DESC")
List<MarkdownFileVO> selectByGroupingIdWithGrouping(@Param("groupingId") String groupingId); List<MarkdownFileVO> selectByGroupingIdWithGrouping(@Param("groupingId") String groupingId);
@Select("SELECT * FROM markdown_file WHERE is_deleted = 1") @Select("SELECT id, grouping_id, `title`, file_name, `content`, created_at, updated_at, is_deleted, deleted_at, deleted_by, is_private FROM `markdown_file` WHERE is_deleted = 1")
List<MarkdownFile> selectDeleted(); List<MarkdownFile> selectDeleted();
@Delete("DELETE FROM markdown_file WHERE id = #{id}") @Delete("DELETE FROM `markdown_file` WHERE id = #{id}")
void physicalDeleteById(@Param("id") Long id); void physicalDeleteById(@Param("id") Long id);
@Delete("DELETE FROM markdown_file WHERE grouping_id = #{groupingId}") @Delete("DELETE FROM `markdown_file` WHERE grouping_id = #{groupingId}")
void physicalDeleteByGroupingId(@Param("groupingId") Long groupingId); void physicalDeleteByGroupingId(@Param("groupingId") Long groupingId);
@Update("UPDATE markdown_file SET is_deleted = 0, deleted_at = NULL, deleted_by = NULL WHERE id = #{id}") @Update("UPDATE `markdown_file` SET is_deleted = 0, deleted_at = NULL, deleted_by = NULL WHERE id = #{id}")
void restoreById(@Param("id") Long id); void restoreById(@Param("id") Long id);
@Delete("DELETE FROM markdown_file WHERE is_deleted = 1") @Delete("DELETE FROM `markdown_file` WHERE is_deleted = 1")
void physicalDeleteByIsDeleted(); void physicalDeleteByIsDeleted();
/** /**
* 获取所有笔记ID * 获取所有笔记ID
* @return 所有笔记ID列表 * @return 所有笔记ID列表
*/ */
@Select("SELECT id FROM markdown_file WHERE is_deleted = 0") @Select("SELECT id FROM `markdown_file` WHERE is_deleted = 0")
List<Integer> findAllIds(); List<Integer> findAllIds();
} }

View File

@@ -8,6 +8,6 @@ import org.apache.ibatis.annotations.Select;
@Mapper @Mapper
public interface UserMapper extends BaseMapper<User> { public interface UserMapper extends BaseMapper<User> {
// 自定义查询方法示例 // 自定义查询方法示例
@Select("SELECT * FROM user WHERE username = #{username}") @Select("SELECT id, `username`, `password`, `email`, created_at, updated_at, `token`, token_enddata FROM `user` WHERE username = #{username}")
User findByUsername(String username); User findByUsername(String username);
} }

View File

@@ -13,7 +13,7 @@ spring:
# #
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://panel-jp.998521.xyz:37857/biji_db?useSSL=false&serverTimezone=UTC&characterEncoding=utf8 url: jdbc:mysql://hdy16-16.311169.xyz:20001/biji_db?useSSL=false&serverTimezone=UTC&characterEncoding=utf8
username: biji_user username: biji_user
password: Ll12331100 password: Ll12331100
jpa: jpa:
@@ -30,6 +30,8 @@ mybatis-plus:
mapper-locations: classpath:mapper/*.xml mapper-locations: classpath:mapper/*.xml
configuration: configuration:
map-underscore-to-camel-case: true map-underscore-to-camel-case: true
# 启用安全模式防止SQL注入
safe-mode: true
global-config: global-config:
db-config: db-config:
logic-delete-field: isDeleted # 全局逻辑删除的实体字段名 logic-delete-field: isDeleted # 全局逻辑删除的实体字段名