标签:contex when users array 查询 pre spring java 调用
mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活。
动态sql中的语法:
where标签
if标签
trim标签
set标签
switch\when标签
foreach标签
1 package com.xm.pojo; 2 /** 3 * 用户实体 4 * @author xm 5 * 6 */ 7 public class User { 8 9 private int id; 10 private String name; 11 private String username; 12 private int age; 13 private String phone; 14 private String email; 15 //无参构造函数必须有,ORM框架调用的就是无参构造函数 16 public User() { 17 18 } 19 public User(int id, String name, String username, int age, String phone, String email) { 20 super(); 21 this.id = id; 22 this.name = name; 23 this.username = username; 24 this.age = age; 25 this.phone = phone; 26 this.email = email; 27 } 28 public int getId() { 29 return id; 30 } 31 public void setId(int id) { 32 this.id = id; 33 } 34 public String getName() { 35 return name; 36 } 37 public void setName(String name) { 38 this.name = name; 39 } 40 public String getUsername() { 41 return username; 42 } 43 public void setUsername(String username) { 44 this.username = username; 45 } 46 public int getAge() { 47 return age; 48 } 49 public void setAge(int age) { 50 this.age = age; 51 } 52 public String getPhone() { 53 return phone; 54 } 55 public void setPhone(String phone) { 56 this.phone = phone; 57 } 58 public String getEmail() { 59 return email; 60 } 61 public void setEmail(String email) { 62 this.email = email; 63 } 64 65 @Override 66 public String toString() { 67 return "User [id=" + id + ", name=" + name + ", username=" + username + ", age=" + age + ", phone=" + phone 68 + ", email=" + email + "]"; 69 } 70 71 72 73 74 75 }
1 package com.xm.mapper; 2 3 /** 4 * 用户mapper接口 5 * @author xm 6 * 7 */ 8 public interface UserMapper { 9 10 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <!--用户mapper关系映射 --> 4 <mapper namespace="com.xm.mapper.UserMapper"> 5 6 </mapper>
1 package com.xm; 2 3 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.boot.test.context.SpringBootTest; 7 import org.springframework.test.context.junit4.SpringRunner; 8 9 import com.xm.mapper.UserMapper; 10 11 @RunWith(SpringRunner.class) 12 @SpringBootTest 13 public class UserTest { 14 15 @Autowired 16 private UserMapper userMapper; 17 18 19 }
1 /** 2 * 根据条件查出学生 3 * @param user 4 * @return 5 */ 6 public List<User> getUser(User user);
1 <select id="getUser" resultType="user"> 2 select * from user where 1=1 3 <if test="id != null">and id=#{id} </if> 4 <if test="age != null">and age=#{age} </if> 5 <if test="name != null">and name=#{name}</if> 6 </select>
1 @Autowired 2 private UserMapper userMapper; 3 4 @Test 5 public void getUserTest() { 6 User user = new User(null , "小明" , null , 12 , null , null); 7 List<User> users = userMapper.getUser(user); 8 System.out.println(users); 9 10 }
where 1=1 在这里的作用是什么呢?可以不用它吗?
1 <select id="getUser" resultType="user"> 2 select * from user 3 <where> 4 <if test="id != null">and id=#{id} </if> 5 <if test="age != null">and age=#{age} </if> 6 <if test="name != null">and name=#{name}</if> 7 </where> 8 </select>
and id=#{id} 换成 id=#{id} and 会怎样?
1 <select id="getUser" resultType="user"> 2 select * from user 3 <trim prefix="where" suffixOverrides="and"> 4 <if test="id != null">id=#{id} and</if> 5 <if test="age != null">age=#{age} and</if> 6 <if test="name != null">name=#{name} and</if> 7 </trim> 8 </select>
trim标签下的四个属性:
prefix:在标签开始添加上该字符串
suffixOverrides:在标签末尾去除上该字符串
suffix:在标签末尾添加上该字符串
prefixOverrides:在标签开始去除上该字符串
用在update语句中,如果字段参数不为null,则修改此参数
1 /** 2 * 根据id修改所有非空字段 3 * @param user 4 */ 5 public void updateUserById(User user);
1 <update id="updateUserById"> 2 update user 3 <set> 4 <if test="name != null">name=#{name},</if> 5 <if test="age != null">age=#{age},</if> 6 <if test="username != null">username=#{username},</if> 7 <if test="email != null">email=#{email},</if> 8 <if test="phone != null">phone=#{phone}</if> 9 </set> 10 <where> 11 id=#{id} 12 </where> 13 </update>
1 @Test 2 public void updateUserTest() { 3 4 User user = new User(1, null, null, null, "12545564454", "14548445@qq.com"); 5 userMapper.updateUserById(user); 6 7 }
满足id!=null查询id,
否则,看满足age否,
接着,看name是否满足,
最后,按age>10查询
1 <select id="getUser" resultType="user"> 2 select * from user where 3 <choose> 4 <when test="id != null">id=#{id} </when> 5 <when test="age != null">age=#{age}</when> 6 <when test="name != null">name=#{name} </when> 7 <otherwise>age>10</otherwise> 8 </choose> 9 </select>
查出多个id的user
1 /** 2 * 根据id批量查询 3 * @param ids 4 * @return 5 */ 6 public List<User> listById(List<Integer> ids);
1 <select id="listById" resultType="user" parameterType="list"> 2 select * from user where id in 3 <foreach collection="list" item="id" separator="," open="(" close=")"> 4 #{id} 5 </foreach> 6 </select>
1 @Test 2 public void listTest() { 3 4 List<User> users = userMapper.listById(Arrays.asList(1)); 5 System.out.println(users); 6 7 }
foreach标签下的所有属性:
collection:获取的集合名,如果是list集合,springboot会把它的key值默认封装为list
item:遍历的单个属性值
separator:拼接隔离的字符串
open:在循环的开始拼接的字符串
close:在循环的结束拼接的字符串
index:索引,在map中作为key
2018-07-04
9、SpringBoot+Mybatis整合------动态sql
标签:contex when users array 查询 pre spring java 调用
原文地址:https://www.cnblogs.com/TimerHotel/p/springboot_matatis_09.html