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接口文档成功。

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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Mybatis-Plus代码生成器可以大大提高开发效率,能够快速生成基本的 Mapper、Entity、Service、Controller 等代码。如果想要在生成的 Controller 中集成 Swagger3,可以按照以下步骤进行操作: 1. 在 pom.xml 文件中添加 Swagger3 依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 2. 在代码生成器的配置文件 `generatorConfig.xml` 中添加 Swagger3 相关注释插件: ```xml <plugin type="io.github.swagger2markup.Swagger2MarkupConverterMojo"> <configuration> <swaggerInput>http://localhost:8080/v3/api-docs</swaggerInput> <outputFile>src/main/resources/docs/swagger</outputFile> <config> <swagger2MarkupConfig> <swaggerMarkupLanguage>MARKDOWN</swaggerMarkupLanguage> </swagger2MarkupConfig> </config> </configuration> </plugin> ``` 其中,`swaggerInput` 是指定 Swagger3 的 API 文档地址,`outputFile` 是指定生成文档存储路径。 3. 在生成的 Controller 类中添加 Swagger3 注解,如: ```java @RestController @RequestMapping("/user") @Api(tags = "用户管理") public class UserController { @Autowired private UserService userService; @ApiOperation(value = "根据用户ID获取用户信息") @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getById(id); } } ``` 其中,`@Api` 注解用于描述 Controller 的作用,`@ApiOperation` 注解用于描述 Controller 中的方法作用。其他注解可以根据实际需要进行添加。 4. 运行代码生成器,生成的 Controller 类中就会包含 Swagger3 相关注解。 以上是 Mybatis-Plus 代码生成器集成 Swagger3 的简单示例,具体实现方式可以根据项目需要进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值