函数中参数数量超过7时,Sonar Lint会弹出警告。
Method has 8 parameters, which is greater than 7 authorized.
List<CellSearchDetail> getCellSearchResult(
Integer tankId,
@Param("rack") Integer rack,
@Param("box") Integer box,
@Param("locationStatus") String locationStatus,
@Param("from") String from,
@Param("to")String to,
@Param("cellName") String cellName,
@Param("projectId") String projectId
);
解决方案:
将参数封装在类中。
List<CellSearchDetail> getCellSearchResult(@Param("searchCell")CellSearchDTO searchCell);
@Data
public class CellSearchDTO {
private Integer tankId;
private Integer rack;
private Integer box;
private String locationStatus;
private String from;
private String to;
private String cellName;
private String projectId;
}
在Mapper.xml中通过test过滤null。
<select id="getCellSearchResult" resultMap="cellSearchDetailMap">
select l.id, concat_ws("-", l.`tank_id`, l.`rack`, l.`box`, l.`row`, l.`column`) as position, l.location_status, c.cell_id, c.cell_name, c.project_id, c.cell_generation, c.freezing_by, c.freezing_date from location l
left join cell c on l.cell_id = c.cell_id
where l.tank_id = #{searchCell.tankId}
<if test="searchCell.rack != null">
and l.rack = #{searchCell.rack}
</if>
<if test="searchCell.box != null">
and l.box = #{searchCell.box}
</if>
<if test="searchCell.locationStatus != null">
and l.location_status = #{searchCell.locationStatus}
</if>
<if test="searchCell.from != null">
and c.freezing_date >= #{searchCell.from}
</if>
<if test="searchCell.to != null">
and c.freezing_date <= #{searchCell.to}
</if>
<if test="searchCell.cellName != null">
and c.cell_name like CONCAT('%',#{searchCell.cellName},'%')
</if>
<if test="searchCell.projectId != null">
and c.project_id like CONCAT('%',#{searchCell.projectId},'%')
</if>
</select>