前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mybatis code helper安装与使用

mybatis code helper安装与使用

作者头像
时间静止不是简史
发布2022-01-05 14:06:07
1.3K0
发布2022-01-05 14:06:07
举报
文章被收录于专栏:Java探索之路

介绍

逆向工程插件指的是: mybatis code helper 等一类插件, 他的作用是, 可以根据写好的数据库表自动生成实体类, swagger以及mapper, service等文件, 适应快速迭代开发. 正向工程, 如: SpringData 等则是根据写好的实体类自动生成对应的数据库表. 同样也是适应快速迭代开发的需要. 二者各有千秋, 我们只需要根据自己的需求去合理的使用这些插件 / 工具, 利用其帮助我们快速达到快速开发的需要.

安装

  1. 下载插件(最新即可) https://zhile.io/2019/04/23/mybatis-code-helper-pro-crack.html
在这里插入图片描述
在这里插入图片描述
  1. 引入第一步下载好的压缩包, 点击ok然后重启idea即可.
在这里插入图片描述
在这里插入图片描述

使用

  1. 连接数据库
在这里插入图片描述
在这里插入图片描述

连接成功后, 如下图所示

在这里插入图片描述
在这里插入图片描述
  1. 使用逆向工程根据数据库表生成实体类, service, mapper等 a. 去除前缀, 指定主键, 指定微服务所在模块. 指定实体类, mapper, mapper.xml生成位置, 配置lombok注解信息, 配置swagger以及注释, 配置mybatisPlus3
在这里插入图片描述
在这里插入图片描述

b. 配置service接口

在这里插入图片描述
在这里插入图片描述

b. 配置自动生成的impl类

在这里插入图片描述
在这里插入图片描述

点击右下角ok, 自动生成即可

实际应用

在实际应用中, 这些增删改查的逻辑是需要我们根据需求去编写的, 因此需要我们对自动生成的内容进行修改

  1. mapper接口(编写具体接口内容)
代码语言:javascript
复制
/**
 * 这里需要去除继承IService接口
 * 在这里定义持久化接口
 */
public interface DictDataService{

    /**
     * 分页查询字典数据类型
     *
     * @param dictDataDto
     * @return
     */
    DataGridView listPage(DictDataDto dictDataDto);


    /**
     * 插入新的字典类型
     *
     * @param dictDataDto
     * @return
     */
    int insert(DictDataDto dictDataDto);

    /**
     * 修改的字典类型
     *
     * @param dictDataDto
     * @return
     */
    int update(DictDataDto dictDataDto);

    /**
     * 根据ID删除字典类型
     *
     * @param dictCodeIds
     * @return
     */
    int deleteDictDataByIds(Long[] dictCodeIds);

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType
     * @return
     */
    List<DictData> selectDictDataByDictType(String dictType);

    /**
     * 根据ID查询一个字典类型
     *
     * @param dictCode
     * @return
     */
    DictData selectDictDataById(Long dictCode);
}

注意: 这里使用的mybatisplus, mapper.xml无需更改, 简单的增删改查已经为我们自动生成, 如下代码

代码语言:javascript
复制
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> wrapper);

    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);
}
  1. 编写业务实现类 需要注意 QueryWrapper 对查询条件的封装, 相当于where子句. update操作中, 利用hutool的BeanUtil, 将dto的属性复制到po上进行更新 deleteDictDataByIds操作中, 批量删除的写法
代码语言:javascript
复制
package com.hrt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrt.contants.Contants;
import com.hrt.domain.DictData;
import com.hrt.dto.DictDataDto;
import com.hrt.mapper.DictDataMapper;
import com.hrt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hrt.service.DictDataService;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class DictDataServiceImpl implements DictDataService{
    @Autowired
    private DictDataMapper dictDataMapper;

    @Override
    public DataGridView listPage(DictDataDto dictDataDto) {

        //开启分页操作
        Page<DictData> page = new Page<>(dictDataDto.getPageNum(), dictDataDto.getPageSize());
        //封装查询条件, 根据前端页面的复杂查询条件实现   字典类型, 字典状态, 字典标识
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        //这里使用的是带判断条件的封装  相当于mybatis 的<if test="taskName !=null and taskName !='' ">and...</if>
        qw.eq(StringUtils.isNotBlank(dictDataDto.getDictType()),DictData.COL_DICT_TYPE, dictDataDto.getDictType());
        qw.eq(StringUtils.isNoneBlank(dictDataDto.getStatus()),DictData.COL_STATUS, dictDataDto.getStatus());
        qw.like(StringUtils.isNotBlank(dictDataDto.getDictLabel()),DictData.COL_DICT_LABEL, dictDataDto.getDictLabel());
        //Inferred type 'E' for type parameter 'E' is not within its bound; should implement 'com.baomidou.mybatisplus.core.metadata.IPage<DictData>'
        //原因: mapper继承了错误路径的BaseMapper<DictData>
        this.dictDataMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public int insert(DictDataDto dictDataDto) {
        //这里采用的是复制, 因为dto的属性没有po全, 直接插入会带来空值问题
        DictData dictData = new DictData();
        BeanUtil.copyProperties(dictDataDto,dictData);
        //设置创建时间
        dictData.setCreateTime(new Date());
        //设置创建者  dictDataDto继承了 BaseDto, 因此可以使用BaseDto的属性
        dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
        return this.dictDataMapper.insert(dictData);
    }

    @Override
    public int update(DictDataDto dictDataDto) {
        //复制更新同上
        DictData dictData = new DictData();
        BeanUtil.copyProperties(dictDataDto,dictData);
        //设置修改人
        dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
        return this.dictDataMapper.updateById(dictData);
    }

    @Override
    public int deleteDictDataByIds(Long[] dictCodeIds) {
        //将数组转化成list
        List<Long> longList = Arrays.asList(dictCodeIds);
        if (CollectionUtil.isEmpty(longList)) {
            return -1;
        } else {
            return this.dictDataMapper.deleteBatchIds(longList);
        }
    }

    @Override
    public List<DictData> selectDictDataByDictType(String dictType) {
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        qw.eq(DictData.COL_DICT_TYPE, dictType);
        qw.eq(DictData.COL_STATUS, Contants.STATUS_TRUE);//可用
        return this.dictDataMapper.selectList(qw);
    }

    @Override
    public DictData selectDictDataById(Long dictCode) {
        return this.dictDataMapper.selectById(dictCode);
    }

}
  1. 封装用户controller用于返回的对象
代码语言:javascript
复制
package com.hrt.vo;

import com.hrt.contants.HttpStatus;

import java.util.HashMap;

/**
 * 通用返回对象类
 *
 * @author caohaiyang
 * @create 2021-07-09 下午 05:23
 */
public class AjaxResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    public static final String CODE_TAG = "code";

    /**
     * 返回内容
     */
    public static final String MSG_TAG = "msg";

    /**
     * 数据对象
     */
    public static final String DATA_TAG = "data";

    /**
     * 数据总条数
     */
    public static final String DATA_TOTAL = "total";

    /**
     * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
     */
    public AjaxResult() {
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     */
    public AjaxResult(int code, String msg) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     * @param data 数据对象
     */
    public AjaxResult(int code, String msg, Object data) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        super.put(DATA_TAG, data);
    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param code  状态码
     * @param msg   返回内容
     * @param data  数据对象
     * @param total 数据总条数
     */
    public AjaxResult(int code, String msg, Object data, Long total) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        super.put(DATA_TAG, data);
        super.put(DATA_TOTAL, total);
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static AjaxResult success() {
        return AjaxResult.success("操作成功");
    }

    /**
     * 返回成功数据
     *
     * @return 成功消息
     */
    public static AjaxResult success(Object data) {
        return AjaxResult.success("操作成功", data);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static AjaxResult success(String msg) {
        return AjaxResult.success(msg, null);
    }

    /**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static AjaxResult success(String msg, Object data) {
        return new AjaxResult(HttpStatus.SUCCESS, msg, data);
    }

    /**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static AjaxResult success(String msg, Object data,Long total) {
        return new AjaxResult(HttpStatus.SUCCESS, msg, data,total);
    }

    /**
     * 返回失败消息
     */
    public static AjaxResult fail() {
        return AjaxResult.fail("操作失败");
    }

    /**
     * 返回失败消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResult fail(String msg) {
        return AjaxResult.fail(msg, null);
    }

    /**
     * 返回失败消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static AjaxResult fail(String msg, Object data) {
        return new AjaxResult(HttpStatus.BAD_REQUEST, msg, data);
    }

    /**
     * 返回错误消息
     */
    public static AjaxResult error() {
        return AjaxResult.error("操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResult error(String msg) {
        return AjaxResult.error(msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static AjaxResult error(String msg, Object data) {
        return new AjaxResult(HttpStatus.ERROR, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg  返回内容
     * @return 警告消息
     */
    public static AjaxResult error(int code, String msg) {
        return new AjaxResult(code, msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param rows 状态码
     * @return 添加修改删除转化信息
     */
    public static AjaxResult toAjax(int rows) {
        return rows > 0 ? AjaxResult.success() : AjaxResult.fail();
    }
}
  1. controller进行页面返回 注意: @NotEmpty()注解的使用
代码语言:javascript
复制
@RestController
@RequestMapping("system/dict/data")
public class DictDataController {
    @Autowired
    private DictDataService dictDataService;


    /**
     * 分页查询
     *
     * @param dictDataDto 字典数据dto
     * @return
     */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DictDataDto dictDataDto) {
        DataGridView gridView = this.dictDataService.listPage(dictDataDto);
        return AjaxResult.success("查询成功", gridView.getData(), gridView.getTotal());
    }

    /**
     * 新增字典数据
     *
     * @param dictDataDto 字典数据dto,新增时需要校验属性
     * @return
     */
    @PostMapping("addDictData")
    public AjaxResult addDictData(@Validated DictDataDto dictDataDto) {
        dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        int insert = this.dictDataService.insert(dictDataDto);
        return AjaxResult.success(insert);
    }

    @PutMapping("updateDictData")
    public AjaxResult updateDictData(@Validated DictDataDto dictDataDto) {
        dictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.success(this.dictDataService.update(dictDataDto));
    }

    /**
     * 根据 ID 查询一个字典信息
     *
     * @param dictCode 字典id
     * @return
     */
    @GetMapping("getOne/{dictCode}")
    public AjaxResult getDictData(@PathVariable @Validated @NotNull(message = "字典ID不能为空") Long dictCode) {
        return AjaxResult.success(this.dictDataService.selectDictDataById(dictCode));
    }


    /**
     * 批量删除
     *
     * @param dictCodeIds
     * @return
     */
    @DeleteMapping("deleteDictDataByIds/{dictCodeIds}")
    public AjaxResult delectDictData(@PathVariable @Validated @NotEmpty(message = "要删除的 ID 不 能为空") Long[] dictCodeIds) {
        return AjaxResult.toAjax(this.dictDataService.deleteDictDataByIds(dictCodeIds));
    }

    /**
     * 查询所有可用的字典类型
     * @param dictType  字典类型
     * @return
     */
    @GetMapping("getDataByType/{dictType}")
    public AjaxResult getDataByType(@PathVariable @Validated @NotEmpty(message = "字典类型不能为空") String dictType) {
        return AjaxResult.success(this.dictDataService.selectDictDataByDictType(dictType));
    }
}
  1. 访问测试 启动项目后. 使用我们上个博客介绍的 yapi 进行访问测试
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/09/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 安装
  • 使用
  • 实际应用
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档