使用mybatis做查询时,希望可以通过动态修改条件来完成查询,又不想封装或者转Map。结果如下
<select id="findPhonesById" parameterType="java.lang.String" resultType="java.lang.String">
select phone from contact_list cl left join contact_group cg on cl.d_id = cg.id
where 1 = 1
<if test="_parameter == 1">
and cl.d_id in (select distinct d_id from contact_list)
</if>
<if test="_parameter != 1">
and cl.d_id = #{id}
</if>
</select>
使用_parameter代替传入的参数,不会报错。
参考:https://www.cnblogs.com/MIXP/p/7655591.html
嗯,这样确实好使,实现了我想要的动态查询
但是,下一条的多参(简单参数)传入这样就不行了,故再查资料如下:
可以看到有个<if test="_parameter != null" >,如果只有一个参数,那么_parameter 就代表该参数,如果有多个参数,那么_parameter 可以get(0)得到第一个参数。
参考:https://www.cnblogs.com/straybirds/p/9085414.html
测了一下发现不行
<select id="findAllData" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from em_data
<if test='_parameter.get("0") != null'>
where em_code = #{emCode,jdbcType=VARCHAR}
</if>
order by ed_id desc
limit #{p_start,jdbcType=BIGINT},#{p_size,jdbcType=BIGINT}
</select>
后经修改测试下面方法可用
<select id="findAllData" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from em_data
<if test='_parameter.get("emCode") != null'>
where em_code = #{emCode,jdbcType=VARCHAR}
</if>
order by ed_id desc
limit #{p_start,jdbcType=BIGINT},#{p_size,jdbcType=BIGINT}
</select>
虽然结果是正常的,但后台会抛出一个debug级错误
详解
并未发现该异常并未对系统的运行造成什么影响,网上也说,关掉debug级别的日志就可以啦,确实,一个放在debug级别里的异常想来也不会是什么重要异常!
参考传送门:https://blog.youkuaiyun.com/rchm8519/article/details/40147745
解决办法:
1 http://nihao-shijie.lofter.com/post/1cd58fc5_cfdd9a2
2 https://blog.youkuaiyun.com/yangxiaovip/article/details/18986493
还有一个更狠的,感觉不靠谱,不安全,老式的SQL注入口么~
<select id="queryTableDataForMap" parameterType="String" resultType="map"> ${_parameter} </select>
参考传送门:https://blog.youkuaiyun.com/ainuser/article/details/81947211
心里还是恶心,我采用的是关了日志显示,更多的有时间了回来再研究
再放一个其它的解决封装方法,其实封装Map传入也会有那个debug。
参考传送门:https://blog.youkuaiyun.com/weixin_37891479/article/details/80525612
其它参考:http://blog.51cto.com/lavasoft/2154752