曾在博客园写下对于MyBatis-Plus实战相干的文章,一共二十篇,不过那个时候都是基于MyBatis-Plus2.x,近来我的博客产品,技术框架降级,随之,MyBatis2.x降级到3.x,大改了从Entity、Dao到Service以及Controller等代码。
如果有敌人还在应用MyBatis-Plus2.x版本的话,能够参考我在博客园写的一系列文章,文章链接为:
MP实战系列\(一共二十篇\)
效果图别离如下:
一、Entity
Entity又称数据模型,通常对应数据表。罕用的注解如@TableName、@TableId、@TableField等,基本上没变,次要的变动是援用包门路产生扭转。
二、Dao
查看了下BaseMapper,代码如下:
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等。
要说变动的,Dao给我比拟直观的感觉是过来的EntityWrapper变成了QueryWrapper。这块也是我在我的博客产品里改变最多的中央之一。
三、Service
Service这层,次要体现在Controller调用的时候。
1.减少
2.删除
3.批改
4.查问
给我比拟直观的感觉,更简洁了。同时这块也是我改变最多了。
四、其它
从下面三点来看,很难看出变动大的具体是哪个,基本上都是一些API变更,过来的办法名没有了,须要批改成新的。
要看具体版本更新,还是得去官网文档看版本更新日志(我应用的是最新的3.4.1版本,这里不倡议应用最新的,最新的意味着版本不稳定性,还是应用3.x比较稳定的):
这里就不接着列举了,之所以列举出于这么几个思考?
第一、在降级版本之前最好理解一些将要降级的版本次要新增哪些API或者修复哪些bug以及与原有版本的兼容性;
第二、对于咱们做开源我的项目有益处,理解他们的提交标准和从中发现哪些问题比拟频繁。
五、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 */@Servicepublic 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(); }}