Mybatis-Plus 官网:https://mp.baomidou.com/
注解使用:https://mp.baomidou.com/guide/annotation.htm
Github:https://github.com/baomidou/mybatis-plus
1、什么是MyBatis-Plus
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
mybatis plus 官网
建议安装 MybatisX 插件
只需要我们的Mapper继承 BaseMapper 就可以拥有crud能力
2、Quick start
mybatis-plus quick start:https://baomidou.com/guide/quick-start.html
文件目录结构
2.1、引入 MyBatis-Plus
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency>

2.2、配置 @MapperScan 注解
如果是放在主程序所在的包,@MapperScan 注解可以不写参数。
package com.example.boot;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@MapperScan("com.example.boot")@SpringBootApplicationpublic class SpringBootApiTest01Application {public static void main(String[] args) {SpringApplication.run(SpringBootApiTest01Application.class, args);}}
2.3、编写 Bean
Role
package com.example.boot.bean;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;@Data@TableName("t_s_role")public class Role {private String id;private String rolecode;private String rolename;private String role_type;}
2.4、编写 Mapper 接口
继承 BaseMapper
RoleMapper
package com.example.boot.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.example.boot.bean.Role;public interface RoleMapper extends BaseMapper<Role> {}
2.5、编写 controller
/role 接口,当不传递 id 时
package com.example.boot.controller;import com.example.boot.mapper.RoleMapper;import com.example.boot.response.Response;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestController@Slf4jpublic class RoleController extends BaseRestController {public RoleController() {log.info("RoleController 构造函数被调用了");}@Autowiredprivate RoleMapper roleMapper;@GetMapping(path = {"/role",})public Response role(@RequestParam(name = "id", defaultValue = "") String id) {if (id == null || id.length() <= 0) {// 查全部 rolereturn success("ok", roleMapper.selectList(null));} else {// 查单个 rolereturn success("ok", roleMapper.selectById(id));}}}


