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.這裏就不列舉測試了,和前面相似

相關文章
相關標籤/搜索