SpringBoot第七篇:整合Mybatis-Plus

作者:追梦1819
原文:https://www.cnblogs.com/yanfei1819/p/10880236.html
版权声明:本文为博主原创文章,转载请附上博文链接!

引言

  一看这个名字,就能看出与 MyBatis 有关。没错,它就是一款 MyBatis 的增强工具。

  下面我们先介绍这款工具,然后再介绍在 SpringBoot 中的使用。这样符合博主的习惯:在学习一个新的技术或者新的框架之前,一定会思考这个技术或者框架为什么会出现?解决了什么问题?有没有别的取代方案?


Mybatis Plus简介

1、概念

  MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

2、特性

  官网列举了很多特性,不过总的来说,无非就是封装好了常规的 CRUD 操作,同时内嵌了很多插件,减少了代码量。


使用

  我们来看看 MyBatis 在 SpringBoot 项目中的应用。

  准备工作,初始化数据库:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `age` int(3) NOT NULL,
  `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 21, 'admin');
INSERT INTO `user` VALUES (2, 22, 'test');

SET FOREIGN_KEY_CHECKS = 1;


首先,创建 SpringBoot 项目,引入 maven 依赖:

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

<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>



  然后,配置数据库信息:

server.port=8087

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://xx.xx.xx.xx:3306/test
spring.datasource.username=root
spring.datasource.password=root



  下面再创建一个实体类,以映射数据库字段:

package com.yanfei1819.mybatisplusdemo.entity;

/**
 * Created by 追梦1819 on 2019-05-17.
 */
public class User {
    private Long id;
    private String name;
    private int age;
    // get/set 省略
}



  然后,创建代理接口:

package com.yanfei1819.mybatisplusdemo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yanfei1819.mybatisplusdemo.entity.User;

/**
 * Created by 追梦1819 on 2019-05-17.
 */
//@Mapper
public interface UserMapper extends BaseMapper<User> {
}

  这里需要注意,需要通过启动类的 @MapperScan 注解或者代理接口上的 @Mapper 注解(本示例中用的是前者)。

  看到这一步,是不是很眼熟?很像前一篇的通用Mapper?

  这里说一句题外话,其实只要了解 MyBatis 的原理,自己都可以写出想要的插件。后续在 MyBatis 的专栏中,我将分析 Mybatis 的使用和原理。敬请关注。

  最后创建一个测试接口方法:

package com.yanfei1819.mybatisplusdemo.web.controller;

import com.yanfei1819.mybatisplusdemo.entity.User;
import com.yanfei1819.mybatisplusdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * Created by 追梦1819 on 2019-05-17.
 */
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/queryUsers")
    public List<User> queryUsers(){
        return userMapper.selectList(null);
    }
}



  启动程序:

package com.yanfei1819.mybatisplusdemo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.yanfei1819.mybatisplusdemo.mapper")
public class MybatisPlusDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusDemoApplication.class, args);
    }
}


  通过 postman 测试的结果:


常用API

  该插件封装好的方法有很多,以上只是演示了其中的查询列表的方法。下面是列举几个常用的方法。当然,如果感兴趣,想要了解更多,可以研究一下插件中的源码:com.baomidou.mybatisplus.core.mapper.BaseMapper

/**
 * 插入一条记录
 * @param entity 实体对象
 */
int insert(T entity);
/**
 * 根据 ID 删除
 * @param id 主键ID
 */
int deleteById(Serializable id);
/**
 * 根据 columnMap 条件,删除记录
 *
 * @param columnMap 表字段 map 对象
 */
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/**
 * 根据 ID 修改
 * @param entity 实体对象
 */
int updateById(@Param(Constants.ENTITY) T entity);
/**
 * 根据 ID 查询
 * @param id 主键ID
 */
T selectById(Serializable id);

/**
 * 根据 entity 条件,查询全部记录
 * @param queryWrapper 实体对象封装操作类(可以为 null)
 */
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);


参考

  1. MyBatis Plus 官网:https://mp.baomidou.com/
  2. 源码:com.baomidou.mybatisplus.core.mapper.BaseMapper


总结

  其实就个人来说,我并不喜欢这个工具。因为它给我的感觉是封装过度了,从某种程度上来说,把简单问题复杂化了。而且对应问题的解决方案有很多,比就如上一篇文章【SpringBoot第六篇:整合通用Mapper】中说到的通用Mapper,就是我比较喜欢的解决方案。

  当然,这只是个人的观点。技术与框架的选择,是多个因素的综合考虑的结果。



<全文完>

猜你喜欢

转载自www.cnblogs.com/yanfei1819/p/10880236.html