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学习 ...
随机推荐
- .NET CORE部署各种问题
1.安装运行时以后,执行dotnet --version查看版本提示 丢失api-ms-win-crt-runtime-l1-1-0.dll, 当本地api-ms-win-crt-runtime-l ...
- 适合初学者的10个linux命令
转http://devopscube.com/list-of-linux-commands-every-developer-should-know/ At some point in you deve ...
- web嵌入到原生的app里需要注意的事项
1.https://www.cnblogs.com/shimily/articles/7943370.html 2.https://www.cnblogs.com/stoneniqiu/p/60771 ...
- SUCTF 2019-EasySQL
0x00 知识点: 1:堆叠注入 2:sql_mode : 它定义了 MySQL 应支持的 SQL 语法,以及应该在数据上执行何种确认检查,其中的 PIPES_AS_CONCAT 将 || 视为字符串 ...
- Q1:Two Sum
1. Two Sum 官方的链接:1. Two Sum Description : Given an array of integers, return indices of the two numb ...
- 期末项目之 Json文件
Github上的json文件: https://raw.githubusercontent.com/color-me/first/master/b
- nodejs(8) 使用ejs渲染动态页面
使用ejs渲染动态页面 步骤: 安装 ejs 模板引擎npm i ejs -S 使用 app.set() 配置默认的模板引擎 app.set('view engine', 'ejs') 使用 app. ...
- Vue-router(5)之 路由的before家族
beforeEach方法 import Vue from 'vue' import Router from 'vue-router' import Son1 from '@/view/New/son1 ...
- [极客大挑战 2019]LoveSQL
0x00 知识点 1:万能密码登陆 2:登陆后直接使用联合查询注入 0x01解题 登陆后进行简单测试发现是字符型注入 order by 测试数据库有多少字段 发现在4的时候报错,没有过滤,直接进行注入 ...
- Mac系统的SVN客户端:Snail SVN 精简版
Mac系统的SVN客户端:Snail SVN 精简版 前言 本人在公司中,使用的是windows操作系统,svn客户端自然也就使用tortoise svn.但自从男朋友给我买了台macbook pro ...