MyBatis-Plus结合Swagger实现接口代码及文档自动生成工具(实践篇)

                                    MyBatis-Plus结合Swagger实现接口代码及文档自动生成工具(实践篇)

         通过前面三篇文章的介绍自动化生成代码工具我们理论上是已经实现了,可是到底行不行好不好读者心里还是个大大的疑问?

这篇文章我就来一步步带大家使用工具快速构建自己的项目。

         第一步:

        访问我的GitHub主页下载以下三个工程的源码。

插件定义源码

插件实现原源码

插件测试源码

 

 第二步:

将autoCodePlugin工程和autoCodePluginImpl工程打成jar包,并上传到你本地的仓库,因为测试的时候需要引入jar包。

 

 第三步:

 导入autoCode项目到你的开发工具,我用的是eclipse,如图:

 MyBatis-Plus结合Swagger实现接口代码及文档自动生成工具(实践篇)_第1张图片

第四步:

文件中配置好你真实的数据库链接,然后执行该插件。这篇文章仅仅做为讲解,因此我只生成一个数据表。得到结构如下:

MyBatis-Plus结合Swagger实现接口代码及文档自动生成工具(实践篇)_第2张图片

可以看到model和mapper路径下生成了对应的文件,接着我们打开TOperHis.java文件,如下:

package com.zte.model;

import java.io.Serializable;
import java.util.Date;

public class TOperHis implements Serializable {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.oper_his_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    private Long operHisId;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.oper_user_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    private Long operUserId;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.function_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    private Long functionId;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.oper_time
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    private Date operTime;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.oper_content
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    private String operContent;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.remark
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    private String remark;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table t_oper_his
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    private static final long serialVersionUID = 1L;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.oper_his_id
     *
     * @return the value of t_oper_his.oper_his_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public Long getOperHisId() {
        return operHisId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.oper_his_id
     *
     * @param operHisId the value for t_oper_his.oper_his_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setOperHisId(Long operHisId) {
        this.operHisId = operHisId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.oper_user_id
     *
     * @return the value of t_oper_his.oper_user_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public Long getOperUserId() {
        return operUserId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.oper_user_id
     *
     * @param operUserId the value for t_oper_his.oper_user_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setOperUserId(Long operUserId) {
        this.operUserId = operUserId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.function_id
     *
     * @return the value of t_oper_his.function_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public Long getFunctionId() {
        return functionId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.function_id
     *
     * @param functionId the value for t_oper_his.function_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setFunctionId(Long functionId) {
        this.functionId = functionId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.oper_time
     *
     * @return the value of t_oper_his.oper_time
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public Date getOperTime() {
        return operTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.oper_time
     *
     * @param operTime the value for t_oper_his.oper_time
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setOperTime(Date operTime) {
        this.operTime = operTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.oper_content
     *
     * @return the value of t_oper_his.oper_content
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public String getOperContent() {
        return operContent;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.oper_content
     *
     * @param operContent the value for t_oper_his.oper_content
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setOperContent(String operContent) {
        this.operContent = operContent == null ? null : operContent.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.remark
     *
     * @return the value of t_oper_his.remark
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public String getRemark() {
        return remark;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.remark
     *
     * @param remark the value for t_oper_his.remark
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setRemark(String remark) {
        this.remark = remark == null ? null : remark.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_oper_his
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        TOperHis other = (TOperHis) that;
        return (this.getOperHisId() == null ? other.getOperHisId() == null : this.getOperHisId().equals(other.getOperHisId()))
            && (this.getOperUserId() == null ? other.getOperUserId() == null : this.getOperUserId().equals(other.getOperUserId()))
            && (this.getFunctionId() == null ? other.getFunctionId() == null : this.getFunctionId().equals(other.getFunctionId()))
            && (this.getOperTime() == null ? other.getOperTime() == null : this.getOperTime().equals(other.getOperTime()))
            && (this.getOperContent() == null ? other.getOperContent() == null : this.getOperContent().equals(other.getOperContent()))
            && (this.getRemark() == null ? other.getRemark() == null : this.getRemark().equals(other.getRemark()));
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_oper_his
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getOperHisId() == null) ? 0 : getOperHisId().hashCode());
        result = prime * result + ((getOperUserId() == null) ? 0 : getOperUserId().hashCode());
        result = prime * result + ((getFunctionId() == null) ? 0 : getFunctionId().hashCode());
        result = prime * result + ((getOperTime() == null) ? 0 : getOperTime().hashCode());
        result = prime * result + ((getOperContent() == null) ? 0 : getOperContent().hashCode());
        result = prime * result + ((getRemark() == null) ? 0 : getRemark().hashCode());
        return result;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_oper_his
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", operHisId=").append(operHisId);
        sb.append(", operUserId=").append(operUserId);
        sb.append(", functionId=").append(functionId);
        sb.append(", operTime=").append(operTime);
        sb.append(", operContent=").append(operContent);
        sb.append(", remark=").append(remark);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

读过前面文章的读者知道,mybatis插件生成的bean是没有Swagger注解的,这个是我们工具完成的工作。

第六步:

配置plugin.xml文件



	
		代码自动生成插件
		D:\\mvnrepository\\com\\lyh\\autoCodePluginImpl\\0.0.1-SNAPSHOT\\autoCodePluginImpl-0.0.1-SNAPSHOT.jar
		com.lyh.plugin.AutoCodePlugin
		
			D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\model
			D:\\bootspaceq\\autoCode\\target\\classes
			D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\rest
			D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\service
			D:\\bootspaceq\\autoCode\\target\\classes\\com\\zte\\model\\
			
		    jdbc:sqlserver://****:9143;DatabaseName=mobile
		    com.microsoft.sqlserver.jdbc.SQLServerDriver
		    111!23
		    111
		
	

其中 jar是你第一步生成的autoCodePluginImpl工程jar包的路径

    D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\model

     添加注释bean所在路径


            D:\\bootspaceq\\autoCode\\target\\classes

    编译文件路径,可随意填写,必须存在


            D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\rest

    生成rest接口包路径


            D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\service

    生成service包路径


            D:\\bootspaceq\\autoCode\\target\\classes\\com\\zte\\model\\
               编译完model类路径


            jdbc:sqlserver://****:9143;DatabaseName=mobile

     数据库地址


            com.microsoft.sqlserver.jdbc.SQLServerDriver

    数据库渠驱动
            111!23

    密码
            111

     用户名

 

  第七步:

  运行AutoCodePluginTest代码,刷新项目,如图:

  MyBatis-Plus结合Swagger实现接口代码及文档自动生成工具(实践篇)_第3张图片

我们发现原本空的rest目录和service目录下生成了两个文件。

依次打开bean、rest、service三个文件:

//lyhcheck=true
package com.zte.model;

import java.io.Serializable;
import java.util.Date;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiParam;
        
@ApiModel("toperhis")
public class TOperHis implements Serializable {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.oper_his_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @ApiModelProperty(value ="操作历史ID")
     @ApiParam(value ="操作历史ID")
    private Long operHisId;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.oper_user_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @ApiModelProperty(value ="操作用户ID")
     @ApiParam(value ="操作用户ID")
    private Long operUserId;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.function_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @ApiModelProperty(value ="功能ID")
     @ApiParam(value ="功能ID")
    private Long functionId;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.oper_time
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @ApiModelProperty(value ="操作时间")
     @ApiParam(value ="操作时间")
    private Date operTime;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.oper_content
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @ApiModelProperty(value ="操作内容")
     @ApiParam(value ="操作内容")
    private String operContent;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column t_oper_his.remark
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @ApiModelProperty(value ="备注")
     @ApiParam(value ="备注")
    private String remark;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table t_oper_his
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    private static final long serialVersionUID = 1L;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.oper_his_id
     *
     * @return the value of t_oper_his.oper_his_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public Long getOperHisId() {
        return operHisId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.oper_his_id
     *
     * @param operHisId the value for t_oper_his.oper_his_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setOperHisId(Long operHisId) {
        this.operHisId = operHisId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.oper_user_id
     *
     * @return the value of t_oper_his.oper_user_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public Long getOperUserId() {
        return operUserId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.oper_user_id
     *
     * @param operUserId the value for t_oper_his.oper_user_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setOperUserId(Long operUserId) {
        this.operUserId = operUserId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.function_id
     *
     * @return the value of t_oper_his.function_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public Long getFunctionId() {
        return functionId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.function_id
     *
     * @param functionId the value for t_oper_his.function_id
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setFunctionId(Long functionId) {
        this.functionId = functionId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.oper_time
     *
     * @return the value of t_oper_his.oper_time
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public Date getOperTime() {
        return operTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.oper_time
     *
     * @param operTime the value for t_oper_his.oper_time
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setOperTime(Date operTime) {
        this.operTime = operTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.oper_content
     *
     * @return the value of t_oper_his.oper_content
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public String getOperContent() {
        return operContent;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.oper_content
     *
     * @param operContent the value for t_oper_his.oper_content
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setOperContent(String operContent) {
        this.operContent = operContent == null ? null : operContent.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column t_oper_his.remark
     *
     * @return the value of t_oper_his.remark
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public String getRemark() {
        return remark;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column t_oper_his.remark
     *
     * @param remark the value for t_oper_his.remark
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    public void setRemark(String remark) {
        this.remark = remark == null ? null : remark.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_oper_his
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        TOperHis other = (TOperHis) that;
        return (this.getOperHisId() == null ? other.getOperHisId() == null : this.getOperHisId().equals(other.getOperHisId()))
            && (this.getOperUserId() == null ? other.getOperUserId() == null : this.getOperUserId().equals(other.getOperUserId()))
            && (this.getFunctionId() == null ? other.getFunctionId() == null : this.getFunctionId().equals(other.getFunctionId()))
            && (this.getOperTime() == null ? other.getOperTime() == null : this.getOperTime().equals(other.getOperTime()))
            && (this.getOperContent() == null ? other.getOperContent() == null : this.getOperContent().equals(other.getOperContent()))
            && (this.getRemark() == null ? other.getRemark() == null : this.getRemark().equals(other.getRemark()));
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_oper_his
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getOperHisId() == null) ? 0 : getOperHisId().hashCode());
        result = prime * result + ((getOperUserId() == null) ? 0 : getOperUserId().hashCode());
        result = prime * result + ((getFunctionId() == null) ? 0 : getFunctionId().hashCode());
        result = prime * result + ((getOperTime() == null) ? 0 : getOperTime().hashCode());
        result = prime * result + ((getOperContent() == null) ? 0 : getOperContent().hashCode());
        result = prime * result + ((getRemark() == null) ? 0 : getRemark().hashCode());
        return result;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_oper_his
     *
     * @mbg.generated Sun Sep 29 11:36:52 CST 2019
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", operHisId=").append(operHisId);
        sb.append(", operUserId=").append(operUserId);
        sb.append(", functionId=").append(functionId);
        sb.append(", operTime=").append(operTime);
        sb.append(", operContent=").append(operContent);
        sb.append(", remark=").append(remark);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

 

package com.zte.rest;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zte.service.TOperHisServiceImpl;
import com.zte.model.TOperHis;
import com.zte.common.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/tOperHis")
@Api(tags = "toperhis")
public class TOperHisRest {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    TOperHisServiceImpl tOperHisServiceImpl;

    @ApiOperation(value = "查询列表接口", notes = "")
    @ApiImplicitParams({
    @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页码", required = true, dataType = "Integer"),
    @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", required = true, dataType = "Integer"),
    @ApiImplicitParam(paramType ="query",name="operHisId",value="操作历史ID",required = false,dataType="java.lang.Long"),
    @ApiImplicitParam(paramType ="query",name="operUserId",value="操作用户ID",required = false,dataType="java.lang.Long"),
    @ApiImplicitParam(paramType ="query",name="functionId",value="功能ID",required = false,dataType="java.lang.Long"),
    @ApiImplicitParam(paramType ="query",name="operTime",value="操作时间",required = false,dataType="java.util.Date"),
    @ApiImplicitParam(paramType ="query",name="operContent",value="操作内容",required = false,dataType="java.lang.String"),
    @ApiImplicitParam(paramType ="query",name="remark",value="备注",required = false,dataType="java.lang.String"),
})
    @GetMapping(value = "list/{pageNum}/{pageSize}", produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResult list(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize,
            TOperHis item) {
       
        return JsonResult.getSuccess(tOperHisServiceImpl.selectList(pageNum, pageSize, item));
    }


    @ApiOperation(value = "查询单个接口", notes = "")
    @ApiImplicitParams({
    @ApiImplicitParam(paramType = "path", name = "id", value = "id", required = true, dataType = "Long") })
    @GetMapping(value = "item/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResult item(@PathVariable("id") long id) {
        return JsonResult.getSuccess(tOperHisServiceImpl.selectOne(id));
    }

    @ApiOperation(value = "修改接口", notes = "")
    @PostMapping(value = "update", produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResult update (@RequestBody TOperHis item) {
        return JsonResult.getSuccess(tOperHisServiceImpl.update(item));
    }

    @ApiOperation(value = "删除接口", notes = "")
    @ApiImplicitParams({
    @ApiImplicitParam(paramType = "path", name = "id", value = "id", required = true, dataType = "Long"), })
    @GetMapping(value = "delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResult delete(@PathVariable("id") long id) {
        return JsonResult.getSuccess(tOperHisServiceImpl.delete(id));
    }

    @ApiOperation(value = "新增接口", notes = "")
    @PostMapping(value = "insert", produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResult insert(@RequestBody TOperHis item) {
        return JsonResult.getSuccess(tOperHisServiceImpl.insert(item));
    }
    
}
package com.zte.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zte.mapper.TOperHisMapper;
import com.zte.model.TOperHis;
import com.zte.model.TOperHisExample;
import com.zte.param.PageInfo;

@Service
public class TOperHisServiceImpl {

	@Autowired
	private TOperHisMapper tOperHisMapper;

	/**
	 * 条件查询、满足所有条件组合查询
	 * @param pageNum 页码
	 * @param pageSize   分页大小
	 * @param item  查询条件对象
	 * @return 分页对象PageInfo
	 */
	public PageInfo selectList(int pageNum, int pageSize, TOperHis item) {
		TOperHisExample example = null;
		// 补充查询参数开始
		if (item != null) {
			example = new TOperHisExample();
			TOperHisExample.Criteria criteria = example.createCriteria();
			
        //操作历史ID
        if(item.getOperHisId()!=null){
           criteria.andOperHisIdEqualTo(item.getOperHisId());
        }
        //操作用户ID
        if(item.getOperUserId()!=null){
           criteria.andOperUserIdEqualTo(item.getOperUserId());
        }
        //功能ID
        if(item.getFunctionId()!=null){
           criteria.andFunctionIdEqualTo(item.getFunctionId());
        }
        //操作时间
        if(item.getOperTime()!=null){
           criteria.andOperTimeEqualTo(item.getOperTime());
        }
        //操作内容
        if(StringUtils.isNotEmpty(item.getOperContent())){
          criteria.andOperContentEqualTo("%"+item.getOperContent()+"%");
        }
        //备注
        if(StringUtils.isNotEmpty(item.getRemark())){
          criteria.andRemarkEqualTo("%"+item.getRemark()+"%");
        }
		}
		// 补充查询参数结束
		Page> page = PageHelper.startPage(pageNum, pageSize);
		List list = tOperHisMapper.selectByExample(example);
		PageInfo info = new PageInfo(page.getPageNum(), page.getPageSize(), page.getTotal(), page.getPages(), list);
		return info;
	}

	/**
	 * 通过主键查询
	 * @param id 主键id
	 * @return
	 */
	public TOperHis selectOne(Long id)

	{
		return  tOperHisMapper.selectByPrimaryKey(id);
	}

	/**
	 * 通过主键删除
	 * @param id 主键id
	 * @return
	 */
	public int delete(Long id) {
		return  tOperHisMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 更新对象
	 * @param item 
	 * @return
	 */
	public int update(TOperHis item)

	{
		return  tOperHisMapper.updateByPrimaryKeySelective(item);
	}

	/**
	 * 插入对象
	 * @param item
	 * @return
	 */
	public int insert(TOperHis item)

	{
		return  tOperHisMapper.insertSelective(item);
	}

}

 

1:bean文件自动添加了Swagger风格注释。

2:rest接口自动生成了增上改查、分页接口。并且生成了Swagger注释。

3:service类生成了相应的自动生成了增上改查、分页代码。

 

第八步:

启动工程,访问:http://localhost:8080/swagger-ui.html

MyBatis-Plus结合Swagger实现接口代码及文档自动生成工具(实践篇)_第4张图片

MyBatis-Plus结合Swagger实现接口代码及文档自动生成工具(实践篇)_第5张图片

自动生成Swagger接口文档成功。

至此一个完整的代码文档生成插件运行成功,有任何疑问请给我留言,我会第一时间回复大家。

你可能感兴趣的:(自动化代码)