mybatis-plus 封装自己格式的curd

mybatis-plus-extension-3.1.0.jar

com.baomidou.mybatisplus.extension.service.impl

ServiceImpl 复制到项目中

添加自己的接口service实现类调用响应如下,即可生成规范的响应结构体

/**
 * 自定义CRUD 单查询
 * @param id
 * @return
 */
public BaseResponse selectById(Serializable id) {
	BaseResponse response = BaseResponse.Instance();
    T t = baseMapper.selectById(id);
    if(Objects.isNull(t)) {
    	response.CHANGE_STATUZ(Statuz.DATA_ERROR_404);
    } else {
    	response.addResult(extractClazzName(t.getClass().getName()), t);
    }
    return response;
}
/**
 * 自定义CRUD 创建单条记录
 * @param id
 * @return
 */
public BaseResponse insert(T entity) {
	BaseResponse response = BaseResponse.Instance();
	Integer result = baseMapper.insert(entity);
	if(null != result || result < 1) {
		response.CHANGE_STATUZ(Statuz.SYS_ERROR);
	}
    return response;
}
复制代码

转载于:https://juejin.im/post/5cf2d80de51d4555e372a59c

猜你喜欢

转载自blog.csdn.net/weixin_34186950/article/details/91416995
今日推荐