MyBatisPlus 学习笔记
作者:故事我忘了
个人微信公众号:程序猿的月光宝盒

https://mybatis.plus/guide/

本篇基于springboot,mybatis Plus的版本为3.4.2

本篇对应的github地址

https://github.com/monkeyKinn/StudyMyBatisPlus

觉得有用给个Star哦~~~~

Star

Star

Star

  1. #创建用户表
  2. CREATE TABLE user (
  3. id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主键',
  4. name VARCHAR(30) DEFAULT NULL COMMENT '姓名',
  5. age INT(11) DEFAULT NULL COMMENT '年龄',
  6. email VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
  7. manager_id BIGINT(20) DEFAULT NULL COMMENT '直属上级id',
  8. create_time DATETIME DEFAULT NULL COMMENT '创建时间',
  9. CONSTRAINT manager_fk FOREIGN KEY (manager_id)
  10. REFERENCES user (id)
  11. ) ENGINE=INNODB CHARSET=UTF8;
  12. #初始化数据:
  13. INSERT INTO user (id, name, age, email, manager_id
  14. , create_time)
  15. VALUES (1087982257332887553, '大boss', 40, 'boss@baomidou.com', NULL
  16. , '2019-01-11 14:20:20'),
  17. (1088248166370832385, '王天风', 25, 'wtf@baomidou.com', 1087982257332887553
  18. , '2019-02-05 11:12:22'),
  19. (1088250446457389058, '李艺伟', 28, 'lyw@baomidou.com', 1088248166370832385
  20. , '2019-02-14 08:31:16'),
  21. (1094590409767661570, '张雨琪', 31, 'zjq@baomidou.com', 1088248166370832385
  22. , '2019-01-14 09:15:15'),
  23. (1094592041087729666, '刘红雨', 32, 'lhm@baomidou.com', 1088248166370832385
  24. , '2019-01-14 09:48:16');
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>mysql</groupId>
  7. <artifactId>mysql-connector-java</artifactId>
  8. <scope>runtime</scope>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.projectlombok</groupId>
  12. <artifactId>lombok</artifactId>
  13. <optional>true</optional>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-test</artifactId>
  18. <scope>test</scope>
  19. <exclusions>
  20. <exclusion>
  21. <groupId>org.junit.vintage</groupId>
  22. <artifactId>junit-vintage-engine</artifactId>
  23. </exclusion>
  24. </exclusions>
  25. </dependency>
  26. <dependency>
  27. <groupId>com.baomidou</groupId>
  28. <artifactId>mybatis-plus-boot-starter</artifactId>
  29. <version>3.4.2</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>junit</groupId>
  33. <artifactId>junit</artifactId>
  34. <version>4.13</version>
  35. <scope>test</scope>
  36. </dependency>
  1. spring.application.name=MyBatisPlus
  2. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://localhost:3306/mp?useSSL=false&serverTimezone=GMT%2B8
  4. spring.datasource.username=root
  5. spring.datasource.password=admin
  1. @Data
  2. public class User{
  3. /** 主键 */
  4. private Long id;
  5. /** 姓名 */
  6. private String name;
  7. /** 年龄 */
  8. private Integer age;
  9. /** 邮件 */
  10. private String email;
  11. /** 直属上级id */
  12. private Long managerId;
  13. /** 创建时间 */
  14. private LocalDateTime createTime;
  15. }
  1. public interface UserMapper extends BaseMapper<User> {
  2. }
  1. @SpringBootApplication
  2. // 注意这里的包扫描路径 要写到mapper所在的包名上
  3. @MapperScan("com.jsc.mybatisplus.mapper")
  4. public class MyBatisPlusApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MyBatisPlusApplication.class, args);
  7. }
  8. }
  1. @SpringBootTest
  2. @RunWith(SpringRunner.class)
  3. class MyBatisPlusApplicationTests {
  4. // 后面会用到,此为创建条件构造器
  5. private QueryWrapper<User> query = Wrappers.query();
  6. @Autowired
  7. private UserMapper userMapper;
  8. @Test
  9. void selectAllTest() {
  10. List<User> users = userMapper.selectList(null);
  11. Assertions.assertEquals(5,users.size());
  12. users.forEach(System.out::println);
  13. }
  14. }
  1. spring.application.name=MyBatisPlus
  2. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://localhost:3306/mp?useSSL=false&serverTimezone=GMT%2B8
  4. spring.datasource.username=root
  5. spring.datasource.password=admin
  6. #日志输出配置
  7. logging.level.root=warn
  8. #只想看这个包里的sql日志 trace是最低级别
  9. logging.level.com.jsc.mybatisplus.mapper=trace
  1. @Test
  2. void insertTest() {
  3. User user = new User();
  4. user.setName("老金");
  5. user.setAge(18);
  6. // 没有email 默认不给插入 没有id 是因为默认雪花id
  7. user.setManagerId(1087982257332887553L);
  8. user.setCreateTime(LocalDateTime.now());
  9. int rows = userMapper.insert(user);
  10. System.out.println("影响行数: " + rows);
  11. }
  1. @TableName("表名") //在实体类上,用来当db中表和实体类名不一致时候,指定表
  2. @TableId //在字段上,用来指定对应的主键,当数据库中的主键不是id命名时,实体类也不是id时用
  3. @TableField("字段名") //在实体类上,用来指定对应的字段名
  1. transient //关键字 无法被序列化(存到磁盘)
  1. static //关键字 只有一份 属于类
  1. @TableField(exit=false) //默认是true,数据库中存在
  1. @Test
  2. void selectByIdTest() {
  3. // 根据id查询
  4. User user = userMapper.selectById(1382699085670719489L);
  5. // User(id=1382699085670719489, name=老金, age=18, email=null, managerId=1087982257332887553, createTime=2021-04-15T22:15:26)
  6. System.out.println(user);
  7. }
  8. @Test
  9. void selectBatchIdsTest() {
  10. // 根据id批量查询
  11. List<Long> ids = Arrays.asList(1382699085670719489L, 1094592041087729666L, 1094590409767661570L);
  12. List<User> users = userMapper.selectBatchIds(ids);
  13. users.forEach(System.out::println);
  14. }
  15. @Test
  16. void selectByMapTest() {
  17. Map<String, Object> map = new HashMap<>();
  18. // map.put("name", "老金");
  19. // map的key是数据库中的字段
  20. map.put("age", 27);
  21. // WHERE name = ? AND age = ?
  22. // WHERE age = ?
  23. List<User> users = userMapper.selectByMap(map);
  24. System.out.println(users);
  25. }
  1. @Test
  2. void selectByWrapper0() {
  3. /*
  4. * 1、名字中包含雨并且年龄小于40
  5. * name like '%雨%' and age<40
  6. * 条件构造器
  7. */
  8. query.like("name", "雨").lt("age", 40);
  9. List<User> users = userMapper.selectList(query);
  10. users.forEach(System.out::println);
  11. }
  12. @Test
  13. void selectByWrapper1() {
  14. /*
  15. * 名字中包含雨年并且龄大于等于20且小于等于40并且email不为空
  16. * name like '%雨%' and age between 20 and 40 and email is not null
  17. */
  18. query.like("name", "雨").between("age", 20, 40).isNotNull("email");
  19. List<User> users = userMapper.selectList(query);
  20. users.forEach(System.out::println);
  21. }
  22. @Test
  23. void selectByWrapper2() {
  24. /*
  25. * 名字为王姓或者年龄大于等于25,按照年龄降序排列,年龄相同按照id升序排列
  26. * name like '王%' or age>=25 order by age desc,id asc
  27. */
  28. query.like("name", "王").or().ge("age", 18).orderByDesc("age").orderByAsc("id");
  29. List<User> users = userMapper.selectList(query);
  30. users.forEach(System.out::println);
  31. }
  32. @Test
  33. void selectByWrapper3() {
  34. /*
  35. * 创建日期为2019年2月14日并且直属上级为名字为王姓 --函数开头用apple拼接sql
  36. * date_format(create_time,'%Y-%m-%d')='2019-02-14' and manager_id in (select id from user where name like '王%')
  37. */
  38. query.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14")
  39. .inSql("manager_id", "select id from user where name like '王%'");
  40. List<User> users = userMapper.selectList(query);
  41. users.forEach(System.out::println);
  42. }
  43. @Test
  44. void selectByWrapper4() {
  45. /*
  46. * 名字为王姓并且(年龄小于40或邮箱不为空)
  47. * name like '王%' and (age<40 or email is not null)
  48. */
  49. query.likeRight("name","王").and(qw->qw.lt("age",40 ).or().isNotNull("email"));
  50. List<User> users = userMapper.selectList(query);
  51. users.forEach(System.out::println);
  52. }
  53. @Test
  54. void selectByWrapper5() {
  55. /*
  56. * 名字为王姓或者(年龄小于40并且年龄大于20并且邮箱不为空)
  57. * name like '王%' or (age<40 and age>20 and email is not null)
  58. */
  59. query.likeRight("name","王").or(qw->
  60. qw.lt("age",40 )
  61. .gt("age",20).isNotNull("email"));
  62. List<User> users = userMapper.selectList(query);
  63. users.forEach(System.out::println);
  64. }
  65. @Test
  66. void selectByWrapper6() {
  67. /*
  68. * (年龄小于40或邮箱不为空)并且名字为王姓,nested 正常嵌套 不带 AND 或者 OR
  69. * (age<40 or email is not null) and name like '王%'
  70. */
  71. query.nested(qw->
  72. qw.lt("age",40).or().isNotNull("email")).likeRight("name","王");
  73. List<User> users = userMapper.selectList(query);
  74. users.forEach(System.out::println);
  75. }
  76. @Test
  77. void selectByWrapper7() {
  78. /*
  79. * 年龄为30、31、34、35
  80. * age in (30、31、34、35)
  81. */
  82. query.in("age",18,30,31,34,35);
  83. List<User> users = userMapper.selectList(query);
  84. users.forEach(System.out::println);
  85. }
  1. @Test
  2. void selectByWrapper8() {
  3. /*
  4. * 10、名字中包含雨并且年龄小于40(需求1加强版)
  5. 第一种情况: select id,name -- 就用select("")
  6. from user
  7. where name like '%雨%' and age<40
  8. 第二种情况: select id,name,age,email
  9. from user
  10. where name like '%雨%' and age<40
  11. */
  12. query.like("name","雨").lt("age",40).select("name","age");
  13. List<User> users = userMapper.selectList(query);
  14. users.forEach(System.out::println);
  15. }
  16. @Test
  17. void selectByWrapper9() {
  18. /*
  19. * 10、名字中包含雨并且年龄小于40(需求1加强版)
  20. 第一种情况: select id,name -- 就用select("")
  21. from user
  22. where name like '%雨%' and age<40
  23. 第二种情况: select id,name,age,email -- 就用select("")
  24. from user
  25. where name like '%雨%' and age<40
  26. */
  27. query.like("name","雨").lt("age",40).select(User.class,info ->
  28. !info.getColumn().equals("create_time")
  29. &&!info.getColumn().equals("manager_id"));
  30. List<User> users = userMapper.selectList(query);
  31. users.forEach(System.out::println);
  32. }
  1. @Test
  2. void selectByCondition() {
  3. // 根据参数条件查询
  4. // String name = "金";
  5. String name = "玉";
  6. String age = "18";
  7. // 如果不为空,就有后面的条件加入到sql中
  8. query.like(StringUtils.isNotBlank(name),"name",name)
  9. .eq(StringUtils.isNotBlank(age),"age",age);
  10. List<User> users = userMapper.selectList(query);
  11. users.forEach(System.out::println);
  12. }
  1. @Test
  2. void selectByWrapperEntity() {
  3. /*
  4. * 使用场景:
  5. * 1、通过实体传过来数据
  6. * 2.不想永理科这样的构造器,默认是等值的
  7. * 如果不想等值,在实体类中加上注解
  8. * @TableField(condition=SqlCondition.like)
  9. * 小于的话就是="%s&lt;#{%s}"
  10. * 列名<列值
  11. */
  12. User whereUser = new User();
  13. whereUser.setName("老金");
  14. whereUser.setAge(18);
  15. query = Wrappers.query(whereUser);
  16. // query.like("name", "雨").lt("age", 40);
  17. List<User> users = userMapper.selectList(query);
  18. users.forEach(System.out::println);
  19. }
  1. @Test
  2. void selectByWrapperAllEq() {
  3. HashMap<String, Object> params = new HashMap<>();
  4. params.put("name", "王天风");
  5. params.put("age", null);
  6. // 后面的false,是忽略null值的列
  7. // query.allEq(params,false);
  8. // 前面一个函数是过滤用的 比如这里就是不包含age列
  9. query.allEq((k,v)->!"age".equals(k),params);
  10. List<User> users = userMapper.selectList(query);
  11. users.forEach(System.out::println);
  12. }
  1. @Test
  2. void selectByWrapperMaps() {
  3. /*
  4. * 使用场景:
  5. * 1.当表特别的多色时候只要查少数 几列,返回一个map,而不是属性大部分都为空的实体
  6. * 2.返回的是统计结果
  7. * 按照直属上级分组,查询每组的 平均年龄、最小龄、最大年龄。
  8. * 并且只取年龄总和小于500的组。
  9. * select
  10. * avg(age) avg_age,
  11. * min(age) min_age,
  12. * max(age) max_age
  13. * from user
  14. * group by manager_id --上级id分组
  15. * having sum(age) <500 --只有总和小于500
  16. * */
  17. // 第一种情况
  18. // query.like("name","雨").lt("age",40).select("id","name");
  19. // 第二种情况
  20. query.select("avg(age) avg_age","min(age) min_age","max(age) max_age")
  21. .groupBy("manager_id")
  22. .having("sum(age) < {0}",500);
  23. List<Map<String, Object>> users = userMapper.selectMaps(query);
  24. users.forEach(System.out::println);
  25. }
  26. @Test
  27. void selectByWrapperObjs() {
  28. query.select("id","name").like("name","雨").lt("age",40);
  29. // 返回第一列的值 只返回一列的时候用
  30. List<Object> users = userMapper.selectObjs(query);
  31. users.forEach(System.out::println);
  32. }
  33. @Test
  34. void selectByWrapperCounts() {
  35. // 不能设置查询的列名了
  36. query.like("name","雨").lt("age",40);
  37. // 查总记录数
  38. Integer count = userMapper.selectCount(query);
  39. System.out.println(count);
  40. }
  41. @Test
  42. void selectByWrapperOne() {
  43. // 不能设置查询的列名了
  44. query.like("name","老金").lt("age",40);
  45. // 只返回一条数据
  46. User user = userMapper.selectOne(query);
  47. System.out.println(user);
  48. }
  1. @Test
  2. void selectLambda() {
  3. // 创建方式
  4. // QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
  5. // LambdaQueryWrapper<User> userLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
  6. LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
  7. // 好处: 防止误写字段名
  8. // where name like '%雨%' and age<40
  9. // 前面是类名 后面是get方法,表示属性
  10. lambdaQueryWrapper.like(User::getName, "雨").lt(User::getAge, 40);
  11. List<User> users = userMapper.selectList(lambdaQueryWrapper);
  12. users.forEach(System.out::println);
  13. }
  14. @Test
  15. void selectLambda1() {
  16. /*
  17. * 名字为王姓并且(年龄小于40或邮箱不为空)
  18. * name like '王%' and (age<40 or email is not null)
  19. */
  20. LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
  21. // 好处: 防止误写字段名
  22. // 前面是类名 后面是get方法,表示属性
  23. lambdaQueryWrapper.likeRight(User::getName,"王").and(lqw->lqw.lt(User::getAge,40).or().isNotNull(User::getEmail));
  24. List<User> users = userMapper.selectList(lambdaQueryWrapper);
  25. users.forEach(System.out::println);
  26. }
  27. @Test
  28. void selectLambda2() {
  29. /*
  30. * 名字为王姓并且(年龄小于40或邮箱不为空)
  31. * name like '王%' and (age<40 or email is not null)
  32. */
  33. // 3.0.7新增创建方式
  34. List<User> users = new LambdaQueryChainWrapper<>(userMapper)
  35. .like(User::getName, "雨").ge(User::getAge, 20).list();
  36. users.forEach(System.out::println);
  37. }
  1. public interface UserMapper extends BaseMapper<User> {
  2. // 参数的注解是固定的 ew 就是WRAPPER的值
  3. @Select("select * from user ${ew.customSqlSegment}")
  4. List<User> selectAll(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
  5. }
  1. @Test
  2. void selectAllCustomize() {
  3. // 自定义的方法
  4. LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
  5. lambdaQueryWrapper.likeRight(User::getName,"王").and(lqw->lqw.lt(User::getAge,40).or().isNotNull(User::getEmail));
  6. List<User> users = userMapper.selectAll(lambdaQueryWrapper);
  7. users.forEach(System.out::println);
  8. }

在配置文件中配置接口对应的mapper文件

  1. #设置扫描路径
  2. mybatis-plus.mapper-locations=mapper/*.xml

省略图

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.jsc.mybatisplus.mapper.UserMapper">
  5. <select id="selectAll" resultType="com.jsc.mybatisplus.entity.User">
  6. <!-- 参数的注解是固定的 ew 就是WRAPPER的值-->
  7. select * from user ${ew.customSqlSegment}
  8. </select>
  9. </mapper>

接口中的sql注解就不用了

首先明确一点,mybatis分页rowbounds确实实现了分页,但是是逻辑分页/内存分页,他先把数据全查出来,load到memory中,然后给你你想要的,换句话说,是海选…你懂我意思吧…可想而知,数据爆炸的时代,你得给多内存,内存不要钱吗,海选可贵,选择又多,多了换一批时间就 ,一个道理~

Mp就提供了物理分页的插件,解决上述问题

既然是插件,肯定要配置

省略

  1. package com.jsc.mybatisplus.config;
  2. import com.baomidou.mybatisplus.annotation.DbType;
  3. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  4. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. /**
  8. * mybatis的分页配置
  9. *
  10. * @author 金聖聰
  11. * @version v1.0
  12. * @email jinshengcong@163.com
  13. * @date Created in 2021/04/16 15:48
  14. */
  15. @Configuration
  16. public class MyBatisPlusConfig {
  17. // 新版废弃
  18. // @Bean
  19. // public PaginationInterceptor paginationInterceptor() {
  20. // return new PaginationInterceptor();
  21. // }
  22. //-------------------------------------------------
  23. /*
  24. 未测试
  25. 新的分页插件,一缓和二缓遵循mybatis的规则,
  26. 需要设置 MybatisConfiguration#useDeprecatedExecutor = false
  27. 避免缓存出现问题(该属性会在旧插件移除后一同移除)
  28. */
  29. /* @Bean
  30. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  31. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  32. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
  33. return interceptor;
  34. }
  35. @Bean
  36. public ConfigurationCustomizer configurationCustomizer() {
  37. return configuration -> configuration.setUseDeprecatedExecutor(false);
  38. }*/
  39. //***************************************************
  40. /**
  41. * 注册插件
  42. * 依赖以下版本+
  43. * <dependency>
  44. * <groupId>com.baomidou</groupId>
  45. * <artifactId>mybatis-plus-boot-starter</artifactId>
  46. * <version>3.4.1</version>
  47. * </dependency>
  48. * @return com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor 拦截器
  49. * @author 金聖聰
  50. * @email jinshengcong@163.com
  51. * Modification History:
  52. * Date Author Description version
  53. *--------------------------------------------------------*
  54. * 2021/04/16 15:56 金聖聰 修改原因 1.0
  55. */
  56. @Bean
  57. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  58. // 0.创建一个拦截器
  59. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  60. // 1. 添加分页插件
  61. PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();
  62. // 2. 设置请求的页面大于最大页后操作,true调回到首页,false继续请求。默认false
  63. pageInterceptor.setOverflow(false);
  64. // 3. 单页分页条数限制,默认无限制
  65. pageInterceptor.setMaxLimit(500L);
  66. // 4. 设置数据库类型
  67. pageInterceptor.setDbType(DbType.MYSQL);
  68. // 5.添加内部拦截器
  69. interceptor.addInnerInterceptor(pageInterceptor);
  70. return interceptor;
  71. }
  72. }
  1. @Test
  2. void selectPage() {
  3. //分页查询
  4. /*
  5. * 1、名字中包含雨并且年龄小于40
  6. * name like '%雨%' and age<40
  7. * 条件构造器
  8. */
  9. LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
  10. lambdaQueryWrapper.ge(User::getAge,18);
  11. // 泛型是实体类 , 当前页数 默认是1,从1开始,不是0。每页最多多少条
  12. // 第一种用selectPage方法,返回的是 Page<User> userPage
  13. /*Page<User> page = new Page<>(1, 2);
  14. Page<User> userPage = userMapper.selectPage(page, lambdaQueryWrapper);
  15. System.out.println("总页数: "+userPage.getPages());
  16. System.out.println("总记录数: "+userPage.getTotal());
  17. userPage.getRecords().forEach(System.out::println);*/
  18. // 第二种用 selectMapsPage方法,返回的是 IPage<Map<String, Object>>
  19. // 如果第一个参数还是用的上面的page,此时page胡报错
  20. // 解决方案:
  21. // 把page 转成对应的类型IPage<Map<String, Object>>
  22. // 因为新版后(3.4.1+), 更改了源码 给他设定了具体类型
  23. IPage<Map<String, Object>> page1 = new Page<>(4, 2);
  24. IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page1, lambdaQueryWrapper);
  25. System.out.println("总页数: "+mapIPage.getPages());
  26. System.out.println("总记录数: "+mapIPage.getTotal());
  27. List<Map<String, Object>> records = mapIPage.getRecords();
  28. records.forEach(System.out::println);
  29. }

实际开发中有的查询是多表联查的,这样就不能用上面的两个方法了,但是不甘心使用传统的写法,,那怎么办?

用xml自定义查询方法

  1. // 自定义分页
  2. IPage<User> selectUserPage(Page<User> page,@Param(Constants.WRAPPER) Wrapper<User> wrapper);
  1. <select id="selectUserPage" resultType="com.jsc.mybatisplus.entity.User">
  2. <!-- 参数的注解是固定的 ew 就是WRAPPER的值-->
  3. <!-- 没有什么改变,但是可以自己多表联查-->
  4. select *
  5. from user ${ew.customSqlSegment}
  6. </select>
  1. @Test
  2. void selectPageCustomize() {
  3. //分页查询
  4. /*
  5. * 1、名字中包含雨并且年龄小于40
  6. * name like '%雨%' and age<40
  7. * 条件构造器
  8. */
  9. LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
  10. lambdaQueryWrapper.ge(User::getAge,18);
  11. // 泛型是实体类 , 当前页数 默认是1,从1开始,不是0。每页最多多少条
  12. // 第一种用selectPage方法,返回的是 Page<User> userPage
  13. Page<User> page = new Page<>(1, 2);
  14. IPage<User> userIPage = userMapper.selectUserPage(page, lambdaQueryWrapper);
  15. System.out.println("总页数: "+userIPage.getPages());
  16. System.out.println("总记录数: "+userIPage.getTotal());
  17. userIPage.getRecords().forEach(System.out::println);
  18. }
  1. @Test
  2. void UpdateById() {
  3. User user = new User();
  4. user.setId(1382713714941648898L);
  5. user.setName("小陈");
  6. int i = userMapper.updateById(user);
  7. System.out.println("影响记录数: "+i);
  8. }
  1. private UpdateWrapper<User> update = Wrappers.update();
  2. @Test
  3. void UpdateByWrapper() {
  4. User user = new User();
  5. // user.setId(1382713714941648898L);
  6. user.setName("小金");
  7. update.eq("name","老金");
  8. int i = userMapper.update(user,update);
  9. System.out.println("影响记录数: "+i);
  10. }
  11. @Test
  12. void UpdateByWrapper1() {
  13. User whereUser = new User();
  14. // user.setId(1382713714941648898L);
  15. whereUser.setName("小金");
  16. // 可以直接把实体传进去
  17. update = new UpdateWrapper<>(whereUser);
  18. update.eq("age",18);
  19. User user = new User();
  20. user.setAge(21);
  21. int i = userMapper.update(user,update);
  22. System.out.println("影响记录数: "+i);
  23. }
  1. @Test
  2. void UpdateByWrapper2() {
  3. // 不创建实体传入,直接在条件中set
  4. update.eq("name","小金").set("name","老金");
  5. int i = userMapper.update(null,update);
  6. System.out.println("影响记录数: "+i);
  7. }
  1. @Test
  2. void UpdateByLambda() {
  3. LambdaUpdateWrapper<User> updateLambda = Wrappers.lambdaUpdate();
  4. updateLambda.eq(User::getName,"老金").set(User::getName,"小金");
  5. int i = userMapper.update(null,updateLambda);
  6. System.out.println("影响记录数: "+i);
  7. }
  8. @Test
  9. void UpdateByLambdaChain() {
  10. // 链式调用
  11. boolean update = new LambdaUpdateChainWrapper<User>(userMapper).eq(User::getName, "小金").set(User::getName, "老金").update();
  12. System.out.println(update?"成功":"失败");
  13. }
  1. @Test
  2. void deleteById() {
  3. int i = userMapper.deleteById(1382699085670719489L);
  4. System.out.println("影响行数: "+ i);
  5. }
  1. @Test
  2. void deleteByMap() {
  3. Map<String, Object> columnMap = new HashMap<>();
  4. columnMap.put("name","小金");
  5. columnMap.put("age","18");
  6. // WHERE name = ? AND age = ?
  7. int i = userMapper.deleteByMap(columnMap);
  8. System.out.println("影响行数: "+ i);
  9. }
  10. @Test
  11. void deleteByBatchIds() {
  12. List<Long> longs = Arrays.asList(1383035889074692097L, 1383035840634646530L);
  13. int i = userMapper.deleteBatchIds(longs);
  14. // WHERE id IN ( ? , ? )
  15. // 根据id批量删除
  16. System.out.println("影响行数: " + i);
  17. }
  1. @Test
  2. void deleteByWrapper() {
  3. LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
  4. lambdaQuery.eq(User::getId, "1383037094597267457");
  5. int delete = userMapper.delete(lambdaQuery);
  6. System.out.println("影响行数: " + delete);
  7. }

活动记录,是一个领域模型模式,

​ 一个模型类对应数据库中的一个表

​ 模型类的一个实例对应表中的一个记录

简单来说就是通过实体类对象对表进行增删改查操作,方便开发人员的开发

用lombok的@Data后,继承别的类后会有个警告,用注解@EqualsAndHashCode(callSuper=false)可以消除警告,再添加一个系列化id

““@EqualsAndHashCode(callSuper=false)“就是不调用父类,

但是,别的时候 大部分需要父类的一些属性作为等值比较的

  1. @Data
  2. @EqualsAndHashCode(callSuper = false)
  3. public class User extends Model<User> {
  4. private static final long serialVersionUID = 7589930312778081895L;
  5. /** 主键 */
  6. private Long id;
  7. /** 姓名 */
  8. private String name;
  9. /** 年龄 */
  10. private Integer age;
  11. /** 邮件 */
  12. private String email;
  13. /** 直属上级id */
  14. private Long managerId;
  15. /** 创建时间 */
  16. private LocalDateTime createTime;
  17. }
  1. @Test
  2. void ARInsertTest() {
  3. User user = new User();
  4. user.setName("xx");
  5. user.setAge(0);
  6. user.setManagerId(1088248166370832385L);
  7. user.setCreateTime(LocalDateTime.now());
  8. // 直接insert,自己插自己可还行
  9. boolean insert = user.insert();
  10. System.out.println(insert?"成功":"失败");
  11. }
  12. @Test
  13. void ARSelectByIdTest1() {
  14. // 不需要参数,直接实体上设置
  15. // 直接查,自己查自己可还行
  16. User user = new User();
  17. user.setId(1383044106274050049L);
  18. User user1 = user.selectById();
  19. System.out.println(user1);
  20. }
  21. // 查出来的都是新的对象,并没有把值设置到原来的对象上
  22. @Test
  23. void ARUpdateTest() {
  24. User user = new User();
  25. user.setId(1383044106274050049L);
  26. user.setName("xxoo");
  27. user.setAge(16);
  28. user.setManagerId(1383035808669888513L);
  29. user.setCreateTime(LocalDateTime.now());
  30. // 自己操作
  31. boolean b = user.updateById();
  32. System.out.println(b?"success":"fail");
  33. }
  34. @Test
  35. void ARDeleteTest() {
  36. User user = new User();
  37. user.setId(1383044106274050049L);
  38. // 自己操作
  39. boolean b = user.deleteById();
  40. System.out.println(b?"success":"fail");
  41. }

通过在实体类上id的注解@TableId(type=IdType.XXX)设置

AUTO 数据库ID自增
NONE 无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
INPUT insert前自行set主键值
ASSIGN_ID 分配ID(主键类型为Number(Long和Integer)或String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为
DefaultIdentifierGenerator雪花算法)
ASSIGN_UUID 分配UUID,主键类型为String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认default方法)
ID_WORKER 分布式全局唯一ID 长整型类型 (please use ASSIGN_ID)
UUID 32位UUID字符串 (please use ASSIGN_UUID)
ID_WORKER_STR 分布式全局唯一ID 字符串类型 (please use ASSIGN_ID)

在配置文件中设置

  1. mybatis-plus.global-config.db-config.id-type=uuid

如果局部策略和全局策略都设置了 ,就近原则

基本的在官网,这里不做过多记录

https://mybatis.plus/config/#基本配置

https://mybatis.plus/config/#configuration

mapUnderscoreToCamelCase这个配置不能和configLocation一起出现,会报错

字段验证策略可以看一下~

不过一般都是默认的NOT_NULL

not_empty (字段为””)也是忽略

ignored 就是空的也插入 但是有风险 在更新的时候

但是在实体类中,

个别的,可以在字段上配置@TableField(stragegy=FieldStrategy.NOT_EMPTY),就是字段为””的时候也忽略

同样的就近原则

mybatis-plus.global-config.db-config.table-prefix=

  1. 新建service包

  2. 新建接口UserService

  3. 继承IService<T> 泛型写所对应的实体类

  4. 创建service的实现包impl

  5. 创建UserServiceImp

  6. 继承ServiceImpl<UserMapper, User> 第一个蚕食数索要操作的mapper接口,第二个参数是对应的实体类

  7. 实现刚才的接口UserService,并给上@Service的注解

    UserService.java

    1. public interface UserService extends IService<User> {
    2. }

    ServiceImpl.java

    1. @Service
    2. public class UserServiceImp extends ServiceImpl<UserMapper, User> implements UserService{
    3. }
  1. @Autowired
  2. private UserService userService;
  3. @Test
  4. public void testService() {
  5. User one = userService.getOne(Wrappers.<User>lambdaQuery().gt(User::getAge,25),false);
  6. System.out.println(one);
  7. }
  1. @Test
  2. public void batchTest() {
  3. // 批量
  4. User user = new User();
  5. user.setName("犀利");
  6. user.setAge(12);
  7. User user1 = new User();
  8. user1.setName("犀利1");
  9. user1.setAge(121);
  10. List<User> users = Arrays.asList(user1, user);
  11. userService.saveBatch(users);
  12. }
  1. @Test
  2. public void chainTest() {
  3. // 查大于25的
  4. List<User> list = userService.lambdaQuery().gt(User::getAge, 25).list();
  5. list.forEach(System.out::println);
  6. }
  7. @Test
  8. public void chainTest1() {
  9. // 更新
  10. boolean update = userService.lambdaUpdate().eq(User::getName, "小陈").set(User::getName, "小玉").update();
  11. System.out.println(update?"success":"fail");
  12. }
  13. @Test
  14. public void chainTest2() {
  15. // 直接删除
  16. boolean update = userService.lambdaUpdate().eq(User::getName, "xx").remove();
  17. System.out.println(update?"success":"fail");
  18. }

版权声明:本文为jsccc520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jsccc520/p/14669347.html