记录下动态sql的常用标签:

1.where

一般用作数据操作添加的条件

例子:

 <select id="selectByRoleId" resultMap="resource">
select * from resource
<where>
role_id = #{roleId}
</where>

2.if

一般用做查询,修改或者删除数据时的一些拼接条件。

test字段为判断条件

例子:

<select id="findCountByContditon" resultType="int" parameterType="com.example.demo.po.User">
select
coalesce(count(id),0)
from user
<where>
<if test="name != null and name != ''">
name like #{name}
</if>
<if test="createTime != null">
and create_time &lt; #{createTime}
</if>
</where>
</select>

上面的sql有个问题,就是如果name条件不成立,creatTime条件成立,那么sql会报错。一般如果在where条件下有多个if判断,在if前加入where 1=1。

<select id="findCountByContditon" resultType="int" parameterType="com.example.demo.po.User">
select
coalesce(count(id),0)
from user
<where>
1=1
<if test="name != null and name != ''">
and name like #{name}
</if>
<if test="createTime != null">
and create_time &lt; #{createTime}
</if>
</where>
</select>

3.foreach

一般用来批量添加数据

collection字段一般为list、array、map类型,值为传入的参数

separator字段值为分隔符,即以什么来分割

item 字段为循环时每个元素的别名

index 字段值为每次循环时的下标

open 字段值为前缀值

close 字段值为后缀值

例子:

<insert id="saveAll" parameterType="java.util.List">
insert into user(name,role_id,password)
values
<foreach collection="users" separator="," item="user" index="index" >
( #{user.name}, #{user.roleId}, #{user.password})
</foreach>
</insert>

4.set

一般用在修改数据,与if经常一起出现

<update id="updateByCondition">
update user
<set>
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="createTime != null">
create_time = @{createTime}
</if>
</set>
where id = #{id}
</update>

5.include

一般是将常用的字段抽出来作为常量

<sql id="user_columns">
id, name,role_id,password,create_time,update_time
</sql> <select id="selectById" resultMap="user">
select
<include refid="user_columns"/>
from user where id = #{id}
</select>

6.trim

prefix字段前缀添加值

suffix字段后缀添加值

prefixOverrides字段删除前缀值

suffixOverrides字段删除后缀值

<update id="updateByCondition">
update user set
<trim suffixOverrides=","> <if test="name != null and name != ''">
name = #{name},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="createTime != null">
create_time = @{createTime},
</if>
</trim>
where id = #{id}
</update>

如果password为空,其他不为空。sql变为

update user set name = #{name}, create_time = #{createTime} where id = #{id }

来源:http://www.1994july.club/seo/

mybatis学习笔记四的更多相关文章

  1. mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)

    下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...

  2. mybatis学习笔记四(动态sql)

    直接贴图,注解在代码上,其他的配置文件在学习一中就不贴了 1 数据库 2 实体类 package com.home.entity; /** * 此类是: 用户实体类 * @author hpc * @ ...

  3. MyBatis学习笔记(四)——解决字段名与实体类属性名不相同的冲突

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4264425.html 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演 ...

  4. mybatis学习笔记(四)

    resultType 语句返回值类型的完整类名或别名 resultType 返回的是一个map集合,key是列名,value是对应的值 使用resultMap实现联表查询 resultMap 查询的结 ...

  5. MyBatis学习笔记(四) 注解

        使用MyBatis注解开发,可以省去类配置文件,简洁方便.但是比较复杂的SQL和动态SQL还是建议书写类配置文件. 注解还是不推荐使用的.只是了解了解!简单的CRUD可以使用注解.简单写写. ...

  6. Mybatis学习笔记(四) —— SqlMapConfig.xml配置文件

    一.properties(属性) SqlMapConfig.xml可以引用java属性文件中的配置信息 在config下定义db.properties文件,如下所示: db.properties配置文 ...

  7. mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)

    文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...

  8. mybatis 学习笔记(四):mybatis 和 spring 的整合

    mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...

  9. 【MyBatis学习笔记】

    [MyBatis学习笔记]系列之预备篇一:ant的下载与安装 [MyBatis学习笔记]系列之预备篇二:ant入门示例 [MyBatis学习笔记]系列之一:MyBatis入门示例 [MyBatis学习 ...

随机推荐

  1. Python安装和虚拟环境创建以及外部库的安装

    Python.虚拟环境.外部库的安装 一 安装Python 1 Windows 到官网下载对应的版本 下载地址 我选择的是Python3.6.8 下载完成后双击运行 !!!勾选Add Python 3 ...

  2. 八、SAP中使用MOVE来赋值

    一.使用MOVE和使用等号的效果是等同的,代码如下: 二.效果如下:

  3. 二十六、CI框架之分页

    一.在模型中读取数据库中的表 二.在控制器中添加dividePage函数 三.在View中写入显示代码 四.查看效果,还是挺漂亮的分页效果 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信 ...

  4. consul配置

    参考:https://blog.csdn.net/achenyuan/article/details/80389410 gcp: consul安装目录:/usr/local/bin/consul co ...

  5. A. Yellow Cards ( Codeforces Round #585 (Div. 2) 思维水题

    ---恢复内容开始--- output standard output The final match of the Berland Football Cup has been held recent ...

  6. 关于Oracle中job定时器(通过create_job创建的)配置示例

    begin dbms_scheduler.create_job(job_name => 'JOB_BASIC_STATISTIC', job_type => 'STORED_PROCEDU ...

  7. JavaScript 之 原型及原型链

    对象[回顾] 通过字面量创建对象 //通过字面量创建对象 var obj1 = { name:'Jack', age: 18 } 通过系统自带的构造函数构造对象 // 通过系统自带的构造函数构造对象 ...

  8. IP欺骗(XFF头等)

    很多时候需要伪造一些http头来绕过WAF 1.X-Forwarded-For: 简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP,只有在通过了HTTP 代理或者负载均衡服务器时才会添加该 ...

  9. 1.4CAD2017绘图基础

    1.新建(ctrl+n) 命令:new 回车——默认样板(acadiso.dwt) 2.打开(ctr+o) 3.保存(ctrl+S) 4.鼠标的应用: 左键:点击拖选等: 中间滚轮:a.滚动,放大缩小 ...

  10. bootstrap 网格

    实现原理 网格系统的实现原理非常简单,仅仅是通过定义容器大小,平分12份(也有平分成24份或32份,但12份是最常见的),再调整内外边距,最后结合媒体查询,就制作出了强大的响应式网格系统.Bootst ...