MyBatis-Plus結合Swagger實現接口代碼及文檔自動生成工具(實踐篇)

                                    MyBatis-Plus結合Swagger實現接口代碼及文檔自動生成工具(實踐篇)

         通過前面三篇文章的介紹自動化生成代碼工具我們理論上是已經實現了,可是到底行不行好不好讀者心裏還是個大大的疑問?

這篇文章我就來一步步帶大家使用工具快速構建自己的項目。

         第一步:

        訪問我的GitHub主頁下載以下三個工程的源碼。

插件定義源碼

插件實現原源碼

插件測試源碼

 

 第二步:

將autoCodePlugin工程和autoCodePluginImpl工程打成jar包,並上傳到你本地的倉庫,因爲測試的時候需要引入jar包。

 

 第三步:

 導入autoCode項目到你的開發工具,我用的是eclipse,如圖:

 

第四步:

文件中配置好你真實的數據庫鏈接,然後執行該插件。這篇文章僅僅做爲講解,因此我只生成一個數據表。得到結構如下:

可以看到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文件

<?xml version="1.0" encoding="UTF-8"?>
<plugins>
	<plugin>
		<name>代碼自動生成插件</name>
		<jar>D:\\mvnrepository\\com\\lyh\\autoCodePluginImpl\\0.0.1-SNAPSHOT\\autoCodePluginImpl-0.0.1-SNAPSHOT.jar</jar>
		<class>com.lyh.plugin.AutoCodePlugin</class>
		<properties>
			<property name="beanPath">D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\model</property>
			<property name="targetPath">D:\\bootspaceq\\autoCode\\target\\classes</property>
			<property name="restPath">D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\rest</property>
			<property name="servicePath">D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\service</property>
			<property name="classPath">D:\\bootspaceq\\autoCode\\target\\classes\\com\\zte\\model\\</property>
			
		    <property name="connectionURL">jdbc:sqlserver://****:9143;DatabaseName=mobile</property>
		    <property name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
		    <property name="password">111!23</property>
		    <property name="userId">111</property>
		</properties>
	</plugin>
</plugins>

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

    <property name="beanPath">D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\model</property>

     添加註釋bean所在路徑


            <property name="targetPath">D:\\bootspaceq\\autoCode\\target\\classes</property>

    編譯文件路徑,可隨意填寫,必須存在


            <property name="restPath">D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\rest</property>

    生成rest接口包路徑


            <property name="servicePath">D:\\bootspaceq\\autoCode\\src\\main\\java\\com\\zte\\service</property>

    生成service包路徑


            <property name="classPath">D:\\bootspaceq\\autoCode\\target\\classes\\com\\zte\\model\\</property>
               編譯完model類路徑


            <property name="connectionURL">jdbc:sqlserver://****:9143;DatabaseName=mobile</property>

     數據庫地址


            <property name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>

    數據庫渠驅動
            <property name="password">111!23</property>

    密碼
            <property name="userId">111</property>

     用戶名

 

  第七步:

  運行AutoCodePluginTest代碼,刷新項目,如圖:

  

我們發現原本空的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<List<TOperHis>> page = PageHelper.startPage(pageNum, pageSize);
		List<TOperHis> 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

自動生成Swagger接口文檔成功。

至此一個完整的代碼文檔生成插件運行成功,有任何疑問請給我留言,我會第一時間回覆大家。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章