mybatis学习笔记四
记录下动态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 < #{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 < #{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学习笔记四的更多相关文章
- mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)
下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...
- mybatis学习笔记四(动态sql)
直接贴图,注解在代码上,其他的配置文件在学习一中就不贴了 1 数据库 2 实体类 package com.home.entity; /** * 此类是: 用户实体类 * @author hpc * @ ...
- MyBatis学习笔记(四)——解决字段名与实体类属性名不相同的冲突
转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4264425.html 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演 ...
- mybatis学习笔记(四)
resultType 语句返回值类型的完整类名或别名 resultType 返回的是一个map集合,key是列名,value是对应的值 使用resultMap实现联表查询 resultMap 查询的结 ...
- MyBatis学习笔记(四) 注解
使用MyBatis注解开发,可以省去类配置文件,简洁方便.但是比较复杂的SQL和动态SQL还是建议书写类配置文件. 注解还是不推荐使用的.只是了解了解!简单的CRUD可以使用注解.简单写写. ...
- Mybatis学习笔记(四) —— SqlMapConfig.xml配置文件
一.properties(属性) SqlMapConfig.xml可以引用java属性文件中的配置信息 在config下定义db.properties文件,如下所示: db.properties配置文 ...
- mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)
文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...
- mybatis 学习笔记(四):mybatis 和 spring 的整合
mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...
- 【MyBatis学习笔记】
[MyBatis学习笔记]系列之预备篇一:ant的下载与安装 [MyBatis学习笔记]系列之预备篇二:ant入门示例 [MyBatis学习笔记]系列之一:MyBatis入门示例 [MyBatis学习 ...
随机推荐
- 路由反射器(RR)配置
IBGP与EBGP的区别:是否使用指定源(update-source) 路由反射器(RR)配置: 零:关闭同步,关闭自动汇总. ①:启用BGP ②:在相同BGP-AS下建立邻居. ③:指定路由器及所在 ...
- 大二暑假第一周总结--初次安装配置Hadoop
本次配置主要使用的教程:http://dblab.xmu.edu.cn/blog/install-hadoop-in-centos/ 以下是自己在配置中的遇到的一些问题和解决方法,或者提示 一.使用虚 ...
- CSS position定位属性
css中的position属性是用于设置元素位置的定位方式 它有以下几种取值: static:默认定位方式,子容器在父容器中按照默认顺序进行摆放 absolute:绝对定位,元素不占据父容器空间,相当 ...
- MySQL表连接原理
以下文章均来自掘金小测: https://juejin.im/book/5bffcbc9f265da614b11b731/section/5c061b0cf265da612577e0f4 表连接本质: ...
- 读写锁ReentrantReadWriteLock源代码浅析
1.简介 并发中常用的ReentrantLock,是一种典型的排他锁,这类锁在同一时刻只允许一个线程进行访问,实际上将并行操作变成了串行操作.在并发量大的业务中,其整体效率.吞吐量不能满足实现的需要. ...
- 题解 Luogu P5434: 有标号荒漠计数
妈妈我终于会这道题了! 设\(n\)个点的有根仙人掌个数的指数型生成函数(EGF)为\(F(x)\), 令\(f_i = [x^n]F(x)\) 对于\(f_i\), 我们考虑钦点\(1\)号点为根, ...
- DLL对应的导入库一定会生成的
测试代码: #pragma once #define TESTDEPEND_EXPORTS #ifdef TESTDEPEND_EXPORTS #define TESTDEPEND_API __dec ...
- Mysql插入数据里有中文字符出现Incorrect string value的错误
问题:Mysql插入数据里有中文字符出现Incorrect string value的错误 描述:CMD里直接敲代码插入数据 提示的部分截取为:ERROR 1366 (HY000): Inco ...
- js 关联数组
踩得坑: JS ,通过 new Array()创建了一个数组: var param = new Array();param["key1"] = value1;param[&quo ...
- VMware-Workstation-Full-12.5.9
https://download3.vmware.com/software/wkst/file/VMware-Workstation-Full-12.5.9-7535481.x86_64.bundle ...