基本查询

  1. /**
  2. * id=?
  3. */
  4. @Test
  5. void selectById() {
  6. User user = mapper.selectById(1087982257332887553L);
  7. System.out.println(user);
  8. }
  9. /**
  10. * id IN ( ? , ? , ? )
  11. */
  12. @Test
  13. void retrieveByIds() {
  14. List<User> users = mapper.selectBatchIds(Arrays.asList(8, 9, 10));
  15. users.forEach(System.out::println);
  16. }
  17. /**
  18. * 根据map查询,key为column,value为具体的值
  19. * name = ? AND age = ?
  20. */
  21. @Test
  22. void retrieveByMap() {
  23. Map<String, Object> map = new HashMap<>();
  24. map.put("name", "大boss");
  25. map.put("age", "40");
  26. List<User> users = mapper.selectByMap(map);
  27. users.forEach(System.out::println);
  28. }

条件构造器查询

  1. /**
  2. * 名字中包含雨并且年龄小于40
  3. * name like '%雨%' and age<40
  4. */
  5. @Test
  6. void selectByWrapper1() {
  7. QueryWrapper<User> wrapper = new QueryWrapper<>();
  8. wrapper.like("name", "雨")
  9. .lt("age", 40);
  10. List<User> users = mapper.selectList(wrapper);
  11. users.forEach(System.out::println);
  12. }
  13. /**
  14. * 名字中包含雨年并且龄大于等于20且小于等于40并且email不为空
  15. * name like '%雨%' and age between 20 and 40 and email is not null
  16. */
  17. @Test
  18. void selectByWrapper2() {
  19. QueryWrapper<User> wrapper = new QueryWrapper<>();
  20. wrapper.like("name", "雨")
  21. .between("age", 20, 40)
  22. .isNotNull("email");
  23. List<User> users = mapper.selectList(wrapper);
  24. users.forEach(System.out::println);
  25. }
  26. /**
  27. * 名字为王姓或者年龄大于等于25,按照年龄降序排列,年龄相同按照id升序排列
  28. * name like '王%' or age>=25 order by age desc,id asc
  29. */
  30. @Test
  31. void selectByWrapper3() {
  32. QueryWrapper<User> wrapper = new QueryWrapper<>();
  33. wrapper.likeRight("name", "王")
  34. .or().ge("age", 25)
  35. .orderByDesc("age")
  36. .orderByAsc("id");
  37. List<User> users = mapper.selectList(wrapper);
  38. users.forEach(System.out::println);
  39. }
  40. /**
  41. * 创建日期为2019年2月14日并且直属上级为名字为王姓
  42. * date_format(create_time,'%Y-%m-%d')='2019-02-14' and
  43. * manager_id in (select id from user where name like '王%')
  44. */
  45. @Test
  46. void selectByWrapper4() {
  47. QueryWrapper<User> wrapper = new QueryWrapper<>();
  48. wrapper.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14")
  49. .inSql("manager_id", "select id from user where name like '王%'");
  50. List<User> users = mapper.selectList(wrapper);
  51. users.forEach(System.out::println);
  52. }
  53. /**
  54. * and中传入lambda
  55. * <p>
  56. * 名字为王姓并且(年龄小于40或邮箱不为空)
  57. * name like '王%' and (age<40 or email is not null)
  58. */
  59. @Test
  60. void selectByWrapper5() {
  61. QueryWrapper<User> wrapper = new QueryWrapper<>();
  62. wrapper.likeRight("name", "王").
  63. and(wq -> wq.lt("age", 40)
  64. .or().isNotNull("email"));
  65. List<User> users = mapper.selectList(wrapper);
  66. users.forEach(System.out::println);
  67. }
  68. /**
  69. * 名字为王姓或者(年龄小于40并且年龄大于20并且邮箱不为空)
  70. * name like '王%' or (age<40 and age>20 and email is not null)
  71. */
  72. @Test
  73. void selectByWrapper6() {
  74. QueryWrapper<User> wrapper = new QueryWrapper<>();
  75. wrapper.likeRight("name", "王").
  76. or(wq -> wq.lt("age", 40)
  77. .gt("age", 20)
  78. .isNotNull("email"));
  79. List<User> users = mapper.selectList(wrapper);
  80. users.forEach(System.out::println);
  81. }
  82. /**
  83. * ( 年龄小于40或邮箱不为空)并且名字为王姓
  84. * (age<40 or email is not null) and name like '王%'
  85. */
  86. @Test
  87. void selectByWrapper7() {
  88. QueryWrapper<User> wrapper = new QueryWrapper<>();
  89. wrapper.nested(wq -> wq.lt("age", 40)
  90. .or().isNotNull("email"))
  91. .likeRight("name", "王");
  92. List<User> users = mapper.selectList(wrapper);
  93. users.forEach(System.out::println);
  94. }
  95. /**
  96. * 年龄为30、31、34、35
  97. */
  98. @Test
  99. void selectByWrapper8() {
  100. QueryWrapper<User> wrapper = new QueryWrapper<>();
  101. wrapper.in("age", Arrays.asList(30, 31, 34, 35));
  102. List<User> users = mapper.selectList(wrapper);
  103. users.forEach(System.out::println);
  104. }
  105. /**
  106. * 只返回满足条件的其中一条语句即可 limit 1
  107. */
  108. @Test
  109. void selectByWrapper9() {
  110. QueryWrapper<User> wrapper = new QueryWrapper<>();
  111. wrapper.in("age", Arrays.asList(30, 31, 34, 35)).last("limit 1");
  112. List<User> users = mapper.selectList(wrapper);
  113. users.forEach(System.out::println);
  114. }

指定列与排除列

  1. /**
  2. * 查询指定列
  3. * SELECT id,name FROM user WHERE (name LIKE ?)
  4. */
  5. @Test
  6. void selectPart1() {
  7. QueryWrapper<User> wrapper = new QueryWrapper<>();
  8. wrapper.select("id", "name").like("name", "雨");
  9. List<User> users = mapper.selectList(wrapper);
  10. users.forEach(System.out::println);
  11. }
  12. /**
  13. * 排除指定列
  14. * SELECT id,name,age,manager_id FROM user WHERE (name LIKE ?)
  15. */
  16. @Test
  17. void selectPart2() {
  18. QueryWrapper<User> wrapper = new QueryWrapper<>();
  19. wrapper.select(User.class, info -> !info.getColumn().equals("create_time") &&
  20. !info.getColumn().equals("email")
  21. ).like("name", "雨");
  22. List<User> users = mapper.selectList(wrapper);
  23. users.forEach(System.out::println);
  24. }

Condition处理参数 拼接动态语句

  1. @Test
  2. void testCondition(){
  3. String name = "王";
  4. String email = "";
  5. //withOutCondition(name,email);
  6. withCondition(name,email);
  7. }
  8. private void withOutCondition(String name,String email){
  9. QueryWrapper<User> wrapper = new QueryWrapper<>();
  10. if(StringUtils.isNotBlank(name)){
  11. wrapper.like("name",name);
  12. }
  13. if(StringUtils.isNotBlank(email)){
  14. wrapper.like("email",email);
  15. }
  16. List<User> users = mapper.selectList(wrapper);
  17. users.forEach(System.out::println);
  18. }
  19. private void withCondition(String name,String email){
  20. QueryWrapper<User> wrapper = new QueryWrapper<>();
  21. wrapper.like(StringUtils.isNotBlank(name),"name",name)
  22. .like(StringUtils.isNotBlank(email),"email",email);
  23. List<User> users = mapper.selectList(wrapper);
  24. users.forEach(System.out::println);
  25. }

以实体作为参数

  1. /**
  2. * WHERE name=? AND age=?
  3. * 以实体为参数 和普通设置参数 互不干扰, 都会存在, 使用时需慎重
  4. * 以实体为参数,默认是等值的,需要使用@TableField注解,并注明sqlCondition
  5. */
  6. @Test
  7. void selectByWrapperEntity(){
  8. User user = new User();
  9. user.setName("天乔巴夏");
  10. user.setAge(20);
  11. QueryWrapper<User> wrapper = new QueryWrapper<>(user);
  12. List<User> users = mapper.selectList(wrapper);
  13. users.forEach(System.out::println);
  14. }
  15. @TableField(value = "name",condition = SqlCondition.LIKE) //指定字段名
  16. private String name;

AllEq的使用

  1. @Test
  2. void selectByWrapperAllEq(){
  3. QueryWrapper<User> wrapper = new QueryWrapper<>();
  4. Map<String,Object> params = new HashMap<>();
  5. params.put("name","天乔巴夏");
  6. params.put("age",null); // age is null , 可以通过 下面这句设置 false
  7. wrapper.allEq(params,false);
  8. List<User> users = mapper.selectList(wrapper);
  9. users.forEach(System.out::println);
  10. }
  11. @Test
  12. void selectByWrapperAllEq2() {
  13. QueryWrapper<User> wrapper = new QueryWrapper<>();
  14. Map<String, Object> params = new HashMap<>();
  15. params.put("name", "天乔巴夏");
  16. params.put("age", null); // age is null , 可以通过 下面这句设置 false
  17. wrapper.allEq((k, v) -> !k.equals("name"), params, false);
  18. List<User> users = mapper.selectList(wrapper);
  19. users.forEach(System.out::println);
  20. }

自定义查询

注解形式

  1. public interface UserMapper extends BaseMapper<User> {
  2. @Select("select * from user ${ew.customSqlSegment}")
  3. List<User> selectAll(@Param(Constants.WRAPPER)Wrapper<User> wrapper);
  4. }
  5. /**
  6. * 测试自定义方法 注解
  7. */
  8. @Test
  9. void selectByCustomAnno(){
  10. QueryWrapper<User> wrapper = new QueryWrapper<>();
  11. wrapper.eq("name","天乔巴夏");
  12. List<User> users = mapper.selectAll(wrapper);
  13. users.forEach(System.out::println);
  14. }

xml形式

  1. public interface UserMapper extends BaseMapper<User> {
  2. List<User> selectAll2(@Param(Constants.WRAPPER)Wrapper<User> wrapper);
  3. }
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  6. <mapper namespace="com.hyh.mybatisplus.mapper.UserMapper">
  7. <select id="selectAll2" resultType="com.hyh.mybatisplus.entity.User">
  8. select * from user ${ew.customSqlSegment}
  9. </select>
  10. </mapper>

其他的查询

  1. /**
  2. * 优雅返回指定字段的结果
  3. */
  4. @Test
  5. void selectByWrapperMaps1() {
  6. QueryWrapper<User> wrapper = new QueryWrapper<>();
  7. wrapper.like("name", "雨")
  8. .lt("age", 40)
  9. .select("id","name");
  10. List<Map<String, Object>> mapList = mapper.selectMaps(wrapper);
  11. mapList.forEach(System.out::println);
  12. }
  13. /**
  14. * 按照直属上级分组,查询每组的平均年龄、最大年龄、最小年龄。
  15. * 并且只取年龄总和小于500的组。
  16. *
  17. * select avg(age) avg_age,min(age) min_age,max(age) max_age
  18. * from user
  19. * group by manager_id
  20. * having sum(age) <500
  21. */
  22. @Test
  23. void selectByWrapperMaps2() {
  24. QueryWrapper<User> wrapper = new QueryWrapper<>();
  25. wrapper.select("avg(age) avg_age","min(age) min_age","max(age) max_age")
  26. .groupBy("manager_id").having("sum(age)<{0}",500);
  27. List<Map<String, Object>> mapList = mapper.selectMaps(wrapper);
  28. mapList.forEach(System.out::println);
  29. }
  30. /**
  31. * 查个数
  32. */
  33. @Test
  34. void selectCount() {
  35. QueryWrapper<User> wrapper = new QueryWrapper<>();
  36. wrapper.like("name","雨");
  37. Integer cnt = mapper.selectCount(wrapper);
  38. System.out.println(cnt);
  39. }
  40. /**
  41. * 查一个实体
  42. */
  43. @Test
  44. void selectOne(){
  45. QueryWrapper<User> wrapper = new QueryWrapper<>();
  46. wrapper.eq("name","天乔巴夏");
  47. User user = mapper.selectOne(wrapper);
  48. System.out.println(user);
  49. }
  50. /**
  51. * lambda构造,编译时检查字段信息,防止误写
  52. */
  53. @Test
  54. void selectLambda(){
  55. //LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
  56. //LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
  57. LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();
  58. lambdaQuery.like(User::getName,"雨").lt(User::getAge,40);
  59. // where name like '%雨%'
  60. List<User> users = mapper.selectList(lambdaQuery);
  61. users.forEach(System.out::println);
  62. }

Mybatis Plus各种查询方法

  1. package com.xiao.permission_system;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
  5. import com.mysql.cj.util.StringUtils;
  6. import com.xiao.permission_system.entity.UserInfo;
  7. import com.xiao.permission_system.mapper.UserInfoMapper;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import org.springframework.test.context.junit4.SpringRunner;
  13. import java.util.Arrays;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;;
  17. @RunWith(SpringRunner.class)
  18. @SpringBootTest
  19. public class PermissionSystemApplicationTests {
  20. @Test
  21. public void contextLoads() {
  22. }
  23. @Autowired
  24. private UserInfoMapper userInfoMapper;
  25. /**
  26. * 普通查询
  27. */
  28. @Test
  29. public void selectById() {
  30. UserInfo userInfo = userInfoMapper.selectById(123);
  31. System.out.println(userInfo);
  32. }
  33. /**
  34. * 批量查询
  35. */
  36. @Test
  37. public void selectByIds() {
  38. List<Long> ids = Arrays.asList(123L,124L,125L);
  39. List<UserInfo> userInfo = userInfoMapper.selectBatchIds(ids);
  40. System.out.println(userInfo);
  41. }
  42. /**
  43. * 名字包含娟并且年龄小雨30
  44. */
  45. @Test
  46. public void selectByWrapper() {
  47. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  48. queryWrapper.like("username","娟").lt("age",30);
  49. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  50. userInfoList.forEach(System.out::println);
  51. }
  52. /**
  53. * 名字包含娟并且年龄大雨等于20且小于等于40并且email不为空
  54. */
  55. @Test
  56. public void selectByWrapper2() {
  57. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  58. queryWrapper.like("username","娟").between("age",20,30).isNotNull("email");
  59. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  60. userInfoList.forEach(System.out::println);
  61. }
  62. /**
  63. * 名字姓肖或者年量大雨等于20,按照年龄降序排列,年龄相同按照id生序排列
  64. */
  65. @Test
  66. public void selectByWrapper3() {
  67. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  68. queryWrapper.likeRight("username","肖")
  69. .or().ge("age",20).orderByDesc("age").orderByAsc("id");
  70. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  71. userInfoList.forEach(System.out::println);
  72. }
  73. /**
  74. * 创建日期为2019年10月2日并且直属上级名字为王姓
  75. */
  76. @Test
  77. public void selectByWrapper4() {
  78. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  79. queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}","2019-10-07")
  80. .inSql("parent_id","select id from user where username like '王%'");
  81. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  82. userInfoList.forEach(System.out::println);
  83. }
  84. /**
  85. * 名字为王姓并且(年龄小于40或邮箱不为空)
  86. */
  87. @Test
  88. public void selectByWrapper5() {
  89. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  90. queryWrapper.likeRight("username","王")
  91. .and(wq->wq.lt("age",40))
  92. .or().isNotNull("email");
  93. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  94. userInfoList.forEach(System.out::println);
  95. }
  96. /**
  97. * 名字为王姓并且(年龄小于40并且大与20或邮箱不为空)
  98. */
  99. @Test
  100. public void selectByWrapper6() {
  101. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  102. queryWrapper.likeRight("username","王")
  103. .and(wq->wq.lt("age",40).gt("age",20))
  104. .or().isNotNull("email");
  105. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  106. userInfoList.forEach(System.out::println);
  107. }
  108. /**
  109. * (年龄小于40并且大与20或邮箱不为空)名字为王姓并且
  110. */
  111. @Test
  112. public void selectByWrapper7() {
  113. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  114. queryWrapper.nested(wq->wq.lt("age",40))
  115. .or().isNotNull("email")
  116. .likeRight("username","王");
  117. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  118. userInfoList.forEach(System.out::println);
  119. }
  120. /**
  121. * 年龄23,30,40
  122. */
  123. @Test
  124. public void selectByWrapper8() {
  125. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  126. queryWrapper.in("age",Arrays.asList(20,30,40));
  127. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  128. userInfoList.forEach(System.out::println);
  129. }
  130. /**
  131. * 只返回满足条件的其中一条语句即可
  132. */
  133. @Test
  134. public void selectByWrapper9() {
  135. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  136. queryWrapper.in("age",Arrays.asList(20,30,40)).last("limit 1");
  137. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  138. userInfoList.forEach(System.out::println);
  139. }
  140. /**
  141. * 名字中包含雨并且年龄小于40(只取id,username)
  142. */
  143. @Test
  144. public void selectByWrapper10() {
  145. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  146. queryWrapper.select("id","username").like("username","雨").lt("age",40);
  147. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  148. userInfoList.forEach(System.out::println);
  149. }
  150. /**
  151. * 名字中包含雨并且年龄小于40(不取create_time,parent_id两个字段,即不列出全部字段)
  152. */
  153. @Test
  154. public void selectByWrapper11() {
  155. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  156. queryWrapper.like("username","雨").lt("age",40)
  157. .select(UserInfo.class,info->!info.getColumn().equals("create_time")&&
  158. !info.getColumn().equals("parent_id"));
  159. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  160. userInfoList.forEach(System.out::println);
  161. }
  162. /**
  163. * 姓名和邮箱不为空
  164. */
  165. public void testCondition() {
  166. String username = "王";
  167. String email = "";
  168. condition(username,email);
  169. }
  170. private void condition(String username,String email){
  171. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  172. queryWrapper.like(StringUtils.isNullOrEmpty(username),"name",username)
  173. .like(StringUtils.isNullOrEmpty(email),"email",email);
  174. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  175. userInfoList.forEach(System.out::println);
  176. }
  177. /**
  178. * 实体作为条件构造器方法的参数
  179. */
  180. @Test
  181. public void selectByWrapperEntity() {
  182. UserInfo whereUser = new UserInfo();
  183. whereUser.setUsername("xiaojuan");
  184. whereUser.setAge(22);
  185. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>(whereUser);
  186. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  187. userInfoList.forEach(System.out::println);
  188. }
  189. /**
  190. * AllEq用法
  191. */
  192. @Test
  193. public void selectByWrapperAllEq() {
  194. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  195. Map<String, Object> params = new HashMap<String, Object>();
  196. params.put("nuserame","xiaojuan");
  197. params.put("age",null);
  198. queryWrapper.allEq(params);
  199. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  200. userInfoList.forEach(System.out::println);
  201. }
  202. /**
  203. * AllEq用法(排除不是条件的字段)
  204. */
  205. @Test
  206. public void selectByWrapperAllEq2() {
  207. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  208. Map<String, Object> params = new HashMap<String, Object>();
  209. params.put("nuserame","xiaojuan");
  210. params.put("age",null);
  211. queryWrapper.allEq((k,v)->!k.equals("name"),params);
  212. List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
  213. userInfoList.forEach(System.out::println);
  214. }
  215. /**
  216. * selectMaps
  217. */
  218. @Test
  219. public void selectByWrapperMaps() {
  220. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  221. queryWrapper.like("name","肖").lt("age",40);
  222. List<Map<String,Object>> userInfoList = userInfoMapper.selectMaps(queryWrapper);
  223. userInfoList.forEach(System.out::println);
  224. }
  225. /**
  226. * 按照直属上级分组,查询每组的平均年龄,最大年龄,最小年龄。并且只取年龄总和小于500的组
  227. */
  228. @Test
  229. public void selectByWrapperMaps2() {
  230. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  231. queryWrapper.select("avg(age) avg_age","min(min) min_age","max(age) max_age")
  232. .groupBy("parent_id").having("sum(age)<{0}",500);
  233. List<Map<String,Object>> userInfoList = userInfoMapper.selectMaps(queryWrapper);
  234. userInfoList.forEach(System.out::println);
  235. }
  236. /**
  237. * selectObjs
  238. */
  239. @Test
  240. public void selectByWrapperObjs() {
  241. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  242. queryWrapper.select("id","name").like("name","肖").lt("age",40);
  243. List<Object> userInfoList = userInfoMapper.selectObjs(queryWrapper);
  244. userInfoList.forEach(System.out::println);
  245. }
  246. /**
  247. * selectCount
  248. */
  249. @Test
  250. public void selectByWrapperCount() {
  251. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  252. queryWrapper.like("name","肖").lt("age",40);
  253. Integer count = userInfoMapper.selectCount(queryWrapper);
  254. System.out.println(count);
  255. }
  256. /**
  257. * selectOne
  258. */
  259. @Test
  260. public void selectByWrapperSelectOne() {
  261. QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
  262. queryWrapper.like("name","肖").lt("age",40);
  263. UserInfo user = userInfoMapper.selectOne(queryWrapper);
  264. System.out.println(user);
  265. }
  266. /**
  267. * 使用Lambda
  268. */
  269. @Test
  270. public void selectLambda() {
  271. // LambdaQueryWrapper<UserInfo> lambda = new QueryWrapper<UserInfo>().lambda();
  272. LambdaQueryWrapper<UserInfo> lambda = new LambdaQueryWrapper<UserInfo>();
  273. lambda.like(UserInfo::getUsername,"娟").lt(UserInfo::getAge,40);
  274. List<UserInfo> userInfoList = userInfoMapper.selectList(lambda);
  275. userInfoList.forEach(System.out::println);
  276. }
  277. /**
  278. * 使用Lambda,名字为王姓(年龄小于40或邮箱不为空)
  279. */
  280. @Test
  281. public void selectLambd2() {
  282. LambdaQueryWrapper<UserInfo> lambda = new LambdaQueryWrapper<UserInfo>();
  283. lambda.like(UserInfo::getUsername,"娟")
  284. .and(lqw->lqw.lt(UserInfo::getAge,40).or().isNotNull(UserInfo::getEmail));
  285. List<UserInfo> userInfoList = userInfoMapper.selectList(lambda);
  286. userInfoList.forEach(System.out::println);
  287. }
  288. /**
  289. * 使用Lambda链式
  290. */
  291. @Test
  292. public void selectLambd3() {
  293. List<UserInfo> userInfoList = new LambdaQueryChainWrapper<UserInfo>(userInfoMapper)
  294. .like(UserInfo::getUsername,"娟").ge(UserInfo::getAge,20).list();
  295. userInfoList.forEach(System.out::println);
  296. }
  297. }