ibatis (mybatis) for循环拼接语句
Posted 万物生长
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ibatis (mybatis) for循环拼接语句相关的知识,希望对你有一定的参考价值。
使用 , 拼接
查询条件dto
public class queryCondition
{
private String[] stuIds;
private String name;
}
查询sqlMap
<select id="selectStu" parameterClass="cn.xy.queryCondition" resultClass="cn.xy.Student">
select id,name from student
<dynamic prepend="where">
<isNotNull property="stuIds" prepend="and">
<iterate property="stuIds" open="id in (" close=")" conjunction=",">
#stuIds[]#
</iterate>
</isNotNull>
<isNotNull property="name" prepend="and">
name like \'%$name$%\'
</isNotNull>
</dynamic>
</select>
在查询条件中有一个数组stuIds,在动态标签中进行遍历,看每一个student的id是否在该数组中。
发出的语句 select id,name from student where id in ( ? , ?) ...
使用 or 拼接
查询条件dto
public class queryCondition
{
private List<Student> lst;
private String name;
}
查询sqlMap
<select id="selectStu" parameterClass="cn.xy.queryCondition" resultClass="cn.xy.Student">
select id,name from student
<dynamic prepend="where">
<isNotNull property="lst" prepend="and">
<iterate property="lst" open=" (" close=")" conjunction="or">
id = #lst[].id#
</iterate>
</isNotNull>
<isNotNull property="name" prepend="and">
name like \'%$name$%\'
</isNotNull>
</dynamic>
</select>
发出的语句 select id,name from student where (id = ? or id = ?)...
以上是关于ibatis (mybatis) for循环拼接语句的主要内容,如果未能解决你的问题,请参考以下文章
ibatis iterate VS mybatis foreach
解决Mybatis报错:org.apache.ibatis.reflection.ReflectionException: There is no getter for property named
Mybatis懒加载时,springMVC返回JSON异常 Could not write JSON: No serializer found for class org.apache.ibatis
mybatis之org.apache.ibatis.reflection.ReflectionException: There is no getter for property named '