模块优化之SQL
1、模糊查询
<!--条件-分页查询字典类别信息-->
<select id="queryByPage" resultType="com.lekai.anysys.entity.vo.SysDictTypeVo">
<include refid="selectDictTypeVo" />
where 1 = 1
<if test="Sys.name != null and Sys.name != ''">
and `name` like CONCAT('%', #{Sys.name}, '%')
</if>
<if test="Sys.type != null and Sys.type != ''">
and `type` like CONCAT('%', #{Sys.type}, '%')
</if>
<if test="Sys.state != null">
and state = #{Sys.state}
</if>
and del_flag = 0
order by created_time desc
</select>
concat('%',#{sda},'%') 查询含sda的数据
2、SQL公共部分-sql标签
<sql id="selectDictTypeVo">
select id, `name`, `type`, state, created_by, created_time, remarks
from any_sys_dict_type
</sql>
引用-include标签
<select id="selectDictTypeAll" resultMap="SysDictTypeResult">
<include refid="selectDictTypeVo"/> where del_flag = 0
</select>
将多次使用的部分提出,在需要使用的地方进行引用。提高代码可读性
3、封装返回集,简化
<resultMap type="com.lekai.anysys.entity.SysDictTypeEntity" id="SysDictTypeResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="type" column="type"/>
<result property="state" column="state"/>
</resultMap>
<select id="selectDictTypeAll" resultMap="SysDictTypeResult">
<include refid="selectDictTypeVo"/> where del_flag = 0
</select>
将resultMap简化
4、in功能-foreach标签
<delete id="deleteDictTypeByIds" parameterType="Integer">
delete from any_sys_dict_type where id in
<foreach collection="array" item="dictId" open="(" separator="," close=")">
#{dictId}
</foreach>
and del_flag = 0
</delete>
5、更新-set标签
<update id="updateDictType" parameterType="com.lekai.anysys.entity.SysDictTypeEntity">
update any_sys_dict_type
<set>
<if test="name != null and name != ''">`name` = #{name},</if>
<if test="type != null and type != ''">`type` = #{type},</if>
<if test="state != null">state = #{state},</if>
<if test="remarks != null">remarks = #{remarks},</if>
<if test="updatedBy != null and updatedBy != ''">updated_by = #{updatedBy},</if>
updated_time = sysdate()
</set>
where id = #{id}
</update>
判断选择设置
6、条件新增
<insert id="insertDictType" parameterType="com.lekai.anysys.entity.SysDictTypeEntity">
insert into any_sys_dict_type (
<if test="name != null and name != ''">name,</if>
<if test="type != null and type != ''">type,</if>
<if test="state != null">state,</if>
<if test="remarks != null and remarks != ''">remarks</if>
) values (
<if test="name != null and name != ''">#{name},</if>
<if test="type != null and type != ''">#{type},</if>
<if test="state != null">#{state},</if>
<if test="remarks != null and remarks != ''">#{remarks}</if>
)
</insert>
别说差点,差点就是永远
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理