MyBatis-Plus之CRUD

曾在博客园写下关于MyBatis-Plus实战相关的文章,一共二十篇,不过那个时候都是基于MyBatis-Plus2.x,近来个人博客产品,技术框架升级,随之,MyBatis2.x升级到3.x,大改了从Entity、Dao到Service以及Controller等代码。html

若是有朋友还在使用MyBatis-Plus2.x版本的话,能够参考我在博客园写的一系列文章,文章连接为:
MP实战系列\(一共二十篇\)java

效果图分别以下:
image.pngweb

image.png

image.png

image.png

1、Entity

Entity又称数据模型,一般对应数据表。经常使用的注解如@TableName、@TableId、@TableField等,基本上没变,主要的变化是引用包路径发生改变。spring

2、Dao

image.png

查看了下BaseMapper,代码以下:mybatis

public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);
    int deleteById(Serializable id);
    int deleteByMap(@Param("cm") Map<String, Object> columnMap);
    int delete(@Param("ew") Wrapper<T> queryWrapper);
    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
    int updateById(@Param("et") T entity);
    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
    T selectById(Serializable id);
    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
    T selectOne(@Param("ew") Wrapper<T> queryWrapper);
    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}

变化不大,其中70%是我在2.x版本用过的,如insert、deleteById、delete、updateById、update、selectById、selectByMap、selectOne、selectList、selectPage等。app

要说变化的,Dao给我比较直观的感受是过去的EntityWrapper变成了QueryWrapper。这块也是我在个人博客产品里改动最多的地方之一。框架

3、Service

Service这层,主要体如今Controller调用的时候。ide

1.增长

image.png

2.删除

image.png

3.修改

image.png

4.查询

image.png

给我比较直观的感受,更简洁了。同时这块也是我改动最多了。测试

4、其它

从上面三点来看,很难看出变更大的具体是哪一个,基本上都是一些API变动,过去的方法名没有了,须要修改为新的。spa

要看具体版本更新,仍是得去官方文档看版本更新日志(我使用的是最新的3.4.1版本,这里不建议使用最新的,最新的意味着版本不稳定性,仍是使用3.x比较稳定的):

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

这里就不接着列举了,之因此列举出于这么几个考虑?
第1、在升级版本以前最好了解一些将要升级的版本主要新增哪些API或者修复哪些bug以及与原有版本的兼容性;
第2、对于咱们作开源项目有好处,了解他们的提交规范和从中发现哪些问题比较频繁。

5、CRUD例子

1.Service

package com.blog.tutorial06.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.blog.tutorial06.entity.Users;
import java.util.List;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:26
 */public interface UsersService extends IService<Users> {
    int add(Users user);
    int del(Long id);
    int modify(Users user);
    List<Users> selectAll();
}

2.Service实现类

package com.blog.tutorial06.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.tutorial06.entity.Users;
import com.blog.tutorial06.dao.UsersDao;
import com.blog.tutorial06.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:27
 */@Service
public class UsersServiceImpl extends ServiceImpl<UsersDao, Users> implements UsersService {
    @Autowired
 private UsersDao usersDao;
    @Override
 public int add(Users user) {
        return usersDao.insert(user);
    }
    @Override
 public int del(Long id) {
        return usersDao.deleteById(id);
    }
    @Override
 public int modify(Users user) {
        return usersDao.updateById(user);
    }
    @Override
 public List<Users> selectAll() {
        return usersDao.selectList(null);
    }
}

3.Controller

package com.blog.tutorial06.controller;
import com.blog.tutorial06.entity.Users;
import com.blog.tutorial06.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:27
 */@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
 private UsersService usersService;
    @PostMapping("/save")
    public int save(Users user) {
        return usersService.add(user);
    }
    @DeleteMapping("/del")
    public int del(Long id) {
        return usersService.del(id);
    }
    @PutMapping("/modify")
    public int modify(Users user) {
        return usersService.modify(user);
    }
    @GetMapping("/list")
    public List<Users> list() {
        return usersService.selectAll();
    }
}

4.这里就不列举测试了,和前面相似

相关文章
相关标签/搜索