MyBatis中foreach元素用法解析

来源:互联网 发布:windows相对路径写法 编辑:程序博客网 时间:2024/05/20 03:44


(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/70946761冷血之心的博客)

     

       动态SQL是MyBatis框架的一个重要功能。如果你是用JDBC,如果你想要根据需要去拼装SQL语句,是一件比较麻烦的事,但是MyBatis提供对SQL语句动态的组装能力。大量的判断都可以在其映射文件XML里边配置,以达到我们需要大量代码才能实现的功能,大大减少了我们编写代码的工作量,这体现了MyBatis的灵活性高度可配置性可维护性

动态SQL包括以下几种元素:

动态SQL的元素元素作用备注if判断语句单条件分支判断choose(when、otherwise)相当于java中的case when 语句多条件分值判断trim(where set)辅助元素用于处理一些SQL拼装的问题foreach循环语句在in语句等列举条件常用


这篇博客,我们讨论其foreach元素的用法。如上所示,foreach元素是一个循环语句,它的作用是遍历集合。foreach能够很好地支持数组和List、Set接口的集合,对此提供遍历功能。

foreach元素中有六个需要配置的属性:

  • collection:代表传递进来的参数名称,可以是一个数组、List、Set等集合
  • item:配置的是循环中的当前元素
  • index:配置的是当前元素在集合中的位置下标
  • openclose:配置的是以什么符号将这些集合元素包装起来
  • separator:是各个元素的间隔符

eg:
<selectid="getProductBean"parameterType="java.util.List"          resultType="com.ywq.ProductBean">          select          productid,          developerid,          platformid,          businessid          from product_basic_info          where         productid in         <foreach collection="list" item="product"   separator="," >           #{product}         </foreach>          </select>

结果报错了!!!

### SQL: select    productid,   developerid,   platformid,   businessid    from product_basic_info   where      productid in
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3171746,3123170,3171747,3171744' at line 10

解决办法:

由于使用了in关键字,所以需要加上foreach中的open和close属性。

<selectid="getProductBean"parameterType="java.util.List"          resultType="com.ywq.ProductBean">          select          productid,          developerid,          platformid,          businessid          from product_basic_info          where         productid in         <foreach collection="list" item="product"   open="(" separator="," close=")" >           #{product}         </foreach>          </select>


        以前自己在使用foreach元素的时候,都是进行insert操作,所以并没有使用过open和close属性,导致错误的发生,甚至在之前还一度以为这两属性是多余的,看来真是惭愧呀,存在即是合理的。

附上批量往表中插入数据的代码:

其中Base_Column_List是一个标签,里边包含表中各个列信息。

<!-- 表 product批量添加记录 --><insert id="insert2ProductTable4Batch"          parameterType="java.util.List">          INSERT INTO product          (          <include refid="Base_Column_List" />          ) VALUES          <foreach collection="list" item="product" separator=",">              (              #{product.businessid},              #{product.productid},              #{product.email},              #{product.productype},              #{product.typename},              #{product.developerid},              #{product.productname},              #{product.isdeleted},              #{product.comefrom},              #{product.registertime},              #{product.totalUser}                          )          </foreach></insert>



在SQL中对于in语句使用的还是比较多的,但是对于大量数据的in 语句必须注意,因为它会消耗大量的资源,导致性能下降。还有一些数据库的SQL对执行的SQL长度也有限制,所以我们在使用时需要预估一下这个collection对象的长度。




以上是对MyBatis中动态SQL功能中foreach元素使用方法的一个简单总结,如果对你有帮助,记得点赞哦~欢迎大家关注我的博客,可以进群366533258一起交流学习哦~hgjg






10 1
原创粉丝点击