开发工具: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的更多相关文章

  1. 一分钟带你了解下MyBatis的动态SQL!

    MyBatis的强大特性之一便是它的动态SQL,以前拼接的时候需要注意的空格.列表最后的逗号等,现在都可以不用手动处理了,MyBatis采用功能强大的基于OGNL的表达式来实现,下面主要介绍下. 一. ...

  2. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  3. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  4. mybatis 使用动态SQL

    RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...

  5. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  6. 使用Mybatis实现动态SQL(一)

    使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:        *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...

  7. MyBatis探究-----动态SQL详解

    1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...

  8. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  9. mybatis中的.xml文件总结——mybatis的动态sql

    resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...

随机推荐

  1. 2019.03.21 读书笔记 ==与Equals

    首先得出一个结论:==是比较变量内存的数据,Equals是值比较.但是他们都能被重写,所以object又增加了一个RefrenceEquals不可被重写,只比较数据: [ReliabilityCont ...

  2. Unity Input.GetMouseButtonDown 拿到鼠标按键

    //点击按键,生成子弹,并射向前方 void ShootBullet() { if (Input.GetMouseButtonDown(0)) { GameObject temp_Buller = G ...

  3. 【LDAP】Openldap导入数据

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://jerry12356.blog.51cto.com/4308715/1851186 ...

  4. netty之==线程模型

    1.1 netty线程模型本质遵循了Reactor的基础线程模型,所以得先介绍Reactor模型  1.2  Reactor模型 无论是C++还是Java编写的网络框架,大多数都是基于Reactor模 ...

  5. WCF、WebAPI、WCFREST、WebService 、RPC、HTTP 概念解释

    在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Services.我分享一下我对 ...

  6. PS基础,数学,语文

    PS基础(修图) 污点修复画笔工具---设置画笔大小---设置类型(内容识别)---修改图片---完成. 修复画笔工具---设置画笔大小---设置源(取样)---修改图片---完成. 修补工具---设 ...

  7. [转]Linq语法二

    本文将讲述LINQ的基础查询(此文所有例子都使用LINQ to Object) 在此之前,我们先创建一个用于示例的数据源: Student类:表示学生,包括学号.姓名及班级 Courses类:表示学生 ...

  8. vue的ajax

    vue的ajax常见的有两种 ,一种是 vue-resource,一种是axios vue-resource: 是vue的插件,非官方库, vue1.x 使用广泛 如何使用: 先在vue的脚本架上安装 ...

  9. 102001 E

    x轴上方给你n个点,m个水平杆子, 然后q组询问,每次询问一个点,问能看到多少个点. n,q<=40000,m<=5 自闭了呀,又写了个 for(int i=1;i<(1<&l ...

  10. Xxy 的车厢调度

    有 一 个 火 车 站 , 铁 路 如 图 所 示 ,每辆火车从 A 驶入, 再从 B 方向驶出,同时它的车厢可以重新组合.假设 从 A 方向驶来的火车有 n 节(n<=1000) ,分别按照顺 ...