码迷,mamicode.com
首页 > 其他好文 > 详细

ibatis (mybatis) for循环拼接语句

时间:2017-04-24 10:14:50      阅读:595      评论:0      收藏:0      [点我收藏+]

标签:sql   ref   condition   cti   class   ati   logs   like   lock   

使用 , 拼接

 

查询条件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 = ?)...

 

 

 

 引用自:http://www.cnblogs.com/Struts-pring/p/5127510.html

 

ibatis (mybatis) for循环拼接语句

标签:sql   ref   condition   cti   class   ati   logs   like   lock   

原文地址:http://www.cnblogs.com/whatlonelytear/p/6755090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!