Installation and use of reverse engineering plug-ins

Keywords: Java Database unit testing

introduce

Reverse engineering plug-ins refer to mybatis code helper and other plug-ins. Their function is to automatically generate entity classes, swagger, mapper, service and other files according to the written database table, so as to adapt to rapid iterative development
Forward engineering, such as spring data, automatically generates the corresponding database table according to the written entity class. It also meets the needs of rapid iterative development
Both have their own advantages. We only need to use these plug-ins / tools reasonably according to our own needs and use them to help us quickly meet the needs of rapid development

install

  1. Download plug-ins (latest)
    https://zhile.io/2019/04/23/mybatis-code-helper-pro-crack.html

  2. The first step is to download the compressed package, click ok and restart idea

use

  1. Connect to database

    After the connection is successful, as shown in the following figure

  2. Use reverse engineering to generate entity classes, services, mappers, etc. from database tables
    a. Remove the prefix, specify the primary key, specify the module where the micro service is located, specify the entity class, mapper, mapper.xml generation location, configure lombok annotation information, configure swagger and annotation, and configure mybatisPlus3

    b. Configure service interface

    b. Configure automatically generated impl classes

    Click ok in the lower right corner to generate it automatically

practical application

In practical application, the logic of adding, deleting, modifying and querying needs to be written according to our needs, so we need to modify the automatically generated content

  1. mapper interface (write specific interface content)
/**
 * You need to remove the inherited IService interface
 * The persistence interface is defined here
 */
public interface DictDataService{

    /**
     * Paging query dictionary data type
     *
     * @param dictDataDto
     * @return
     */
    DataGridView listPage(DictDataDto dictDataDto);


    /**
     * Insert new dictionary type
     *
     * @param dictDataDto
     * @return
     */
    int insert(DictDataDto dictDataDto);

    /**
     * Modified dictionary type
     *
     * @param dictDataDto
     * @return
     */
    int update(DictDataDto dictDataDto);

    /**
     * Delete dictionary type by ID
     *
     * @param dictCodeIds
     * @return
     */
    int deleteDictDataByIds(Long[] dictCodeIds);

    /**
     * Query dictionary data according to dictionary type
     *
     * @param dictType
     * @return
     */
    List<DictData> selectDictDataByDictType(String dictType);

    /**
     * Query a dictionary type by ID
     *
     * @param dictCode
     * @return
     */
    DictData selectDictDataById(Long dictCode);
}

Note: the mybatisplus and mapper.xml used here do not need to be changed. Simple addition, deletion, modification and query have been automatically generated for us, as shown in the following code

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. Write business implementation class
    Need attention
    QueryWrapper encapsulates query conditions, which is equivalent to where clause
    In the update operation, use the BeanUtil of hutool to copy the attributes of dto to po for update
    The writing method of batch deletion in the deleteDictDataByIds operation
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) {

        //Open paging operation
        Page<DictData> page = new Page<>(dictDataDto.getPageNum(), dictDataDto.getPageSize());
        //Encapsulate the query conditions, and realize the dictionary type, dictionary status and dictionary identification according to the complex query conditions of the front-end page
        QueryWrapper<DictData> qw = new QueryWrapper<>();
        //The encapsulation with judgment conditions is used here, which is equivalent to < if test = "taskname! = null and taskname! = ''" > and... < / if > of mybatis
        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>'
        //Cause: mapper inherits basemapper < dictdata > of the wrong path
        this.dictDataMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public int insert(DictDataDto dictDataDto) {
        //Replication is used here, because the attributes of dto are not po complete, and direct insertion will cause null value problems
        DictData dictData = new DictData();
        BeanUtil.copyProperties(dictDataDto,dictData);
        //Set creation time
        dictData.setCreateTime(new Date());
        //The setting creator dictDataDto inherits BaseDto, so you can use the properties of BaseDto
        dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
        return this.dictDataMapper.insert(dictData);
    }

    @Override
    public int update(DictDataDto dictDataDto) {
        //Copy update as above
        DictData dictData = new DictData();
        BeanUtil.copyProperties(dictDataDto,dictData);
        //Set modifier
        dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
        return this.dictDataMapper.updateById(dictData);
    }

    @Override
    public int deleteDictDataByIds(Long[] dictCodeIds) {
        //Convert array to 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);//available
        return this.dictDataMapper.selectList(qw);
    }

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

}


  1. Encapsulates the object that the user controller uses to return
package com.hrt.vo;

import com.hrt.contants.HttpStatus;

import java.util.HashMap;

/**
 * General return object class
 *
 * @author caohaiyang
 * @create 2021-07-09 05:23 PM
 */
public class AjaxResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    /**
     * Status code
     */
    public static final String CODE_TAG = "code";

    /**
     * Return content
     */
    public static final String MSG_TAG = "msg";

    /**
     * data object
     */
    public static final String DATA_TAG = "data";

    /**
     * Total number of data
     */
    public static final String DATA_TOTAL = "total";

    /**
     * Initialize a newly created Ajax result object to represent an empty message.
     */
    public AjaxResult() {
    }

    /**
     * Initialize a newly created Ajax result object
     *
     * @param code Status code
     * @param msg  Return content
     */
    public AjaxResult(int code, String msg) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    /**
     * Initialize a newly created Ajax result object
     *
     * @param code Status code
     * @param msg  Return content
     * @param data data object
     */
    public AjaxResult(int code, String msg, Object data) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        super.put(DATA_TAG, data);
    }

    /**
     * Initialize a newly created Ajax result object
     *
     * @param code  Status code
     * @param msg   Return content
     * @param data  data object
     * @param total Total number of data
     */
    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 success message
     *
     * @return Success message
     */
    public static AjaxResult success() {
        return AjaxResult.success("Operation succeeded");
    }

    /**
     * Return success data
     *
     * @return Success message
     */
    public static AjaxResult success(Object data) {
        return AjaxResult.success("Operation succeeded", data);
    }

    /**
     * Return success message
     *
     * @param msg Return content
     * @return Success message
     */
    public static AjaxResult success(String msg) {
        return AjaxResult.success(msg, null);
    }

    /**
     * Return success message
     *
     * @param msg  Return content
     * @param data data object
     * @return Success message
     */
    public static AjaxResult success(String msg, Object data) {
        return new AjaxResult(HttpStatus.SUCCESS, msg, data);
    }

    /**
     * Return success message
     *
     * @param msg  Return content
     * @param data data object
     * @return Success message
     */
    public static AjaxResult success(String msg, Object data,Long total) {
        return new AjaxResult(HttpStatus.SUCCESS, msg, data,total);
    }

    /**
     * Return failure message
     */
    public static AjaxResult fail() {
        return AjaxResult.fail("operation failed");
    }

    /**
     * Return failure message
     *
     * @param msg Return content
     * @return Warning message
     */
    public static AjaxResult fail(String msg) {
        return AjaxResult.fail(msg, null);
    }

    /**
     * Return failure message
     *
     * @param msg  Return content
     * @param data data object
     * @return Warning message
     */
    public static AjaxResult fail(String msg, Object data) {
        return new AjaxResult(HttpStatus.BAD_REQUEST, msg, data);
    }

    /**
     * Return error message
     */
    public static AjaxResult error() {
        return AjaxResult.error("operation failed");
    }

    /**
     * Return error message
     *
     * @param msg Return content
     * @return Warning message
     */
    public static AjaxResult error(String msg) {
        return AjaxResult.error(msg, null);
    }

    /**
     * Return error message
     *
     * @param msg  Return content
     * @param data data object
     * @return Warning message
     */
    public static AjaxResult error(String msg, Object data) {
        return new AjaxResult(HttpStatus.ERROR, msg, data);
    }

    /**
     * Return error message
     *
     * @param code Status code
     * @param msg  Return content
     * @return Warning message
     */
    public static AjaxResult error(int code, String msg) {
        return new AjaxResult(code, msg, null);
    }

    /**
     * Return error message
     *
     * @param rows Status code
     * @return Add modify delete conversion information
     */
    public static AjaxResult toAjax(int rows) {
        return rows > 0 ? AjaxResult.success() : AjaxResult.fail();
    }
}
  1. The controller returns the page
    Note: use of @ NotEmpty() annotation
@RestController
@RequestMapping("system/dict/data")
public class DictDataController {
    @Autowired
    private DictDataService dictDataService;


    /**
     * Paging query
     *
     * @param dictDataDto Dictionary data dto
     * @return
     */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DictDataDto dictDataDto) {
        DataGridView gridView = this.dictDataService.listPage(dictDataDto);
        return AjaxResult.success("query was successful", gridView.getData(), gridView.getTotal());
    }

    /**
     * Add dictionary data
     *
     * @param dictDataDto Dictionary data dto. The attribute needs to be verified when adding
     * @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));
    }

    /**
     * Query a dictionary information according to ID
     *
     * @param dictCode Dictionary id
     * @return
     */
    @GetMapping("getOne/{dictCode}")
    public AjaxResult getDictData(@PathVariable @Validated @NotNull(message = "Dictionaries ID Cannot be empty") Long dictCode) {
        return AjaxResult.success(this.dictDataService.selectDictDataById(dictCode));
    }


    /**
     * Batch delete
     *
     * @param dictCodeIds
     * @return
     */
    @DeleteMapping("deleteDictDataByIds/{dictCodeIds}")
    public AjaxResult delectDictData(@PathVariable @Validated @NotEmpty(message = "To delete ID Cannot be empty") Long[] dictCodeIds) {
        return AjaxResult.toAjax(this.dictDataService.deleteDictDataByIds(dictCodeIds));
    }

    /**
     * Query all available dictionary types
     * @param dictType  Dictionary type
     * @return
     */
    @GetMapping("getDataByType/{dictType}")
    public AjaxResult getDataByType(@PathVariable @Validated @NotEmpty(message = "Dictionary type cannot be empty") String dictType) {
        return AjaxResult.success(this.dictDataService.selectDictDataByDictType(dictType));
    }
}
  1. Access test
    After starting the project, use the information introduced in our last blog yapi Conduct access test

Posted by SunsetKnight on Sun, 12 Sep 2021 15:06:33 -0700