mybatis|springboot 整合mybatis plus

简单介绍下 mybatis plus,mybatis plus(简称 MP)是一个 mybatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
本文讲解使用 springboot 框架整合 mybatis plus,搭建开发环境,并介绍 mybatis plus 中一些常见的方法使用

1、创建项目
使用 eclipse、sts 编辑器或 idea 创建 springboot 项目,项目名称就使用 spring boot 和 mybatis plus 的开头英文字母命名吧,即 sbmp
创建项目完成后,添加需要使用的依赖,依赖 pom 文件如下

【mybatis|springboot 整合mybatis plus】 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.13.RELEASE com.sbmp sbmp 0.0.1-SNAPSHOT sbmp Demo project for Spring Boot 1.8 3.1.1 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter 3.3.1 mysql mysql-connector-java org.projectlombok lombok org.springframework.boot spring-boot-devtools runtime org.springframework.boot spring-boot-maven-plugin


依赖添加完成后,配置 application.yml 文件
spring: datasource: url: jdbc:mysql://192.168.0.110:3306/sbmp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 #mybatis plus 配置 mybatis-plus: mapper-locations: classpath:mapper/*Mapper.xml global-config: banner: false db-config: id-type: uuid table-underline: true configuration: call-setters-on-nulls: true #sql打印 logging: level: com.sbmp.mapper: debug

数据库的配置,和 sql打印的配置就不多说了
mybatis plus 的配置
mapper-locations 指定 mybatis xml文件的位置
global-config: banner: false不显示 mybatis plus 的 banner
global-config: db-config: id-type: uuid 全局默认主键类型为 uuid
global-config: db-config: table-underline: true 表名是否使用驼峰转下划线命名,只对表名生效
configuration: call-setters-on-nulls: true 返回查询语句中某些字段值是null的

yml文件配置完成后,在 resources 目录下创建 mapper 文件夹,用来放 xml 文件
项目创建完成后,结构如下
mybatis|springboot 整合mybatis plus
文章图片


springboot 项目启动类
配置 mapper 扫描包路径
package com.sbmp; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.sbmp.mapper") @SpringBootApplication public class SbmpApplication { public static void main(String[] args) { SpringApplication.run(SbmpApplication.class, args); }}



2、数据库建表
建表 sql 如下
人物表
DROP TABLE IF EXISTS `sbmp_user`; CREATE TABLE `sbmp_user`( `user_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `user_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `nick_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `department_id` int(5) NULL DEFAULT NULL, `create_date` datetime(0) NULL DEFAULT NULL, `del_flag` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`user_id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of sbmp_user -- ---------------------------- INSERT INTO `sbmp_user` VALUES ('01061280e061419b98069b8efd6be804', '周通', '小霸王', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('02e8a27163984dca94876ec7d85f0389', '彭玘', '天目将', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('123d2bddd5d3465c9ab531d183a7600b', '杨志', '青面兽', 4, '2020-08-12 14:58:09', '0'); INSERT INTO `sbmp_user` VALUES ('15f578aac45846a7a0a0eb05f9a70eb4', '呼延灼', '双鞭', 3, '2020-08-11 14:54:20', '0'); INSERT INTO `sbmp_user` VALUES ('16226f66f6e943b1836452ca6cc95076', '孙立', '病尉迟', 5, '2020-08-18 07:04:56', '0'); INSERT INTO `sbmp_user` VALUES ('17ed7e281c3c43d2bb3dbcf1a4c19139', '刘唐', '赤发鬼', 6, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('24cb471e3ed14d069fd725c673d6a18c', '解珍', '两头蛇', 6, '2020-08-18 15:20:41', '0'); INSERT INTO `sbmp_user` VALUES ('2fef80719549413eaaf09b4f6e3e097b', '武松', '行者', 6, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('37d675c8670e4908897437fed4030a98', '黄信', '镇三山', 5, '2020-08-18 07:03:16', '0'); INSERT INTO `sbmp_user` VALUES ('394f70197cf34bd5869469f6313c7277', '穆弘', '没遮拦', 4, '2020-08-18 07:02:07', '0'); INSERT INTO `sbmp_user` VALUES ('39b78c88fd5d43daa8bd5da80b5b223d', '鲁智深', '花和尚', 6, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('3ff9a386eb8b419784cf1136b430636d', '杨林', '锦豹子', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('4427b546e58a493990f4633270a09a61', '董平', '双枪将', 3, '2020-08-18 06:55:51', '0'); INSERT INTO `sbmp_user` VALUES ('469602c0533743c081a0a49042a61d57', '雷横', '插翅虎', 6, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('4ec8fe199e104492bd5d9e4f6f2fc06e', '张清', '没羽箭', 4, '2020-08-13 07:00:31', '0'); INSERT INTO `sbmp_user` VALUES ('538e80dc2ef14e94852112da14f42508', '陈达', '跳涧虎', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('5b1e615d92e34471ba18b7d6b34d112e', '杨春', '白花蛇', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('65bce3c9ae884a18952cbe5e429071c0', '李忠', '打虎将', 7, '2020-08-18 15:21:20', '0'); INSERT INTO `sbmp_user` VALUES ('703f218227574ba29aa8ac8c49c967aa', '秦明', '霹雳火', 3, '2020-08-11 14:53:34', '0'); INSERT INTO `sbmp_user` VALUES ('744bf74937384a96a5a44be053da6d02', '石秀', '拼命三郎', 6, '2020-08-16 15:20:31', '0'); INSERT INTO `sbmp_user` VALUES ('7b3c7296d12b4700936cca6440ca60bf', '燕青', '浪子', 6, '2020-08-15 15:20:24', '0'); INSERT INTO `sbmp_user` VALUES ('7c2052f30ac741f7bf4e5effee164965', '马麟', '铁笛仙', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('80d4eda453f943a5aa60a1642dae6fbc', '吴用', '智多星', 2, '2020-08-10 14:49:42', '0'); INSERT INTO `sbmp_user` VALUES ('8a7436a6f5884cc5a59908ec73060d2a', '燕顺', '锦毛虎', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('8d3396996aa04cb1bb71ce17cb2624c4', '邓飞', '火眼狻猊', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('936eca00e3ba47528341f3ffd2e99363', '欧鹏', '摩云金翅', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('975afbff93a443839c9cb036c2a2d3a2', '李逵', '黑旋风', 6, '2020-08-15 15:20:28', '0'); INSERT INTO `sbmp_user` VALUES ('9d36acdc42c149cd88c323e92bb1458f', '卢俊义', '玉麒麟', 1, '2020-08-09 14:48:36', '0'); INSERT INTO `sbmp_user` VALUES ('a16cf2ef4030405cbc8a2f774eeba504', '魏定国', '神火将', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('a7f5684c7906485cbd8fd8e623192bfc', '关胜', '大刀', 3, '2020-08-11 14:51:31', '0'); INSERT INTO `sbmp_user` VALUES ('aabed1004c4f44ddb5f01b48275fbb57', '林冲', '豹子头', 3, '2020-08-11 14:53:04', '0'); INSERT INTO `sbmp_user` VALUES ('adbec35c5e664c47ad24eafd15e832c5', '单廷', '圣水将', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('b799d4f3e4a842d19b2bb0f75c634f4f', '索超', '急先锋', 4, '2020-08-12 06:58:53', '0'); INSERT INTO `sbmp_user` VALUES ('b8ce96e985974f92a29f12c91bb9a70d', '史进', '九纹龙', 4, '2020-08-18 07:01:42', '0'); INSERT INTO `sbmp_user` VALUES ('bcf6f9425beb4372b1d9eb759668d394', '公孙胜', '入云龙', 2, '2020-08-10 14:50:37', '0'); INSERT INTO `sbmp_user` VALUES ('c2155bd6cb854afd9e8653c30143c97a', '韩滔', '百胜将', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('c70b18768f1a4368a2bcf5a7d0c6d0c7', '解宝', '双尾蝎', 6, '2020-08-17 15:20:38', '0'); INSERT INTO `sbmp_user` VALUES ('c9b00a4ad55b4e4bb1009ee0907bd777', '宣赞', '丑郡马', 5, '2020-08-18 07:08:46', '0'); INSERT INTO `sbmp_user` VALUES ('e9cf6112649944348113986e3026553b', '花荣', '小李广', 4, '2020-08-12 14:56:41', '0'); INSERT INTO `sbmp_user` VALUES ('f138761dd1e74b849c6c76944cd66b86', '朱仝', '美髯公', 4, '2020-08-18 07:01:08', '0'); INSERT INTO `sbmp_user` VALUES ('f9cbf631fdd74ad492179a1e920ea757', '徐宁', '金枪手', 4, '2020-08-12 14:57:24', '0'); INSERT INTO `sbmp_user` VALUES ('fc19642ee7e34353908af012d20e6f11', '杨雄', '病关索', 6, '2020-08-17 15:20:35', '0'); INSERT INTO `sbmp_user` VALUES ('fd4720f79ea04b348d8d650b619a0e23', '宋江', '呼保义', 1, '2020-08-08 14:47:44', '0'); INSERT INTO `sbmp_user` VALUES ('ff98a17f60544eeaa7a37a009ae2ab40', '郝思文', '井木犴', 5, '2020-08-18 07:08:46', '0'); SET FOREIGN_KEY_CHECKS = 1;


部门表
DROP TABLE IF EXISTS `sbmp_department`; CREATE TABLE `sbmp_department`( `department_id` int(11) NULL DEFAULT NULL, `department_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of sbmp_department -- ---------------------------- INSERT INTO `sbmp_department` VALUES (1, '梁山泊总兵都头领'); INSERT INTO `sbmp_department` VALUES (2, '梁山泊掌管机密军师'); INSERT INTO `sbmp_department` VALUES (3, '马军五虎将'); INSERT INTO `sbmp_department` VALUES (4, '马军八骠骑兼先锋使'); INSERT INTO `sbmp_department` VALUES (5, '马军小彪将兼远探出哨头领'); INSERT INTO `sbmp_department` VALUES (6, '步军头领'); INSERT INTO `sbmp_department` VALUES (7, '步军将校'); INSERT INTO `sbmp_department` VALUES (8, '梁山泊四寨水军头领'); INSERT INTO `sbmp_department` VALUES (9, '梁山泊四店打听声息邀接来宾头领'); INSERT INTO `sbmp_department` VALUES (10, '梁山泊总探声息头领'); INSERT INTO `sbmp_department` VALUES (11, '梁山泊军中走报机密步军头领'); INSERT INTO `sbmp_department` VALUES (12, '守护中军马军骁将'); INSERT INTO `sbmp_department` VALUES (13, '守护中军步军骁将'); INSERT INTO `sbmp_department` VALUES (14, '梁山泊专掌行刑刽子'); INSERT INTO `sbmp_department` VALUES (15, '专掌三军内探事马军头领'); INSERT INTO `sbmp_department` VALUES (16, '梁山泊一同参赞军务头领'); INSERT INTO `sbmp_department` VALUES (17, '梁山泊掌管监造诸事头领'); INSERT INTO `sbmp_department` VALUES (18, '掌管行文走徼调兵遣将'); SET FOREIGN_KEY_CHECKS = 1;



3、创建 bean、mapper、service、controller 层
这里为了演示,笔者以水浒传里的人物为数据,进行演示

创建User实体类
当表名与实体类名不一致时,可以通过注解@TableName进行映射
注解@TableId可指定主键字段,设置主键类型,如 uuid 或 主键自增
package com.sbmp.bean; import java.util.Date; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @TableName("sbmp_user") @Data public class User { @TableId(type = IdType.ASSIGN_UUID) private String userId; private String userName; private String nickName; private Integer departmentId; private Date createDate; private String delFlag; }


创建Department 实体类
package com.sbmp.bean; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @TableName("sbmp_department") @Data public class Department { @TableId(type = IdType.AUTO) private Integer departmentId; private String departmentName; }


创建 mapper 层
UserMapper
package com.sbmp.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.sbmp.bean.User; public interface UserMapper extends BaseMapper{}

DepartmentMapper
package com.sbmp.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.sbmp.bean.Department; public interface DepartmentMapper extends BaseMapper{}



创建 service 层
UserService
package com.sbmp.service; import com.baomidou.mybatisplus.extension.service.IService; import com.sbmp.bean.User; public interface UserService extends IService{}

DepartmentService
package com.sbmp.service; import com.baomidou.mybatisplus.extension.service.IService; import com.sbmp.bean.Department; public interface DepartmentService extends IService{}


创建 service 实现层
UserServiceImpl
package com.sbmp.service.impl; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.sbmp.bean.User; import com.sbmp.mapper.UserMapper; import com.sbmp.service.UserService; @Service public class UserServiceImpl extends ServiceImpl implements UserService{}

DepartmentServiceImpl
package com.sbmp.service.impl; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.sbmp.bean.Department; import com.sbmp.mapper.DepartmentMapper; import com.sbmp.service.DepartmentService; @Service public class DepartmentServiceImpl extends ServiceImpl implements DepartmentService {}


创建 controller 层
package com.sbmp.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.sbmp.bean.Department; import com.sbmp.service.DepartmentService; @RequestMapping("/department") @RestController public class DepartmentController { @Autowired private DepartmentService departmentService; @RequestMapping("/list") public Object list() { List list = departmentService.list(); return list; }}


以上各层创建完成后,springboot 整合mybatis plus 环境基本搭建完成

4、测试
浏览器访问:http://localhost:8080/department/list
效果如下图
mybatis|springboot 整合mybatis plus
文章图片



返回这个结果,说明搭建成功

初次使用 mybatis plus 的人可能会有疑问,没写sql,为什么能查出全部部门表数据,这就是mybatis plus的强大之处,它已经封装了基本的sql,后面笔者慢慢介绍

5、自定义 sql
除了使用 mybatis plus 封装好的 sql 外,mybatis plus 还可以像使用 mybatis 一样,在xml 中写sql,下面讲解如何实现自定义sql

(1)、新建实体类 UserDetail
用来映射关联后的数据
package com.sbmp.bean; import lombok.Data; @Data public class UserDetail { private String userName; private String nickName; private String departmentName; }

(2)、UserMapper 中添加新的方法
package com.sbmp.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.sbmp.bean.User; import com.sbmp.bean.UserDetail; public interface UserMapper extends BaseMapper{ UserDetail getUserDetail(String id); }

(3)UserMapper.xml 中写对应的 sql 语句
select user_name as userName, nick_name as nickName, department_name as departmentName from sbmp_user u, sbmp_department d where u.department_id = d.department_id and u.user_id = #{id}

(4)、修改 UserService及 UserService实现层
package com.sbmp.service; import com.baomidou.mybatisplus.extension.service.IService; import com.sbmp.bean.User; import com.sbmp.bean.UserDetail; public interface UserService extends IService{ UserDetail getUserDetail(String id); }

UserService实现层
package com.sbmp.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.sbmp.bean.User; import com.sbmp.bean.UserDetail; import com.sbmp.mapper.UserMapper; import com.sbmp.service.UserService; @Service public class UserServiceImpl extends ServiceImpl implements UserService{ @Autowired private UserMapper userMapper; @Override public UserDetail getUserDetail(String id) { return userMapper.getUserDetail(id); }}

(5)、UserController
package com.sbmp.controller; 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 com.sbmp.service.UserService; @RequestMapping("/user") @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/get/{id}") public Object getUserDetail(@PathVariable("id")String id) { return userService.getUserDetail(id); }}

(6)测试
浏览器访问:http://localhost:8080/user/get/bcf6f9425beb4372b1d9eb759668d394
效果如下
mybatis|springboot 整合mybatis plus
文章图片


至此, springboot 框架整合 mybatis plus,搭建开发环境完成
关于mybatis plus 的自带 sql 实现增删改查,后续介绍




    推荐阅读