• 尚硅谷MyBatis-Plus笔记001【简介、入门案例、基本CRUD】


    视频地址:【尚硅谷】MyBatisPlus教程(一套玩转mybatis-plus)_哔哩哔哩_bilibili

    1. 尚硅谷MyBatis-Plus笔记01【简介、入门案例、基本CRUD】
    2. 尚硅谷MyBatis-Plus笔记02【】

    3. 尚硅谷MyBatis-Plus笔记03【】

    4. 尚硅谷MyBatis-Plus笔记04【】

    5. 尚硅谷MyBatis-Plus笔记05【】

    目录

    一、MyBatis-Plus简介

    P01【01-MyBatis-Plus简介】03:18

    P02【02-MyBatis-Plus特性】03:39

    P03【03-MyBatis-Plus支持的数据库以及框架结构】03:00

    二、入门案例

    P04【04-入门案例之开发环境】01:51

    P05【05-创建测试数据库和表】01:21

    P06【06-创建Spring Boot工程】05:55

    P07【07-配置application.yml】08:16

    P08【08-创建实体类以及lombok的简单使用】05:53

    P09【09-创建mapper接口并扫描】03:25

    P10【10-测试】07:22

    P11【11-加入日志功能】03:22

    三、基本CRUD

    P12【12-BaseMapper】04:37

    P13【13-测试BaseMapper的新增功能】04:25

    P14【14-测试BaseMapper的删除功能】08:22

    P15【15-测试BaseMapper的修改功能】03:08

    P16【16-测试BaseMapper的查询功能】07:36

    P17【17-测试自定义功能】05:35

    P18【18-通用Service接口】07:23

    P19【19-测试通用Service之查询总记录数】03:13

    P20【20-测试通用Service之批量添加功能】06:28

    四、常用注解

    P21【21-MyBatis-Plus的常用注解@TableName】06:14


    一、MyBatis-Plus简介

    MyBatis-Plus是MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。本视频从MyBatis-Plus的特性到优秀插件,以及多数据源的配置都有详细讲解。

    P01【01-MyBatis-Plus简介】03:18

    尚硅谷 MyBatis-Plus,讲师:杨博超

    课程主要内容

    P02【02-MyBatis-Plus特性】03:39

    MyBatis-plus官网:MyBatis-Plus

    P03【03-MyBatis-Plus支持的数据库以及框架结构】03:00

    二、入门案例

    P04【04-入门案例之开发环境】01:51

    版本:

    1. IDE:idea 2019.2
    2. JDK:JDK8+
    3. 构建工具:maven 3.5.4
    4. MySQL版本:MySQL 5.7
    5. Spring Boot:2.6.3
    6. MyBatis-Plus:3.5.1

    P05【05-创建测试数据库和表】01:21

    bigint而不是int,MyBatisPlus进行数据插入的时候默认使用雪花算法来生成id,长度比较长,所以使用bigint。

    1. CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
    2. USE `mybatis_plus`;
    3. CREATE TABLE `user` (
    4. `id` BIGINT(20) NOT NULL COMMENT '主键ID',
    5. `name` VARCHAR(30) DEFAULT NULL COMMENT '姓名',
    6. `age` INT(11) DEFAULT NULL COMMENT '年龄',
    7. `email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
    8. PRIMARY KEY (`id`)
    9. ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    10. INSERT INTO USER (id, NAME, age, email) VALUES
    11. (1, 'Jone', 18, 'test1@baomidou.com'),
    12. (2, 'Jack', 20, 'test2@baomidou.com'),
    13. (3, 'Tom', 28, 'test3@baomidou.com'),
    14. (4, 'Sandy', 21, 'test4@baomidou.com'),
    15. (5, 'Billie', 24, 'test5@baomidou.com');

    P06【06-创建Spring Boot工程】05:55

     

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.6.3version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.atguigugroupId>
    12. <artifactId>mybatisplusartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>mybatisplusname>
    15. <description>mybatisplusdescription>
    16. <properties>
    17. <java.version>1.8java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starterartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>org.springframework.bootgroupId>
    26. <artifactId>spring-boot-starter-testartifactId>
    27. <scope>testscope>
    28. dependency>
    29. <dependency>
    30. <groupId>com.baomidougroupId>
    31. <artifactId>mybatis-plus-boot-starterartifactId>
    32. <version>3.5.1version>
    33. dependency>
    34. <dependency>
    35. <groupId>org.projectlombokgroupId>
    36. <artifactId>lombokartifactId>
    37. <optional>trueoptional>
    38. dependency>
    39. <dependency>
    40. <groupId>mysqlgroupId>
    41. <artifactId>mysql-connector-javaartifactId>
    42. <scope>runtimescope>
    43. dependency>
    44. dependencies>
    45. <build>
    46. <plugins>
    47. <plugin>
    48. <groupId>org.springframework.bootgroupId>
    49. <artifactId>spring-boot-maven-pluginartifactId>
    50. plugin>
    51. plugins>
    52. build>
    53. project>

    P07【07-配置application.yml】08:16

    1. application.properties
    2. #spring.datasource.type=com.zaxxer.hikari.HikariDataSource
    3. #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    1. application.yml
    2. spring:
    3. # 配置数据源信息
    4. datasource:
    5. # 配置数据源类型
    6. type: com.zaxxer.hikari.HikariDataSource
    7. # 配置连接数据库信息
    8. driver-class-name: com.mysql.cj.jdbc.Driver
    9. url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf8&useSSL=false
    10. username: root
    11. password: 123456

    P08【08-创建实体类以及lombok的简单使用】05:53

    Data注解:相当于无参构造、getter、setter、equals、hashCode、toString方法。

    P09【09-创建mapper接口并扫描】03:25

    1. BaseMapper是MyBatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,泛型为操作的实体类型。
    2. @MapperScan("com.atguigu.mybatisplus.mapper") //用于扫描mapper接口所在的包、扫描指定包下面的mapper接口

    P10【10-测试】07:22

    IDEA在 userMapper 处报错,因为找不到注入的对象,因为类是动态创建的,但是程序可以正确地执行。

    为了避免报错,可以在mapper接口上添加 @Repository 注解。

    1. package com.atguigu.mybatisplus;
    2. import com.atguigu.mybatisplus.mapper.UserMapper;
    3. import com.atguigu.mybatisplus.pojo.User;
    4. import org.junit.jupiter.api.Test;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.boot.test.context.SpringBootTest;
    7. import java.util.List;
    8. @SpringBootTest
    9. public class MybatisPlusTest {
    10. @Autowired
    11. private UserMapper userMapper;//正常报错,不影响运行
    12. //userMapper动态生成的代理类交给了ioc容器管理
    13. @Test
    14. public void testSelectList() {
    15. //通过条件构造器查询一个list集合,若没有条件,则可以设置null为参数.
    16. List list = userMapper.selectList(null);
    17. list.forEach(System.out::println);
    18. }
    19. }

    P11【11-加入日志功能】03:22

    三、基本CRUD

    P12【12-BaseMapper】04:37

    BaseMapper.java,MyBatis-Plus中的基本CRUD在内置的BaseMapper中都已得到了实现,我们可以直接使用,接口如 下:

    1. /*
    2. * Copyright (c) 2011-2022, baomidou (jobob@qq.com).
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. *
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS,
    12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. * See the License for the specific language governing permissions and
    14. * limitations under the License.
    15. */
    16. package com.baomidou.mybatisplus.core.mapper;
    17. import com.baomidou.mybatisplus.core.conditions.Wrapper;
    18. import com.baomidou.mybatisplus.core.metadata.IPage;
    19. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
    20. import com.baomidou.mybatisplus.core.toolkit.Constants;
    21. import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
    22. import org.apache.ibatis.annotations.Param;
    23. import java.io.Serializable;
    24. import java.util.Collection;
    25. import java.util.List;
    26. import java.util.Map;
    27. /*
    28. _ _ /_ _ _/_. ____ / _
    29. / / //_//_//_|/ /_\ /_///_/_\ Talk is cheap. Show me the code.
    30. _/ /
    31. */
    32. /**
    33. * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
    34. *

      这个 Mapper 支持 id 泛型

    35. *
    36. * @author hubin
    37. * @since 2016-01-23
    38. */
    39. public interface BaseMapper extends Mapper {
    40. /**
    41. * 插入一条记录
    42. *
    43. * @param entity 实体对象
    44. */
    45. int insert(T entity);
    46. /**
    47. * 根据 ID 删除
    48. *
    49. * @param id 主键ID
    50. */
    51. int deleteById(Serializable id);
    52. /**
    53. * 根据实体(ID)删除
    54. *
    55. * @param entity 实体对象
    56. * @since 3.4.4
    57. */
    58. int deleteById(T entity);
    59. /**
    60. * 根据 columnMap 条件,删除记录
    61. *
    62. * @param columnMap 表字段 map 对象
    63. */
    64. int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
    65. /**
    66. * 根据 entity 条件,删除记录
    67. *
    68. * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
    69. */
    70. int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper);
    71. /**
    72. * 删除(根据ID或实体 批量删除)
    73. *
    74. * @param idList 主键ID列表或实体列表(不能为 null 以及 empty)
    75. */
    76. int deleteBatchIds(@Param(Constants.COLLECTION) Collection idList);
    77. /**
    78. * 根据 ID 修改
    79. *
    80. * @param entity 实体对象
    81. */
    82. int updateById(@Param(Constants.ENTITY) T entity);
    83. /**
    84. * 根据 whereEntity 条件,更新记录
    85. *
    86. * @param entity 实体对象 (set 条件值,可以为 null)
    87. * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
    88. */
    89. int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper);
    90. /**
    91. * 根据 ID 查询
    92. *
    93. * @param id 主键ID
    94. */
    95. T selectById(Serializable id);
    96. /**
    97. * 查询(根据ID 批量查询)
    98. *
    99. * @param idList 主键ID列表(不能为 null 以及 empty)
    100. */
    101. List selectBatchIds(@Param(Constants.COLLECTION) Collection idList);
    102. /**
    103. * 查询(根据 columnMap 条件)
    104. *
    105. * @param columnMap 表字段 map 对象
    106. */
    107. List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
    108. /**
    109. * 根据 entity 条件,查询一条记录
    110. *

      查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常

    111. *
    112. * @param queryWrapper 实体对象封装操作类(可以为 null)
    113. */
    114. default T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper) {
    115. List ts = this.selectList(queryWrapper);
    116. if (CollectionUtils.isNotEmpty(ts)) {
    117. if (ts.size() != 1) {
    118. throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records");
    119. }
    120. return ts.get(0);
    121. }
    122. return null;
    123. }
    124. /**
    125. * 根据 Wrapper 条件,判断是否存在记录
    126. *
    127. * @param queryWrapper 实体对象封装操作类
    128. * @return
    129. */
    130. default boolean exists(Wrapper queryWrapper) {
    131. Long count = this.selectCount(queryWrapper);
    132. return null != count && count > 0;
    133. }
    134. /**
    135. * 根据 Wrapper 条件,查询总记录数
    136. *
    137. * @param queryWrapper 实体对象封装操作类(可以为 null)
    138. */
    139. Long selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);
    140. /**
    141. * 根据 entity 条件,查询全部记录
    142. *
    143. * @param queryWrapper 实体对象封装操作类(可以为 null)
    144. */
    145. List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);
    146. /**
    147. * 根据 Wrapper 条件,查询全部记录
    148. *
    149. * @param queryWrapper 实体对象封装操作类(可以为 null)
    150. */
    151. List> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper);
    152. /**
    153. * 根据 Wrapper 条件,查询全部记录
    154. *

      注意: 只返回第一个字段的值

    155. *
    156. * @param queryWrapper 实体对象封装操作类(可以为 null)
    157. */
    158. List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper);
    159. /**
    160. * 根据 entity 条件,查询全部记录(并翻页)
    161. *
    162. * @param page 分页查询条件(可以为 RowBounds.DEFAULT)
    163. * @param queryWrapper 实体对象封装操作类(可以为 null)
    164. */
    165. extends IPage> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

    166. /**
    167. * 根据 Wrapper 条件,查询全部记录(并翻页)
    168. *
    169. * @param page 分页查询条件
    170. * @param queryWrapper 实体对象封装操作类
    171. */
    172. extends IPage>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

    173. }
    174. P13【13-测试BaseMapper的新增功能】04:25

      1. @Test
      2. public void testInsert() {
      3. //实现新增用户信息
      4. //INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
      5. User user = new User();
      6. //user.setId(100L);
      7. user.setName("张三");
      8. user.setAge(23);
      9. user.setEmail("zhangsan@atguigu.com");
      10. int result = userMapper.insert(user);
      11. System.out.println("result:" + result);
      12. System.out.println("id:" + user.getId());
      13. }

      P14【14-测试BaseMapper的删除功能】08:22

      1. @Test
      2. public void testDelete() {
      3. //1、通过id删除用户信息
      4. //DELETE FROM user WHERE id=?
      5. // int result = userMapper.deleteById(1659014646172069890L);
      6. // System.out.println("result:" + result);
      7. //2、根据map集合中所设置的条件删除用户信息
      8. //DELETE FROM user WHERE name = ? AND age = ?
      9. // Map map = new HashMap<>();
      10. // map.put("name", "张三");
      11. // map.put("age", 23);
      12. // int result = userMapper.deleteByMap(map);
      13. // System.out.println("result:" + result);
      14. //3、通过多个id实现批量删除
      15. //DELETE FROM user WHERE id IN ( ? , ? , ? )
      16. List list = Arrays.asList(1L, 2L, 3L);
      17. int result = userMapper.deleteBatchIds(list);
      18. System.out.println("result:" + result);
      19. }

      P15【15-测试BaseMapper的修改功能】03:08

      1. @Test
      2. public void testUpdate() {
      3. //修改用户信息
      4. //UPDATE user SET name=?, email=? WHERE id=?
      5. User user = new User();
      6. user.setId(4L);
      7. user.setName("李四");
      8. user.setEmail("lisi@atguigu.com");
      9. int result = userMapper.updateById(user);
      10. System.out.println("result:" + result);
      11. }

      P16【16-测试BaseMapper的查询功能】07:36

      1. @Test
      2. public void testSelect() {
      3. //通过id查询用户信息
      4. //SELECT id,name,age,email FROM user WHERE id=?
      5. User user = userMapper.selectById(1L);
      6. System.out.println(user);
      7. //根据多个id查询多个用户信息
      8. //SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
      9. List list = Arrays.asList(1L, 2L, 3L);
      10. List users = userMapper.selectBatchIds(list);
      11. users.forEach(System.out::println);
      12. //根据map集合中的条件查询用户信息
      13. //SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
      14. Map map = new HashMap<>();
      15. map.put("name", "Jack");
      16. map.put("age", 20);
      17. List users = userMapper.selectByMap(map);
      18. users.forEach(System.out::println);
      19. //查询所有数据
      20. //SELECT id,name,age,email FROM user
      21. List users = userMapper.selectList(null);
      22. users.forEach(System.out::println);
      23. Map map = userMapper.selectMapById(1L);
      24. System.out.println(map);
      25. }

      P17【17-测试自定义功能】05:35

      1. "1.0" encoding="UTF-8" ?>
      2. mapper
      3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      5. <mapper namespace="com.atguigu.mybatisplus.mapper.UserMapper">
      6. <select id="selectMapById" resultType="map">
      7. select id, name, age, email
      8. from user
      9. where id = #{id}
      10. select>
      11. mapper>

      P18【18-通用Service接口】07:23

      Ctrl+N:Ctrl+N按名字搜索类,搜索查看类。

      1. package com.atguigu.mybatisplus.service;
      2. import com.atguigu.mybatisplus.pojo.User;
      3. import com.baomidou.mybatisplus.extension.service.IService;
      4. public interface UserService extends IService {
      5. }
      1. package com.atguigu.mybatisplus.service.impl;
      2. import com.atguigu.mybatisplus.mapper.UserMapper;
      3. import com.atguigu.mybatisplus.pojo.User;
      4. import com.atguigu.mybatisplus.service.UserService;
      5. import com.baomidou.mybatisplus.extension.service.IService;
      6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
      7. public class UserServiceImpl extends ServiceImpl implements UserService {
      8. }

      P19【19-测试通用Service之查询总记录数】03:13

      1. package com.atguigu.mybatisplus;
      2. import com.atguigu.mybatisplus.service.UserService;
      3. import org.junit.jupiter.api.Test;
      4. import org.springframework.beans.factory.annotation.Autowired;
      5. import org.springframework.boot.test.context.SpringBootTest;
      6. @SpringBootTest
      7. public class MyBatisPlusServiceTest {
      8. @Autowired
      9. private UserService userService;
      10. @Test
      11. public void testGetCount() {
      12. //查询总记录数
      13. //SELECT COUNT( * ) FROM user
      14. long count = userService.count();
      15. System.out.println("总记录数:" + count);
      16. }
      17. }

      P20【20-测试通用Service之批量添加功能】06:28

      IDEA提供了CTRL+ALT+V对该行快速根据变量类型自动生成变量。

      1. package com.atguigu.mybatisplus;
      2. import com.atguigu.mybatisplus.pojo.User;
      3. import com.atguigu.mybatisplus.service.UserService;
      4. import org.junit.jupiter.api.Test;
      5. import org.springframework.beans.factory.annotation.Autowired;
      6. import org.springframework.boot.test.context.SpringBootTest;
      7. import java.util.ArrayList;
      8. import java.util.List;
      9. @SpringBootTest
      10. public class MyBatisPlusServiceTest {
      11. @Autowired
      12. private UserService userService;
      13. @Test
      14. public void testGetCount() {
      15. //查询总记录数
      16. //SELECT COUNT( * ) FROM user
      17. long count = userService.count();
      18. System.out.println("总记录数:" + count);
      19. }
      20. @Test
      21. public void testInsertMore() {
      22. //批量添加
      23. //INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? )
      24. List list = new ArrayList<>();
      25. for (int i = 1; i < 10; i++) {
      26. User user = new User();
      27. user.setId(Integer.toUnsignedLong(i * 10));
      28. user.setName("abc" + i);
      29. user.setAge(20 + i);
      30. list.add(user);
      31. }
      32. boolean b = userService.saveBatch(list);
      33. System.out.println(b);
      34. }
      35. }

      四、常用注解

      P21【21-MyBatis-Plus的常用注解@TableName】06:14

      有待学习...

    175. 相关阅读:
      配置编译设置
      Resnet的在指静脉识别应用与改进
      ubuntu安全扩展交换内存swap memory
      python 模拟后台点击
      CMake详解--从创建到编译
      uniapp生成的h5与flutter的原生进行交互
      中国电信研究院发布《5G+数字孪生赋能城市数字化应用研究报告》
      【iOS】——分类、扩展和关联对象
      postman json复杂数据的模拟
      除硅树脂-HP4500
    176. 原文地址:https://blog.csdn.net/weixin_44949135/article/details/130727101