什么是MyBatisPlus(MP)?

为简化开发,提升效率而生,对mybatis功能进行增强,但未做改变。
支持任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库。

案例操作

新建springboot工程

java mybatisplus bool 类型参数 mybatis-plus+springboot_数据库

若选择https://start.spring.io下一步失败

java mybatisplus bool 类型参数 mybatis-plus+springboot_spring boot_02

则选择Custom,输入:https://start.aliyun.com后下一步

java mybatisplus bool 类型参数 mybatis-plus+springboot_spring boot_03

java mybatisplus bool 类型参数 mybatis-plus+springboot_mysql_04

添加需要的依赖

java mybatisplus bool 类型参数 mybatis-plus+springboot_java_05

安装插件

file --> settings --> Plugins --> Marketplace中搜索MyBatisX --> Installed

java mybatisplus bool 类型参数 mybatis-plus+springboot_mysql_06

添加其他依赖,全部依赖如下:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--Mybatis-plus的依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
        <!--mysql的依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

修改配置文件:

application.properties:改为application.yml

application.yml:

#端口号8080
server:
  port: 8080

#数据库名:mysql,用户名root,密码123456
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    
# mybatis-plus配置
mybatis-plus:
  # xml文件位置
  mapper-locations: classpath:mapper/*.xml

/src/main/java/com.example.demo下设置:service,controller,mapper,entity包
/src/main/resouces下设置mapping文件夹

controller创建StudentController类;
service下创建StudentService接口,并创建impl包,创建StudentServiceImpl类;
mapper下创建StudentMapper接口;
entity下创建StudentInfo类;
mapping文件夹下创建StudentMapper.xml文件;(可不建此文件,mybatis-plus在写基础功能时不需要xml文件手写sql,复杂sql不在本次讨论范围内)

(本次未使用分页等功能,不需配置config)

结构如下图所示:

java mybatisplus bool 类型参数 mybatis-plus+springboot_spring_07

新建数据库测试表:student

CREATE TABLE student(
	id VARCHAR(2) COMMENT '学生ID',
	sname VARCHAR(20) COMMENT '学生姓名',
	classId VARCHAR(3) COMMENT '班级ID',
	birthday VARCHAR(5) COMMENT '学生生日',
	email VARCHAR(20) COMMENT '学生电子邮箱'
);

INSERT INTO student(id,sname,class_id,birthday,email)
VALUES(1,'张三',101,1016,'1@163.com'),(2,'李四',101,511,'2@163.com'),
	  (3,'王五',101,1016,'3@163.com'),(4,'赵六',103,615,'4@163.com');

java mybatisplus bool 类型参数 mybatis-plus+springboot_数据库_08

StudentInfo类:
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("student")
public class StudentInfo {
    //若id为主键,则为@TableId("id")
    @TableField("id")
    private String id;

    @TableField("sname")
    private String sname;

    @TableField("classId")
    private String classId;

    @TableField("birthday")
    private String birthday;

    @TableField("email")
    private String email;
}
StudentMapper(继承BaseMapper)
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.StudentInfo;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentMapper extends BaseMapper<StudentInfo> {
}
StudentService(继承IService)
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.StudentInfo;

public interface StudentService extends IService<StudentInfo> {
}
StudentServiceImpl(继承ServiceImplement)
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.StudentInfo;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentInfo> implements StudentService {
}
StudentController:
import com.example.demo.entity.StudentInfo;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/demo11")
public class StudentController {

    @Autowired(required = false)
    private StudentService studentService;

    /**
     * 查询学生信息
     * @param id
     * @return
     */
    @RequestMapping("getInfo/{id}")
    public StudentInfo getStudentInfo(@PathVariable String id){
        return studentService.getById(id);
    }

    /**
     * 插入学生信息
     * @param studentInfo
     */
    @RequestMapping("/insert")
    public void insertInfo(StudentInfo studentInfo){

        StudentInfo info=new StudentInfo();
        info.setId(studentInfo.getId());
        info.setSname(studentInfo.getSname());
        info.setClassId(studentInfo.getClassId());
        info.setBirthday(studentInfo.getBirthday());
        info.setEmail(studentInfo.getEmail());
        studentService.save(info);
    }

    /**
     * 查询全部学生信息
     * @return
     */
    @RequestMapping("/selectAll")
    public List<StudentInfo> selectAll(){
        return studentService.list();
    }

    /**
     * 根据id更新学生表信息
     * @param studentInfo
     */
    @RequestMapping("/update")
    public void updateById(StudentInfo studentInfo){

        StudentInfo info=new StudentInfo();
        info.setId(studentInfo.getId());
        info.setSname(studentInfo.getSname());
        info.setClassId(studentInfo.getClassId());
        info.setBirthday(studentInfo.getBirthday());
        info.setEmail(studentInfo.getEmail());
        studentService.updateById(info);
    }

    /**
     * 根据id删除学生信息
     * @param id
     */
    @RequestMapping("/delete")
    public void deleteById(String id){
        studentService.removeById(id);
    }

}

启动程序

java mybatisplus bool 类型参数 mybatis-plus+springboot_数据库_09

使用postman测试

测试:查询id为2的学生信息

URL:

localhost:8080/demo11/getInfo/2

java mybatisplus bool 类型参数 mybatis-plus+springboot_spring_10

测试:插入一条学生信息

URL:

localhost:8080/demo11/insert?id=5&sname=小红&classId=103&birthday=1231&email=5@163.com

java mybatisplus bool 类型参数 mybatis-plus+springboot_mysql_11

查看数据库student表

java mybatisplus bool 类型参数 mybatis-plus+springboot_spring boot_12

测试:删除id为5的学生信息

URL:

localhost:8080/demo11/delete?id=5

java mybatisplus bool 类型参数 mybatis-plus+springboot_spring boot_13

查看数据库student表

java mybatisplus bool 类型参数 mybatis-plus+springboot_java_14

测试:修改id为4的学生信息

URL:

localhost:8080/demo11/update?id=4&sname=小明&classId=103&birthday=1231&email=4@163.com

java mybatisplus bool 类型参数 mybatis-plus+springboot_spring_15

查看数据库student表

java mybatisplus bool 类型参数 mybatis-plus+springboot_mysql_16

测试:查询学生表中全部信息

URL:

localhost:8080/demo11/selectAll

java mybatisplus bool 类型参数 mybatis-plus+springboot_mysql_17

测试完成