MyBatis 常用写法

1、forEach 循环

  forEach 元素的属性主要有 item, idnex, collection, open, separator, close。

  1. collection:传入的 List 或 Array 或自己封装的 Map。
  2. item:集合中元素迭代时的别名。
  3. idnex:集合中元素迭代是的索引。
  4. open:where 后面表示以什么开始,如以‘(’开始。
  5. separator:表示在每次进行迭代是的分隔符。
  6. close:where后面表示以什么结束,如以‘)’结束。
//mapper中需要传递一个容器
public List<User> queryByIdList(List<Integer> userIdList); <select id="queryByIdList" resultMap="BaseResultMap" parameterType="map">
SELECT * FROM user
WHERE userId IN
<foreach collection="userIdList" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</select>

2、concat 模糊查询

//模糊查询使用concat拼接sql
<select id="queryByName" resultMap="BaseResultMap" paramterType"string">
SELECT * FROM user
<where>
<if test="name != null">
name like concat('%', concat(#{name}, '%'))
</if>
</where>
</select>

3、if + where 标签

  用 if 标签判断参数是否有效来进行条件查询。

<select id="getUserList" resultMap="BaseResultMap" paramterType="com.demo.User">
SELECT * FROM user
<where>
<if test="userId !=null and userId!= ''">
userId= #{userId}
</if>
<if test="name !=null and name!= ''">
AND name= #{name}
</if>
<if phone="userId !=null and phone!= ''">
AND phone= #{phone}
</if>
</where>
</select>

where 动态语句中,where 标签会自动去掉 AND 或 OR。防止 WHERE AND 错误。

4、if + set

  使用 set 标签可以动态的配置 SET 关键字,使用 if + set 标签,如果某项为 null 则不进行更新。

<update id="updateUser" paramterType="com.demo.User">
UPDATE user
<set>
<if test=" name != null and name != ''">
name = #{name},
</if>
<if test=" phone != null and phone != ''">
phone = #{phone},
</if>
</set>
WHERE userId = #{userId}
</update>

5、if + trim 代替 where/set 标签

  trim 可以更灵活的去处多余关键字的标签,可以实现 where 和 set 的效果。

<select id="getUserList" resultMap="BaseResultMap" paramterType="com.demo.User">
SELECT * FROM user
<trim prefix="WHERE" prefixOverrides="AND|OR">
<if test="userId !=null and userId!= ''">
userId= #{userId}
</if>
<if test="name !=null and name!= ''">
AND name= #{name}
</if>
<if phone="userId !=null and phone!= ''">
AND phone= #{phone}
</if>
</trim>
</select> <update id="updateUser" paramterType="com.demo.User">
UPDATE user
<trim prefix="SET" suffixOverrides=",">
<if test=" name != null and name != ''">
name = #{name},
</if>
<if test=" phone != null and phone != ''">
phone = #{phone},
</if>
</trim>
WHERE userId = #{userId}
</update>

5、choose(when, otherwise)标签

  choose 标签是按顺序判断其内部 when 标签中的 test 条件是否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满足,则执行 otherwise 中的 sql。类似 java 中的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

<select id="selectCustomerByCustNameAndType" parameterType="map" resultMap="BaseResultMap">
SELECT * FROM user
<choose>
<when test="Utype == 'name'">
WHERE name = #{name}
</when>
<when test="Utype == 'phone'">
WHERE phone= #{phone}
</when>
<when test="Utype == 'email'">
WHERE email= #{email}
</when>
<otherwise>
WHERE name = #{name}
</otherwise>
</choose>
</select>

MyBatis 常用写法的更多相关文章

  1. 转--Android按钮单击事件的四种常用写法总结

    这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下     很多学习Android程序设计的人都会发现每个人对代码的 ...

  2. Mybatis 常用注解

    Mybatis常用注解对应的目标和标签如表所示: 注解 目标 对应的XML标签 @CacheNamespace 类 <cache> @CacheNamespaceRef 类 <cac ...

  3. 【Android】按钮点击事件的常用写法

    学习总结: 最近学习了Android点击事件的常用写法.点击事件会触发监听对象身上的回调,常用写法有以下四种: 方法一:使用匿名内部类. public class MainActivity exten ...

  4. jquery常用写法简单记录

    好久不写东西了......话不多说,主要记录一下,最近做的项目中用到的js的记录(虽然特别特别简单) 一 jquery常用写法记录 jQuery(this).addClass("select ...

  5. Android按钮单击事件的四种常用写法

    这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下 很多学习Android程序设计的人都会发现每个人对代码的写法都有 ...

  6. Mybatis常用xml

    工作中mybatis常用的xml代码 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ma ...

  7. angularjs中ng-class常用写法,三元表达式、评估表达式与对象写法

     壹 ❀ 引 ng-class可以说在angularjs样式开发中使用频率特别高了,这不我想利用ng-class的三元运算符的写法来定义一个样式,结果怎么都想不起来正确写法,恼羞成怒还是整理一遍吧,那 ...

  8. python time和datetime常用写法格式

    python 的time和datetime常用写法 import time from datetime import datetime from datetime import timedelta # ...

  9. mongodb java操作常用写法

    MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成.MongoDB 文档类似于 JSON 对象.字段值可以包含其他文档,数组及文档数组.下面介绍的是用java操作 ...

随机推荐

  1. Day 27 类的进阶-反射

    11. __new__ 和 __metaclass__ 阅读以下代码: 1 2 3 4 5 6 class Foo(object): def __init__(self): pass obj = Fo ...

  2. 741. Cherry Pickup

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  3. iOS method swizz

    1: 防止按钮在一定时间内重复响应默认1秒 // // UIButton+AvoidDoubleClick.h // 51WaywardShop // // Created by jisa on 20 ...

  4. 压缩VBox虚拟机空间的方法

      虚拟机使用久了就会发现虚拟文件越来越大,删除虚拟机中的文件之后物理主机的磁盘空间并不会相应减少,所以实际占用的空间并没有虚拟文件大小那么大,因此我们很有必要进行压缩.不过VirtualBox貌似没 ...

  5. [题解]luogu P4116 Qtree3

    终于来到了Qtree3, 其实这是Qtree系列中最简单的一道题,并不需要线段树, 只要树链剖分的一点思想就吼了. 对于树链剖分剖出来的每一根重链,在重链上维护一个Set就好了, 每一个Set里存的都 ...

  6. 【xsy1131】tortue FFT

    题目大意: 一次游戏要按N个按键.每个按键阿米巴有P[i]的概率按错.对于一串x个连续按对的按键,阿米巴可以得分 $f(x)=tan(\dfrac{x}{N})\times e^{arcsin(0.8 ...

  7. 【poj3252】 Round Numbers (数位DP+记忆化DFS)

    题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位dp,感觉神清气爽~(原谅我这个 ...

  8. 《LeetBook》leetcode题解(18) : 4Sum[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. Nginx安装图片模块出错,提示fatal error: curl/curl.h

    获得安装包,从网上直接下载下载地址:https://curl.haxx.se/download.html 然后解压安装后就可以了 # # cd curl- # ./configure # make & ...

  10. 带你了解数据库中事务的ACID特性

    前言 前面我们介绍过数据库中 带你了解数据库中JOIN的用法 与 带你了解数据库中group by的用法的相关用法.本章节主要来介绍下数据库中一个非常重要的知识点事务,也是我们项目中或面试中经常会遇到 ...