下面是基本的环境 
1.数据库有一张表 (我先当mybatis设计表)
2.控制层,持久层,服务层 ,配置类准备好
如下
配置类: 插件可以不配置,看需求

/**
 * @author Royalreairman
 * @create 2022-08-06 20:30
 */
@Configuration
@MapperScan("com.example.mybatis.plus.mapper")
public class MpConfig {




    //乐观锁插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    //逻辑删除
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }



    /**
     * sql执行性能分析插件
     * 三种环境
     *      *dev:开发环境
     *      *test:测试环境
     *      *prod:生产环境
     * @return
     */
    @Bean
    @Profile({"dev", "test"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(500);//ms,超过此处设置的ms则sql不执行
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;

    }
}

控制层不需要,所以没有 ,业务逻辑层也没有,我们不需要 

mapper层

@Repository
public interface UserMapper extends BaseMapper<User> {

}

 实体类

package com.example.mybatis.plus.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;

import java.util.Date;

@Data
public class User {

    @TableId(type = IdType.ID_WORKER)//mybatis plus自带的策略
    private Long id;

    private String name;

    private Integer age;

    private String email;

}

表设计

percona monitoring and management使用攻略 perm application_User

 是不是感觉现在可以开始学习了,弄一个测试看一下

percona monitoring and management使用攻略 perm application_User_02

点运行会发生什么呢?直接报错

 

percona monitoring and management使用攻略 perm application_spring_03

简单说就是我不认识你这些东西,这么解决了加mybatis-plus必备的字段

percona monitoring and management使用攻略 perm application_mybatis_04

 下面是建表语句,实体类也要改我这里就不展示了

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '名字',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '100' COMMENT '邮箱',
  `create_time` timestamp NULL DEFAULT NULL COMMENT '创建时间',
  `create_by` bigint(20) DEFAULT NULL COMMENT '创建人',
  `modify_time` timestamp NULL DEFAULT NULL COMMENT '更新时间',
  `modify_by` bigint(20) DEFAULT NULL COMMENT '更新人',
  `ip_str` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '修改ip',
  `is_del` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除(0:未删除 1:已删除)',
  `check_time` timestamp NULL DEFAULT NULL COMMENT '审核时间精确到秒',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=123 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

 我们再测试一下

percona monitoring and management使用攻略 perm application_mybatis_05

 

percona monitoring and management使用攻略 perm application_Test_06

 现在应该是空的,现在开始正是的学习

1.新增 ,添加类

 1-1 insert

@Test
    public void add(){
        User user = new User();
        user.setName("张三");
        user.setAge(13);
        user.setEmail("1234@.com");
        userMapper.insert(user);
    }

对应的SQL

INSERT 
    INTO
        user
        ( name, age, email ) 
    VALUES
        ( '张三', 13, '1234@.com' )

2.查询

通过id查询

@Test
    public void addList(){
        userMapper.selectById(123);
    }

 对应的SQL

SELECT
        id,
        name,
        age,
        email,
        create_time,
        create_by,
        modify_time,
        modify_by,
        ip_str,
        is_del,
        check_time 
    FROM
        user 
    WHERE
        id=123

通过多个id查询

@Test
    public void addList(){
        userMapper.selectBatchIds(Arrays.asList(123,124));
    }

 对应的SQL
 

SELECT
        id,
        name,
        age,
        email,
        create_time,
        create_by,
        modify_time,
        modify_by,
        ip_str,
        is_del,
        check_time 
    FROM
        user 
    WHERE
        id IN (
            123 , 124 
        )

 3.利用条件构造器查询年龄等于13的
 

@Test
    public void selectList(){
        List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>()
                .eq(User::getAge, 13));


    }

对应的SQL

SELECT
        id,
        name,
        age,
        email,
        create_time,
        create_by,
        modify_time,
        modify_by,
        ip_str,
        is_del,
        check_time 
    FROM
        user 
    WHERE
        age = 13

4.map查询((有了条件构造器很少用,我一般不用)

@Test
    public void  test() {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("age","13");
        userMapper.selectByMap(map);

    }

注意这种key是字段名 后面是你查的数据 ,对应的SQL

SELECT
        id,
        name,
        age,
        email,
        create_time,
        create_by,
        modify_time,
        modify_by,
        ip_str,
        is_del,
        check_time 
    FROM
        user 
    WHERE
        age = '13'

5.查询数量求和

@Test
    public void  test() {
      userMapper.selectCount(new LambdaQueryWrapper<User>()
      .eq(User::getAge,13));
    }

对应的SQL

SELECT
        COUNT(1) 
    FROM
        user 
    WHERE
        age = 13

6.查询单条语句,只能查出一条 

@Test
    public void  test() {
      userMapper.selectOne(new LambdaQueryWrapper<User>()
              .eq(User::getId,123));
    }

对应的SQL

SELECT
        id,
        name,
        age,
        email,
        create_time,
        create_by,
        modify_time,
        modify_by,
        ip_str,
        is_del,
        check_time 
    FROM
        user 
    WHERE
        id = 123

7.分页查询(真实开发中,直接用他带的很少,都是自己写sql,比如:姓名-电话 

@Test
    public void  test() {
        //0是当前页,10是每页显示的页数 ,
        Page<User> page = new Page<>(0, 10);
        userMapper.selectPage(page,new LambdaQueryWrapper<User>()
                .eq(User::getAge,13));
    }

对应的SQL

SELECT
        id,
        name,
        age,
        email,
        create_time,
        create_by,
        modify_time,
        modify_by,
        ip_str,
        is_del,
        check_time 
    FROM
        user 
    WHERE
        age = 13 LIMIT 0,10

3.删除

在测试删除的时候建议大家,多弄几条数据
1.条件删除

@Test
    public void  test() {
        int delete = userMapper.delete(new LambdaQueryWrapper<User>()
        .eq(User::getId,123));
    }

对应的SQL

DELETE 
    FROM
        user 
    WHERE
        id = 123

2.通过id删除
 

@Test
    public void  test() {
        //124L
        userMapper.deleteById(124l);
    }

对应的SQL

DELETE 
    FROM
        user 
    WHERE
        id=124

3.通过多个id删除

@Test
    public void  test() {
        int i = userMapper.deleteBatchIds(Arrays.asList(125, 126));
    }

对应的sql

DELETE 
    FROM
        user 
    WHERE
        id IN (
            125 , 126 
        )

4.map删除(有了构造器很少用)

@Test
    public void  test() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",128);
        int i = userMapper.deleteByMap(map);
    }

对应的SQL

DELETE 
    FROM
        user 
    WHERE
        id = 128

4.修改

1.通过id

修改

通过id修改 至少需要两个字段 1是id ,二是需要修改的字段

@Test
    public void  test() {
        User user = new User();
        user.setId(127L);
        user.setName("李四");
        userMapper.updateById(user);
    }

对应的SQL

UPDATE
        user 
    SET
        name='李四' 
    WHERE
        id=127

2.条件修改 

@Test
    public void  test() {
        User user = new User();
        user.setName("李四");
        userMapper.update(user,new LambdaQueryWrapper<User>()
                .eq(User::getAge,13));
    }

对应的SQL

UPDATE
        user 
    SET
        name='李四' 
    WHERE
        age = 13

最基本的功能我这里就说完了,剩下的后面的文章会说明 了