Commit 8a426338 by zhouhaibao

[+]岚图质检规则新增

1 parent e766de35
package cn.com.glsx.dj.mindworkplate.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Table(name = "recording_tag_config_model_type")
@Data
public class RecordingTagConfigModelType {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/**
* 配置编码
*/
@Column(name = "config_code")
private String configCode;
/**
* 模型类型
*/
@Column(name = "model_type")
private String modelType;
/**
* 创建人
*/
@Column(name = "created_by")
private String createdBy;
/**
* 创建时间
*/
@Column(name = "created_date")
private Date createdDate;
/**
* 修改人
*/
@Column(name = "updated_by")
private String updatedBy;
/**
* 修改时间
*/
@Column(name = "updated_date")
private Date updatedDate;
/**
* 删除标识(D:已删除;N:未删除)
*/
@Column(name = "deleted_flag")
private String deletedFlag;
}
package cn.com.glsx.dj.mindworkplate.config;
import java.util.List;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* 模型类型配置
*/
@Data
@Configuration
@ConfigurationProperties("model.type")
public class ModelTypeConfig {
/**
* 走新逻辑的模型类型集合
*/
private List<String> newLogicModelTypes;
}
package cn.com.glsx.dj.mindworkplate.mapper;
import cn.com.glsx.dj.mindworkplate.model.RecordingTagConfigModelType;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.oreframework.datasource.mybatis.mapper.OreMapper;
@Mapper
public interface RecordingTagConfigModelTypeMapper extends OreMapper<RecordingTagConfigModelType> {
/**
* 根据模型类型查询配置编码
*/
List<String> findConfigCodesByModelType(@Param("modelType") String modelType);
/**
* 根据配置编码查询模型类型
*/
List<String> findModelTypesByConfigCode(@Param("configCode") String configCode);
/**
* 检查配置编码和模型类型是否存在
*/
int countByConfigCodeAndModelType(@Param("configCode") String configCode, @Param("modelType") String modelType);
}
package cn.com.glsx.dj.mindworkplate.service.recordingtag;
import cn.com.glsx.dj.mindworkplate.mapper.RecordingTagConfigModelTypeMapper;
import cn.com.glsx.dj.mindworkplate.model.RecordingTagConfigModelType;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 录音标签配置模型类型关联服务
*/
@Service
public class RecordingTagConfigModelTypeService {
@Autowired
private RecordingTagConfigModelTypeMapper recordingTagConfigModelTypeMapper;
/**
* 根据模型类型查询配置编码
*/
public List<String> findConfigCodesByModelType(String modelType) {
return recordingTagConfigModelTypeMapper.findConfigCodesByModelType(modelType);
}
/**
* 检查配置编码和模型类型是否存在
*/
public boolean existsByConfigCodeAndModelType(String configCode, String modelType) {
return recordingTagConfigModelTypeMapper.countByConfigCodeAndModelType(configCode, modelType) > 0;
}
/**
* 保存配置编码和模型类型的关联关系
*/
public void save(String configCode, String modelType) {
if (!existsByConfigCodeAndModelType(configCode, modelType)) {
RecordingTagConfigModelType recordingTagConfigModelType = new RecordingTagConfigModelType();
recordingTagConfigModelType.setConfigCode(configCode);
recordingTagConfigModelType.setModelType(modelType);
recordingTagConfigModelType.setCreatedBy("system");
recordingTagConfigModelType.setCreatedDate(new Date());
recordingTagConfigModelType.setUpdatedBy("system");
recordingTagConfigModelType.setUpdatedDate(new Date());
recordingTagConfigModelType.setDeletedFlag("N");
recordingTagConfigModelTypeMapper.insert(recordingTagConfigModelType);
}
}
/**
* 批量保存配置编码和模型类型的关联关系
*/
public void saveBatch(String configCode, List<String> modelTypes) {
for (String modelType : modelTypes) {
save(configCode, modelType);
}
}
/**
* 删除配置编码和模型类型的关联关系
*/
public void delete(String configCode, String modelType) {
RecordingTagConfigModelType recordingTagConfigModelType = new RecordingTagConfigModelType();
recordingTagConfigModelType.setConfigCode(configCode);
recordingTagConfigModelType.setModelType(modelType);
recordingTagConfigModelType.setDeletedFlag("Y");
recordingTagConfigModelType.setUpdatedBy("system");
recordingTagConfigModelType.setUpdatedDate(new Date());
recordingTagConfigModelTypeMapper.updateByExampleSelective(recordingTagConfigModelType, null);
}
}
<?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="cn.com.glsx.dj.mindworkplate.mapper.RecordingTagConfigModelTypeMapper" >
<resultMap id="BaseResultMap" type="cn.com.glsx.dj.mindworkplate.model.RecordingTagConfigModelType" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="config_code" property="configCode" jdbcType="VARCHAR" />
<result column="model_type" property="modelType" jdbcType="VARCHAR" />
<result column="created_by" property="createdBy" jdbcType="VARCHAR" />
<result column="created_date" property="createdDate" jdbcType="TIMESTAMP" />
<result column="updated_by" property="updatedBy" jdbcType="VARCHAR" />
<result column="updated_date" property="updatedDate" jdbcType="TIMESTAMP" />
<result column="deleted_flag" property="deletedFlag" jdbcType="CHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, config_code, model_type, created_by, created_date, updated_by, updated_date, deleted_flag
</sql>
<select id="findConfigCodesByModelType" resultType="java.lang.String">
select config_code from recording_tag_config_model_type
where model_type = #{modelType} and deleted_flag = 'N'
</select>
<select id="findModelTypesByConfigCode" resultType="java.lang.String">
select model_type from recording_tag_config_model_type
where config_code = #{configCode} and deleted_flag = 'N'
</select>
<select id="countByConfigCodeAndModelType" resultType="java.lang.Integer">
select count(*) from recording_tag_config_model_type
where config_code = #{configCode} and model_type = #{modelType} and deleted_flag = 'N'
</select>
</mapper>
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议

    1. 逻辑缺陷:delete 方法存在全表更新风险(最高优先级)

    • 问题描述:在 RecordingTagConfigModelTypeService.delete 方法中,调用 updateByExampleSelective 时第二个参数(Example 条件对象)传入了 null。
    • 影响分析:在大多数 MyBatis 通用 Mapper 框架(如 TK MyBatis 或 MyBatis Plus)中,若 Example 准则为 null,生成的 SQL 将不带 WHERE 子句。这将导致数据库表中所有记录的 deleted_flag 被更新为 'Y',造成灾难性的数据逻辑删除。
    • 优化建议:必须构造具体的 Example 对象或 Criteria 来限定更新范围。

      public void delete(String configCode, String modelType) {
        Example example = new Example(RecordingTagConfigModelType.class);
        example.createCriteria()
               .andEqualTo("configCode", configCode)
               .andEqualTo("modelType", modelType)
               .andEqualTo("deletedFlag", "N");
      
        RecordingTagConfigModelType updateNode = new RecordingTagConfigModelType();
        updateNode.setDeletedFlag("Y");
        updateNode.setUpdatedBy("system");
        updateNode.setUpdatedDate(new Date());
        recordingTagConfigModelTypeMapper.updateByExampleSelective(updateNode, example);
      }
      

    2. 性能瓶颈:saveBatch 循环操作数据库(N+1 问题)

    • 问题描述:saveBatch 方法通过循环调用 save 方法实现,而 save 方法内部又包含了 exists 查询和 insert 操作。
    • 影响分析:若 modelTypes 列表较大(例如 100 个),则会产生 200 次数据库网络交互(RTT)。在高并发或大数据量场景下,会显著消耗连接池资源并增加接口响应耗时,且缺乏事务原子性。
    • 优化建议:
      1. 在 XML 中编写批量插入 SQL:INSERT INTO ... VALUES (...), (...)...。
      2. 在插入前通过 IN 查询一次性获取已存在的记录,在内存中过滤后再执行单次批量插入。
      3. 建议在数据库层面为 config_code 和 model_type 建立联合唯一索引。

    3. 健壮性风险:缺乏并发控制与事务管理

    • 问题描述:save 方法采用“先查询后插入”的逻辑,且未标注 @Transactional 注解。
    • 影响分析:在并发环境下,两个线程可能同时通过 exists 校验,随后同时执行 insert,导致数据库出现重复的关联数据(若无唯一索引则逻辑重复,若有唯一索引则抛出异常导致流程中断)。
    • 优化建议:
      1. 为 saveBatch 方法添加 @Transactional(rollbackFor = Exception.class)。
      2. 配合数据库联合唯一索引,使用 INSERT IGNORE 或 MyBatis 的 on duplicate key update 语法来保证幂等性。

    二、评分明细

    • 功能实现的正确性与健壮性(20/40分):delete 逻辑存在严重隐患,可能导致全表数据失效;save 逻辑在并发下不安全。
    • 安全性与潜在风险(25/30分):虽然没有 SQL 注入(使用了参数化查询),但逻辑删除的实现方式存在误删全表的高风险。
    • 最佳实践与可维护性(15/20分):代码结构清晰,使用了标准的 Service/Mapper 模式;但硬编码了 "system" 字符串,建议提取为常量或通过上下文获取。
    • 性能与资源利用(2/5分):saveBatch 的实现方式是典型的性能反模式,存在大量不必要的 IO 开销。
    • 提交信息质量(4/5分):提交信息清晰,但略显简单,未能体现技术层面的变更概要。

    总分:66分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!