9、SpringBoot+Mybatis整合------动态sql
开发工具:STS
前言:
mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活。
动态sql中的语法:
where标签
if标签
trim标签
set标签
switch\when标签
foreach标签
一、搭建项目
1.创建用户表:

2.添加实体:
package com.xm.pojo;
/**
* 用户实体
* @author xm
*
*/
public class User { private int id;
private String name;
private String username;
private int age;
private String phone;
private String email;
//无参构造函数必须有,ORM框架调用的就是无参构造函数
public User() { }
public User(int id, String name, String username, int age, String phone, String email) {
super();
this.id = id;
this.name = name;
this.username = username;
this.age = age;
this.phone = phone;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", username=" + username + ", age=" + age + ", phone=" + phone
+ ", email=" + email + "]";
} }
User.java
3.添加mapper接口:
package com.xm.mapper; /**
* 用户mapper接口
* @author xm
*
*/
public interface UserMapper { }
UserMapper.java
4.添加mapper映射:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--用户mapper关系映射 -->
<mapper namespace="com.xm.mapper.UserMapper"> </mapper>
UserMapper.xml
5.添加测试类:
package com.xm; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import com.xm.mapper.UserMapper; @RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest { @Autowired
private UserMapper userMapper; }
UserText.java
二、动态sql
1.if标签的使用
(1)添加mapper接口:
/**
* 根据条件查出学生
* @param user
* @return
*/
public List<User> getUser(User user);
(2)添加mapper映射:
<select id="getUser" resultType="user">
select * from user where 1=1
<if test="id != null">and id=#{id} </if>
<if test="age != null">and age=#{age} </if>
<if test="name != null">and name=#{name}</if>
</select>
(3)添加测试:
@Autowired
private UserMapper userMapper; @Test
public void getUserTest() {
User user = new User(null , "小明" , null , 12 , null , null);
List<User> users = userMapper.getUser(user);
System.out.println(users); }
(4)测试结果:

(5)思考:
where 1=1 在这里的作用是什么呢?可以不用它吗?
2.where标签的使用
(1)更改mapper映射:
<select id="getUser" resultType="user">
select * from user
<where>
<if test="id != null">and id=#{id} </if>
<if test="age != null">and age=#{age} </if>
<if test="name != null">and name=#{name}</if>
</where>
</select>
(2)测试结果:

(3)思考:
and id=#{id} 换成 id=#{id} and 会怎样?
3.trim标签的使用
(1)更改mapper映射:
<select id="getUser" resultType="user">
select * from user
<trim prefix="where" suffixOverrides="and">
<if test="id != null">id=#{id} and</if>
<if test="age != null">age=#{age} and</if>
<if test="name != null">name=#{name} and</if>
</trim>
</select>
(2)测试结果:

(3)分析:
trim标签下的四个属性:
prefix:在标签开始添加上该字符串
suffixOverrides:在标签末尾去除上该字符串
suffix:在标签末尾添加上该字符串
prefixOverrides:在标签开始去除上该字符串
4.set标签的使用
(1)描述需求:
用在update语句中,如果字段参数不为null,则修改此参数
(2)添加mapper接口:
/**
* 根据id修改所有非空字段
* @param user
*/
public void updateUserById(User user);
(3)添加mapper映射:
<update id="updateUserById">
update user
<set>
<if test="name != null">name=#{name},</if>
<if test="age != null">age=#{age},</if>
<if test="username != null">username=#{username},</if>
<if test="email != null">email=#{email},</if>
<if test="phone != null">phone=#{phone}</if>
</set>
<where>
id=#{id}
</where>
</update>
(4)添加测试方法:
@Test
public void updateUserTest() { User user = new User(1, null, null, null, "12545564454", "14548445@qq.com");
userMapper.updateUserById(user); }
(5)测试结果

5.switch\when标签的使用
(1)描述需求:
满足id!=null查询id,
否则,看满足age否,
接着,看name是否满足,
最后,按age>10查询
(2)更改mapper映射:
<select id="getUser" resultType="user">
select * from user where
<choose>
<when test="id != null">id=#{id} </when>
<when test="age != null">age=#{age}</when>
<when test="name != null">name=#{name} </when>
<otherwise>age>10</otherwise>
</choose>
</select>
(3)测试结果:

6.foreach标签的使用
(1)需求描述:
查出多个id的user
(2)添加mapper接口:
/**
* 根据id批量查询
* @param ids
* @return
*/
public List<User> listById(List<Integer> ids);
(3)添加mapper映射:
<select id="listById" resultType="user" parameterType="list">
select * from user where id in
<foreach collection="list" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
(4)添加测试用例:
@Test
public void listTest() { List<User> users = userMapper.listById(Arrays.asList(1));
System.out.println(users); }
(5)分析:
foreach标签下的所有属性:
collection:获取的集合名,如果是list集合,springboot会把它的key值默认封装为list
item:遍历的单个属性值
separator:拼接隔离的字符串
open:在循环的开始拼接的字符串
close:在循环的结束拼接的字符串
index:索引,在map中作为key
2018-07-04
9、SpringBoot+Mybatis整合------动态sql的更多相关文章
- 一分钟带你了解下MyBatis的动态SQL!
MyBatis的强大特性之一便是它的动态SQL,以前拼接的时候需要注意的空格.列表最后的逗号等,现在都可以不用手动处理了,MyBatis采用功能强大的基于OGNL的表达式来实现,下面主要介绍下. 一. ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- 使用Mybatis实现动态SQL(一)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- mybatis中的.xml文件总结——mybatis的动态sql
resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...
随机推荐
- Django-4 模板层
你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...
- [转]LazyLoad.js及scrollLoading.js
本文转自:http://blog.csdn.net/ning109314/article/details/7042829 目前图片延迟加载主要分两大块,一是触发加载(根据滚动条位置加载图片):二是自动 ...
- Murano Setup Steps
1. Select a Linux Distribution Only Ubuntu 14.04 (Trusty), Fedora 21 (or Fedora 22) and CentOS/RHEL ...
- 监听outlook新邮件
using System; using System.Linq; using Microsoft.Office.Interop.Outlook; using System.Collections.Ge ...
- 关闭ubuntu讨厌的内部错误提示
修改/etc/default/apport 浏览下/etc/init/apport.conf 内容你会发现,控制此服务是否启动的是/etc/default/apport 所以把/etc/default ...
- Unity3D跨平台动态库编译---记kcp基于CMake的各平台构建实践
一 为什么需要动态库 1)提供原生代码(native code)的支持,也叫原生插件,但是我实践的是c/cpp跨平台动态库,这里不具体涉及安卓平台java库和ios平台的objectc库构建. 2)某 ...
- Unity C# 用枚举(enum)制作复选框
最近在项目中做测试脚本用到一些布尔值做方法的开关,突然想到可以制作一个复选框控制开关. 首先搜集网上的资料,基本大同小异,这里就不多做解释了,代码附上: public class EnumFlagsA ...
- DIV+CSS如何让图片和文字在同一行!
在div+css布局中,如果一行(或一个DIV)内容中有图片和文字的话,图片和文字往往会一个在上一个在下,这是一个新手都会遇到问题,我的解决方法有三: 1.添加CSS属性:vertical-align ...
- c#-day02学习笔记
类型转化 为什么要类型转化:因为C#语言是强类型的语言,所以区分了很多的类型,类型和类型之间是不能直接赋值的,如果要赋值 就需要转换类型 类型转换分为两大类: 第一类:隐式转换 隐式转换是系统默认的转 ...
- 斗鱼扩展--DouyuRoom使用说明(十四)
1.从 https://pan.baidu.com/s/1yBfZFtcakbDxmyas0VCpRw 下载 DouyuRoom.zip 然后解压到一个目录,我是放在C盘根目录下的,你们随意.然后解压 ...