如何实现后端开发框架(三)-操作数据库

如何实现后端开发框架(三)-操作数据库

1. 问题描述

后端系统经常会把数据保存到数据库中,我们一般会使用Mybatis等持久层框架来操作数据库,但是一些简单的数据库增删改查(CURD)操作,Mybatis并没有提供简化的方法,那么我们在框架中如何简化这些操作呢?

2. 实现思路

寻找Mybatis的扩展框架来简化CURD操作,这里我选择的是MyBatis-Plus(https://baomidou.com/)(以前我还用过tk和pageHelper,现在用MyBatis-Plus就足够了)。

为了框架的扩展性,不要直接使用MyBatis-Plus,而是要通过框架的一些基类来集成,这样以后在基类中还可以增加一些额外的通用方法。

3. 实现步骤

3.1 实现Entity基类

@Data
@EqualsAndHashCode()
public class MyBaseEntity implements Serializable {
  private static final long serialVersionUID = 1L;

  @TableId(value = "ID")
  protected String id;
}

@Data和@EqualsAndHashCode()都是lombok注解,用于自动生成实体类的get,set等模板代码。

这里暂时只添加了一个id属性,表示每张数据库都有一个主键字段ID,后续文章中还会添加其它通用属性。

3.2 实现Controller基类

public abstract class MyBaseController<S extends MyBaseService<T>, T> {
  @Autowired protected S myBaseService;
}

myBaseService的作用是自动注入Controller类对应Service类的bean,用于简化后面使用Service中的方法。

3.2 实现Service基类

import com.baomidou.mybatisplus.extension.service.IService;

public interface MyBaseService<T> extends IService<T> {

}

这里暂时只是继承了MyBatis-Plus的IService接口,后续文章中还会添加其它通用方法。IService接口类中存在大量的CURD通用方法。

3.3 实现ServiceImpl基类

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

public class MyBaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T>
    implements MyBaseService<T> {
    
}

这里暂时只是继承了MyBatis-Plus的ServiceImpl接口,后续文章中还会添加其它通用方法。ServiceImpl接口类中存在大量的CURD通用方法。

3.4 实现Mapper基类

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

public interface MyBaseMapper<T> extends BaseMapper<T> {
    
}

这里继承了MyBatis-Plus的BaseMapper接口,BaseMapper接口类中存在大量的CURD通用方法。

总结:

通过继承MyBatis-Plus的Service和Mapper层基类,框架本身的每层基类上也具有了大量的CURD通用方法。

4. 测试代码

CURD操作的示例代码如下:

@RestController
@RequestMapping("/test/user")
public class UserController extends MyBaseController<UserService, User> {
  @RequestMapping(value = "/create", method = RequestMethod.POST)
  public boolean create(@RequestBody User user) throws Exception {
    boolean result = myBaseService.save(user);
    if (result == false) {
      throw new Exception("新增记录失败!");
    }
    return result;
  }

  @RequestMapping(value = "/update", method = RequestMethod.POST)
  public boolean update(@RequestBody User user) throws Exception {
    String id = user.getId();
    if (StringUtils.isBlank(id)) {
      throw new Exception("修改记录时id不能为空!");
    }
    boolean result = myBaseService.updateById(user);
    if (result == false) {
      throw new Exception("修改记录失败!");
    }
    return result;
  }

  @RequestMapping(value = "/getOne", method = RequestMethod.POST)
  public User getOne(@RequestBody User user) {
    User result = myBaseService.getOne(new QueryWrapper(user));
    return result;
  }

  @RequestMapping(value = "/list", method = RequestMethod.POST)
  public List<User> list(@RequestBody(required = false) User user) {
    List<User> result = myBaseService.list(new QueryWrapper(user));
    return result;
  }

  @RequestMapping(value = "/delete", method = RequestMethod.POST)
  public boolean delete(@RequestBody User user) throws Exception {
    String id = user.getId();
    if (StringUtils.isBlank(id)) {
      throw new Exception("删除记录时id不能为空!");
    }
    // 为了安全起见,只允许按id进行删除
    boolean result = myBaseService.removeById(user.getId());
    if (result == false) {
      throw new Exception("删除记录失败!");
    }
    return result;
  }
}

5. 完整代码

完整代码见以下Git仓库中的database-operation子项目:

https://github.com/randy0098/framework-samples

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值