关于java:MyBatisPlus之CRUD

5次阅读

共计 3836 个字符,预计需要花费 10 分钟才能阅读完成。

曾在博客园写下对于 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
 */@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. 这里就不列举测试了,和后面相似

正文完
 0