MyBatis 常用写法
MyBatis 常用写法
1、forEach 循环
forEach 元素的属性主要有 item, idnex, collection, open, separator, close。
- collection:传入的 List 或 Array 或自己封装的 Map。
- item:集合中元素迭代时的别名。
- idnex:集合中元素迭代是的索引。
- open:where 后面表示以什么开始,如以‘(’开始。
- separator:表示在每次进行迭代是的分隔符。
- 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 常用写法的更多相关文章
- 转--Android按钮单击事件的四种常用写法总结
这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下 很多学习Android程序设计的人都会发现每个人对代码的 ...
- Mybatis 常用注解
Mybatis常用注解对应的目标和标签如表所示: 注解 目标 对应的XML标签 @CacheNamespace 类 <cache> @CacheNamespaceRef 类 <cac ...
- 【Android】按钮点击事件的常用写法
学习总结: 最近学习了Android点击事件的常用写法.点击事件会触发监听对象身上的回调,常用写法有以下四种: 方法一:使用匿名内部类. public class MainActivity exten ...
- jquery常用写法简单记录
好久不写东西了......话不多说,主要记录一下,最近做的项目中用到的js的记录(虽然特别特别简单) 一 jquery常用写法记录 jQuery(this).addClass("select ...
- Android按钮单击事件的四种常用写法
这篇文章主要介绍了Android按钮单击事件的四种常用写法总结,比较了常见的四种写法的优劣,有不错的参考借鉴价值,需要的朋友可以参考下 很多学习Android程序设计的人都会发现每个人对代码的写法都有 ...
- Mybatis常用xml
工作中mybatis常用的xml代码 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ma ...
- angularjs中ng-class常用写法,三元表达式、评估表达式与对象写法
壹 ❀ 引 ng-class可以说在angularjs样式开发中使用频率特别高了,这不我想利用ng-class的三元运算符的写法来定义一个样式,结果怎么都想不起来正确写法,恼羞成怒还是整理一遍吧,那 ...
- python time和datetime常用写法格式
python 的time和datetime常用写法 import time from datetime import datetime from datetime import timedelta # ...
- mongodb java操作常用写法
MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成.MongoDB 文档类似于 JSON 对象.字段值可以包含其他文档,数组及文档数组.下面介绍的是用java操作 ...
随机推荐
- map函数和reduce函数、filter函数的区别
①从参数方面来讲:map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数.reduce()函数 ...
- s11 day 102 python Linux环境安装 与路飞项目 微信平台接口
1.微信公众号平台沙箱环境地址 https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login 二.结算中心业务 结算中心: -购物车,删 ...
- 790. Domino and Tromino Tiling
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...
- 队列的实现——java
同样实现方法有两种: 1. 数组的实现,可以存储任意类型的数据(略): 2. Java的 Collection集合 中自带的"队列"(LinkedList)的示例: import ...
- python stdout 重定向
import sys class Buffer(object): def __init__(self): self.buffer = [] def write(self, *args, **kwarg ...
- IntelliJ IDEA配置maven3.3.3+mybatis3.1.1
注:本文参考了孤傲苍狼关于MyBatis学习总结,在此表示感谢,原文链接为http://www.cnblogs.com/xdp-gacl/p/4261895.html. 1.新建project,勾选c ...
- JS 对象 合并
来自:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Sy ...
- git 删除 repository
git 删除 repository 打开版本库,选择要删除的repository 点击Settings 找到删除选项 输入repository name,点击delet…… 删除本地仓库 执行git ...
- shiro授权-记调试过程
根据张开涛老师的shiro教程学习过程中 感觉shiro授权这块有点绕 调试了十几遍 大概有个思路 记录一下 1.单元测试入口 2.subject().isPermitted("+user ...
- 3. C++ POD类型
POD全称Plain Old Data,通常用于说明1个类型的属性.通俗的讲,一个类或结构体通过二进制拷贝后还能保持其数据不变,那么它就是一个POD类型. C++11将POD划分为2个基本概念的合集, ...