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学习 ...
随机推荐
- 修改虚拟机ip
在命令行里输入:more /etc/sysconfig/network-scripts/ifcfg-eth0 (注意more后要空一格,还有eth0,最后是数字零). 然后再输入:ifconfig ...
- DCGAN
Deep Convolutional Generative Adversarial Networks we introduced the basic ideas behind how GANs wor ...
- Unity 可重复随机数
出处 https://blogs.unity3d.com/cn/2015/01/07/a-primer-on-repeatable-random-numbers/ (英文原版) http://ww ...
- Python说文解字_Python之多任务_05
问:在Py3.5之前yield表现非常好,在Py3.5之后为了将予以变得更加明确,就引入了async和await关键词用于定义原生的协议. 答:async和await原生协程: async def d ...
- linux下springboot项目通过jetty发布war包应用
Linux下jetty发布jar包 1.通过官网下载jetty点击下载https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distributi ...
- Java多人聊天室第一版
package cn.zhang.chat; import java.io.BufferedReader; import java.io.PrintWriter; /** * 所有用户均有的信息,单独 ...
- Spring源码解读:核心类DefaultListableBeanFactory的继承体系
1 简介 我们常用的ClassPathXmlApplicationContext是AbstractRefreshableApplicationContext的子类,而DefaultListableBe ...
- MySQL--SHOW ENGINE INNODB STATUS
===================================== -- :: 0x7f305b965700 INNODB MONITOR OUTPUT =================== ...
- 踩一踩win7安装neo4j的坑
本文使用zip解压方式安装,下载社区版zip 解压到喜欢的文件夹,然后配置环境变量NEO4J_HOME=D:\neo4j-community-3.5.5(自己的解压目录) 配置Path=%NEO4J_ ...
- Json返回结果为null属性不显示解决
import java.io.IOException; import org.springframework.boot.autoconfigure.condition.ConditionalOnMis ...