mybatis-plus:xml拼接sql方式
作者:玉成226
这篇文章主要介绍了mybatis-plus:xml拼接sql方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
mybatis-plus:xml拼接sql
foreach操作map
<if test = "vo.map!= null and vo.map.size() > 0">
<foreach item="item" index="key" collection="vo.map">
<if test = "key != null and key != 'assetType'">
// jsonParam为json类型字段
and JSON_EXTRACT(jsonParam, '$.${key}') = #{item}
</if>
<if test = "key != null and key == 'assetType'">
and ${key} = #{item}
</if>
</foreach>
</if>foreach操作List
<if test="assetCodes != null and list.size > 0">
and field in
<foreach collection="list" item="e" index="index" open="(" close=")" separator=",">
#{e}
</foreach>
</if>foreach操作String
<if test="vo.departmentCodeList != null and vo.departmentCodeList != ''">
AND r.department_code in
<foreach item="departmentCode" collection="vo.departmentCodeList.split(',')" open="(" separator="," close=")">
#{departmentCode}
</foreach>
</if>mybatis-plus动态拼接sql语句
解释
${ew.customSqlSegment} 是 MyBatis-Plus 中用于在 SQL 语句中插入自定义 SQL 片段的占位符。
ew 是指 Wrapper 对象(通常是 QueryWrapper 或 UpdateWrapper),而 customSqlSegment 是这个对象中的自定义 SQL 片段。
用途
这个占位符通常用于动态拼接 SQL 语句
允许在特定的 SQL 语句部分插入自定义的条件、过滤器或者其他 SQL 片段。
例子
QueryWrapper<MsCustomer> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("status", "active");
// 动态添加一个自定义的 SQL 片段
queryWrapper.apply("custom_column > {0}", someValue);
List<MsCustomer> customers = msCustomerMapper.selectList(queryWrapper);在 Mapper XML 文件中,可以像这样使用 ${ew.customSqlSegment}:
<select id="selectCustomers" resultType="MsCustomer">
SELECT * FROM ms_customer ${ew.customSqlSegment}
</select>作用
customSqlSegment 允许你通过编程方式动态生成 SQL 语句中的某些部分,从而实现更灵活的查询和操作。
例如:
如果在代码中调用了 apply() 方法或其他添加条件的方法,这些条件会被自动拼接到 ${ew.customSqlSegment} 处,从而生成最终的 SQL 语句。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
