Springboot + Mybatis-Plus代码生成指南

使用 Spring Boot 和 MyBatis-Plus 生成代码,可以大大简化开发流程,可以保持编码的规范性,生成单元测试等。以下是详细步骤:

配置pom.xml

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version>
</dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.4.1</version>
</dependency><!-- velocity-->
<dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.0</version>
</dependency><!-- swagger-->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.8.0</version>
</dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.8.0</version>
</dependency><!-- 单元测试相关-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>

配置数据库连接

在 src/main/resources/application.properties 或 application.yml 中配置数据库连接信息:

server:port: 8000
spring:application:name: ocean-code-generationjackson:date-format: yyyy-MM-dd HH:mm:sstime-zone: GMT+8deserialization.accept_empty_string_as_null_object: truedatasource:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/db-ocean?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=trueusername: rootpassword: 123456mvc:pathmatch:matching-strategy: ANT_PATH_MATCHERmybatis-plus:mapper-locations: classpath*:mapper/*Mapper.xmlconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

定义代码生成模版

模版代码目录截图:

在这里插入图片描述

核心代码

  1. controller.java.vm
package ${package.Controller};import ${package.base_packages}.common.BaseController;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import ${package.base_packages}.common.ApiResult;
import ${package.base_packages}.common.PageResult;
import ${package.base_packages}.domain.dto.${entity}DTO;
import ${package.base_packages}.domain.entity.${entity};
import ${package.base_packages}.domain.qo.${entity}QO;
import ${package.base_packages}.domain.vo.${entity}VO;
import ${package.base_packages}.service.${entity}Service;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;/**1. $!{table.comment} 前端控制器2.  3. @author ${author}4. @time ${date}*/
@Api(value = "$!{table.comment}接口", tags = {"$!{table.comment}相关接口"})
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
public class ${table.controllerName} extends BaseController {@Resourceprivate ${entity}Service service;@ApiOperation(value = "$!{table.comment}列表", notes = "$!{table.comment}列表", consumes = "application/json", produces = "application/json", httpMethod = "GET")@ApiResponses({@ApiResponse(code = 200, message = "OK"),@ApiResponse(code = 50000, message = "service exception")})@GetMapping("/list")public PageResult<${entity}VO> page(${entity}QO qo) {return service.page(qo);}@ApiOperation(value = "获取$!{table.comment}信息", notes = "获取$!{table.comment}信息", consumes = "application/json", produces = "application/json", httpMethod = "GET")@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "$!{table.comment}ID", required = true)})@ApiResponses({@ApiResponse(code = 200, message = "OK", response = ${entity}.class),@ApiResponse(code = 50000, message = "service exception")})@GetMapping("/get/{id}")public ApiResult<${entity}> get(@PathVariable Long id) {${entity} entity = service.getById(id);if(null == entity) {return ApiResult.error();}return ApiResult.success(entity);}@ApiOperation(value = "保存$!{table.comment}", notes = "保存$!{table.comment}", consumes = "application/json", produces = "application/json", httpMethod = "POST")@ApiResponses({@ApiResponse(code = 200, message = "OK"),@ApiResponse(code = 50000, message = "service exception")})@PostMapping("/save")public ApiResult<Long> save(@RequestBody ${entity}DTO dto) {return service.save(dto);}@ApiOperation(value = "更新$!{table.comment}", notes = "更新$!{table.comment}", consumes = "application/json", produces = "application/json", httpMethod = "POST")@ApiResponses({@ApiResponse(code = 200, message = "OK"),@ApiResponse(code = 50000, message = "service exception")})@PostMapping("/update")public ApiResult<Long> update(@RequestBody ${entity}DTO dto) {return service.update(dto);}@ApiOperation(value = "删除$!{table.comment}", notes = "删除$!{table.comment}", consumes = "application/json", produces = "application/json", httpMethod = "DELETE")@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "$!{table.comment}ID", required = true)})@ApiResponses({@ApiResponse(code = 200, message = "OK"),@ApiResponse(code = 50000, message = "service exception")})@DeleteMapping("/delete/{id}")public ApiResult<Void> delete(@PathVariable Long id) {service.removeById(id);return ApiResult.success();}
}
  1. service.java.vm
package ${package.Service};import ${package.Entity}.${entity};
import ${superServiceClassPackage};
import ${package.qo_packages}.${entity}QO;
import ${package.vo_packages}.${entity}VO;
import ${package.dto_packages}.${entity}DTO;
import ${package.base_packages}.common.ApiResult;
import ${package.base_packages}.common.PageResult;/*** $!{table.comment} 服务类** @author ${author}* @time ${date}*/
#if(${kotlin})
interface ${table.serviceName} : ${superServiceClass}<${entity}>
#else
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {/*** @desc 分页查询** @param qo* @return ${package.base_packages}.common.PageResult<${package.vo_packages}.${entity}VO>*/PageResult<${entity}VO> page(${entity}QO qo);/*** @desc 保存** @param dto* @return ${package.base_packages}.common.ApiResult*/ApiResult<Long> save(${entity}DTO dto);/*** @desc 更新** @param dto* @return ${package.base_packages}.common.ApiResult*/ApiResult<Long> update(${entity}DTO dto);}
#end
  1. serviceImpl.java.vm
package ${package.ServiceImpl};import cn.hutool.core.bean.BeanUtil;
import ${package.base_packages}.common.ApiResult;
import ${package.base_packages}.common.PageResult;
import ${package.base_packages}.constant.ResultCode;
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import ${package.qo_packages}.${entity}QO;
import ${package.vo_packages}.${entity}VO;
import ${package.dto_packages}.${entity}DTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;/**1. $!{table.comment} 服务实现类2.  3. @author ${author}4. @time ${date}*/
@Service
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {@Resourceprivate ${entity}Mapper mapper;@Overridepublic PageResult<${entity}VO> page(${entity}QO qo) {List<${entity}VO> data = mapper.list${entity}(qo);int total = mapper.count${entity}(qo);PageResult<${entity}VO> pageResult = PageResult.success(data, total);pageResult.setPageSize(qo.getPageSize());pageResult.setPageNum(qo.getPageNum());int pages = total % qo.getPageSize() == 0 ? total / qo.getPageSize() : total / qo.getPageSize() + 1;pageResult.setPages(pages);return pageResult;}@Override@Transactionalpublic ApiResult<Long> save(${entity}DTO dto) {${entity} entity = new ${entity}();BeanUtil.copyProperties(dto, entity);mapper.insert(entity);return ApiResult.success(entity.getId());}@Override@Transactionalpublic ApiResult<Long> update(${entity}DTO dto) {if(null == dto.getId()) {return ApiResult.error(ResultCode.PARAM_ERROR);}${entity} entity = new ${entity}();BeanUtil.copyProperties(dto, entity);mapper.updateById(entity);return ApiResult.success(entity.getId());}
}
  1. mapper.java.vm
package ${package.Mapper};import ${package.Entity}.${entity};
import ${superMapperClassPackage};
import ${package.qo_packages}.${entity}QO;
import ${package.vo_packages}.${entity}VO;
import java.util.List;
import org.apache.ibatis.annotations.Param;/**1. $!{table.comment} Mapper2.  3. @author ${author}4. @time ${date}*/
#if(${kotlin})
interface ${table.mapperName} : ${superMapperClass}<${entity}>
#else
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {/*** @desc $!{table.comment}列表** @param qo* @return java.util.List<${package.vo_packages}.${entity}VO>*/List<${entity}VO> list${entity}(@Param("qo") ${entity}QO qo);/*** @desc $!{table.comment}条数** @param qo* @return int*/int count${entity}(@Param("qo") ${entity}QO qo);
}
#end
  1. mapper.xml.vm
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">#if(${enableCache})<!-- 开启二级缓存 --><cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>#end
#if(${baseResultMap})<!-- 通用查询映射结果 --><resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
#foreach($field in ${table.fields})
#if(${field.keyFlag})##生成主键排在第一位<id column="${field.name}" property="${field.propertyName}" />
#end
#end
#foreach($field in ${table.commonFields})##生成公共字段<result column="${field.name}" property="${field.propertyName}" />
#end
#foreach($field in ${table.fields})
#if(!${field.keyFlag})##生成普通字段<result column="${field.name}" property="${field.propertyName}" />
#end
#end</resultMap>#end
#if(${baseColumnList})<!-- 通用查询结果列 --><sql id="Base_Column_List">
#foreach($field in ${table.commonFields})${field.columnName},
#end${table.fieldNames}</sql><select id="list${entity}" resultType="${package.vo_packages}.${entity}VO">SELECT
#foreach($field in ${table.commonFields})${field.columnName},
#end${table.fieldNames}FROM ${table.name}ORDER BY id DESCLIMIT #{qo.limit},#{qo.offset}</select><select id="count${entity}" resultType="java.lang.Integer">SELECT count(*) FROM ${table.name}</select>#end
</mapper>
  1. entity.java.vm
package ${package.Entity};#if(${swagger2})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
#if(${entityLombokModel})
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
import java.io.Serializable;
#if(${chainModel})
import lombok.experimental.Accessors;
#end
#end/*** $!{table.comment}** @author ${author}* @time ${date}*/
#if(${entityLombokModel})
@Data#if(${superEntityClass})
@EqualsAndHashCode(callSuper = true)#else
@EqualsAndHashCode(callSuper = false)#end#if(${chainModel})
@Accessors(chain = true)#end
#end
#if(${swagger2})
@ApiModel(value="${entity}", description="$!{table.comment}")
#end
#if(${superEntityClass})
public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
#elseif(${activeRecord})
public class ${entity} extends Model<${entity}> {
#else
public class ${entity} implements Serializable {
#end#if(${entitySerialVersionUID})private static final long serialVersionUID = 1L;
#end
## ----------  BEGIN 字段循环遍历  ----------
#foreach($field in ${table.fields})#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")#if(${swagger2})@ApiModelProperty(value = "${field.comment}")#else/*** ${field.comment}*/#end
#end
#if(${field.keyFlag})
## 普通字段
#elseif(${field.fill})
## -----   存在字段填充设置   -----#if(${field.convert})@TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})#else@TableField(fill = FieldFill.${field.fill})#end
#elseif(${field.convert})@TableField("${field.annotationColumnName}")
#end
## 乐观锁注解
#if(${versionFieldName}==${field.name})@Version
#end
## 逻辑删除注解
#if(${logicDeleteFieldName}==${field.name})@TableLogic
#endprivate ${field.propertyType} ${field.propertyName};
#end
## ----------  END 字段循环遍历  ----------#if(!${entityLombokModel})
#foreach($field in ${table.fields})#if(${field.propertyType.equals("boolean")})#set($getprefix="is")#else#set($getprefix="get")#endpublic ${field.propertyType} ${getprefix}${field.capitalName}() {return ${field.propertyName};}#if(${chainModel})public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {#elsepublic void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {#endthis.${field.propertyName} = ${field.propertyName};#if(${chainModel})return this;#end}
#end
## --foreach end---
#end
## --end of #if(!${entityLombokModel})--#if(${entityColumnConstant})#foreach($field in ${table.fields})public static final String ${field.name.toUpperCase()} = "${field.name}";#end
#end
#if(${activeRecord})@Overrideprotected Serializable pkVal() {#if(${keyPropertyName})return this.${keyPropertyName};#elsereturn null;#end}#end
#if(!${entityLombokModel})@Overridepublic String toString() {return "${entity}{" +#foreach($field in ${table.fields})#if($!{foreach.index}==0)"${field.propertyName}=" + ${field.propertyName} +#else", ${field.propertyName}=" + ${field.propertyName} +#end#end"}";}
#end
}
  1. dto.java.vm
package ${package.dto_packages};import java.util.Date;
#if(${swagger2})
import io.swagger.annotations.ApiModelProperty;
#end
import lombok.Data;
import java.io.Serializable;/*** $!{table.comment}** @author ${author}* @time ${date}*/
@Data
public class ${entity}DTO implements Serializable {private static final long serialVersionUID = 1L;## ----------  BEGIN 字段循环遍历  ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")#if(${swagger2})@ApiModelProperty(value = "${field.comment}")#else/*** ${field.comment}*/#end
#end
#if(${field.fill})## -----   存在字段填充设置   -----#elseif(${field.convert})
@TableField("${field.annotationColumnName}")
#end
## 乐观锁注解
#if(${versionFieldName}==${field.name})
@Version
#end
## 逻辑删除注解
#if(${logicDeleteFieldName}==${field.name})
@TableLogic
#endprivate ${field.propertyType} ${field.propertyName};#end
## ----------  END 字段循环遍历  ----------}
  1. qo.java.vm
package ${package.qo_packages};import java.util.Date;
import ${package.base_packages}.common.PageDomain;
#if(${swagger2})
import io.swagger.annotations.ApiModelProperty;
#end
import lombok.Data;
import java.io.Serializable;/*** $!{table.comment}** @author ${author}* @time ${date}*/
@Data
public class ${entity}QO extends PageDomain implements Serializable {private static final long serialVersionUID = 1L;## ----------  BEGIN 字段循环遍历  ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")#if(${swagger2})@ApiModelProperty(value = "${field.comment}")#else/*** ${field.comment}*/#end
#end
#if(${field.fill})## -----   存在字段填充设置   -----#elseif(${field.convert})
@TableField("${field.annotationColumnName}")
#end
## 乐观锁注解
#if(${versionFieldName}==${field.name})
@Version
#end
## 逻辑删除注解
#if(${logicDeleteFieldName}==${field.name})
@TableLogic
#endprivate ${field.propertyType} ${field.propertyName};#end
## ----------  END 字段循环遍历  ----------}
  1. vo.java.vm
package ${package.vo_packages};import java.util.Date;
#if(${swagger2})
import io.swagger.annotations.ApiModelProperty;
#end
import lombok.Data;
import java.io.Serializable;/*** $!{table.comment}** @author ${author}* @time ${date}*/
@Data
public class ${entity}VO implements Serializable {private static final long serialVersionUID = 1L;## ----------  BEGIN 字段循环遍历  ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")#if(${swagger2})@ApiModelProperty(value = "${field.comment}")#else/*** ${field.comment}*/#end
#end
#if(${field.fill})## -----   存在字段填充设置   -----#elseif(${field.convert})
@TableField("${field.annotationColumnName}")
#end
## 乐观锁注解
#if(${versionFieldName}==${field.name})
@Version
#end
## 逻辑删除注解
#if(${logicDeleteFieldName}==${field.name})
@TableLogic
#endprivate ${field.propertyType} ${field.propertyName};#end
## ----------  END 字段循环遍历  ----------}

单元测试代码

  1. controller.java.vm
package ${package.Controller};import ${package.base_packages}.common.ApiResult;
import ${package.base_packages}.common.PageResult;
import ${package.base_packages}.domain.dto.${entity}DTO;
import ${package.base_packages}.domain.entity.${entity};
import ${package.base_packages}.domain.qo.${entity}QO;
import ${package.base_packages}.domain.vo.${entity}VO;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import com.alibaba.fastjson2.JSON;@Slf4j
@SpringBootTest
class ${entity}ControllerTest {@Resourceprivate ${entity}Controller controller;@Testvoid page() {${entity}QO qo = new ${entity}QO();PageResult<${entity}VO> result = controller.page(qo);log.info("{}", JSON.toJSONString(result));}@Testvoid get() {Long id = 0L;ApiResult<${entity}> result = controller.get(id);log.info("{}", JSON.toJSONString(result));}@Testvoid save() {${entity}DTO dto = new ${entity}DTO();ApiResult<?> result = controller.save(dto);log.info("{}", JSON.toJSONString(result));}@Testvoid update() {${entity}DTO dto = new ${entity}DTO();ApiResult<?> result = controller.update(dto);log.info("{}", JSON.toJSONString(result));}@Testvoid delete() {Long id = 0L;ApiResult<?> result = controller.delete(id);log.info("{}", JSON.toJSONString(result));}
}
  1. service.java.vm
package ${package.Service};import ${package.qo_packages}.${entity}QO;
import ${package.vo_packages}.${entity}VO;
import ${package.dto_packages}.${entity}DTO;
import ${package.base_packages}.common.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import com.alibaba.fastjson2.JSON;
import javax.annotation.Resource;@Slf4j
@SpringBootTest
class ${entity}ServiceTest {@Resourceprivate ${entity}Service service;@Testvoid page() {${entity}QO qo = new ${entity}QO();PageResult<${entity}VO> result = service.page(qo);log.info("{}", JSON.toJSONString(result));}@Testvoid save() {${entity}DTO dto = new ${entity}DTO();service.save(dto);}@Testvoid update() {${entity}DTO dto = new ${entity}DTO();service.update(dto);}
}
  1. mapper.java.vm
package ${package.Mapper};import ${package.qo_packages}.${entity}QO;
import ${package.vo_packages}.${entity}VO;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import com.alibaba.fastjson2.JSON;
import javax.annotation.Resource;@Slf4j
@SpringBootTest
class ${entity}MapperTest {@Resourceprivate ${entity}Mapper mapper;@Testvoid list${entity}() {${entity}QO qo = new ${entity}QO();List<${entity}VO> result = mapper.list${entity}(qo);log.info("{}", JSON.toJSONString(result));}@Testvoid count${entity}() {${entity}QO qo = new ${entity}QO();int result = mapper.count${entity}(qo);log.info("{}", result);}
}

其他使用到的类

package com.angel.ocean.common;public class BaseController {}
package com.angel.ocean.common;import com.angel.ocean.constant.ResultCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;@Data
public class ApiResult<T> {@ApiModelProperty(value = "响应码")private Integer code;@ApiModelProperty(value = "提示信息")private String msg;@ApiModelProperty(value = "数据集合")private T data;public ApiResult(Integer code, String msg, T data) {this.code = code;this.msg = msg;this.data = data;}public static <T> ApiResult<T> success(T data) {return new ApiResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);}public static <T> ApiResult<T> success() {return new ApiResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);}public static <T> ApiResult<T> error(int code, String msg) {return new ApiResult(code, msg, null);}public static <T> ApiResult<T> error() {return new ApiResult(ResultCode.SERVICE_INTERNAL_ERROR.getCode(), ResultCode.SERVICE_INTERNAL_ERROR.getMsg(), null);}public static <T> ApiResult<T> error(ResultCode messageCode) {return new ApiResult(messageCode.getCode(), messageCode.getMsg(), null);}
}
package com.angel.ocean.common;import com.angel.ocean.constant.ResultCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;@Data
public class PageResult<T> implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty(value = "总条数")private Integer total;@ApiModelProperty(value = "数据集合")private List<T> rows;@ApiModelProperty(value = "响应码")private Integer code;@ApiModelProperty(value = "提示信息")private String msg;@ApiModelProperty(value = "页码,1-首页")private Integer pageNum;@ApiModelProperty(value = "每页数据大小")private Integer pageSize;@ApiModelProperty(value = "总页数")private Integer pages;public PageResult() {}public PageResult(List<T> list, int total) {this.rows = list;this.total = total;}public PageResult(int code, String msg) {this.code = code;this.msg = msg;}public PageResult(Integer total, List<T> rows, int code, String msg) {this.total = total;this.rows = rows;this.code = code;this.msg = msg;}public PageResult(Integer total, List<T> rows, int code, String msg, int pageNum, int pageSize) {this.total = total;this.rows = rows;this.code = code;this.msg = msg;this.pageNum = pageNum;this.pageSize = pageSize;this.pages = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;}public static <T> PageResult<T> success(List<T> list, Integer total) {return new PageResult(total, list, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());}public static <T> PageResult<T> success(List<T> list, Integer total, Integer pageNum, Integer pageSize) {return new PageResult(total, list, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), pageNum, pageSize);}public static <T> PageResult<T> error(ResultCode resultCode) {return new PageResult(resultCode.getCode(), resultCode.getMsg());}public static <T> PageResult<T> error() {return new PageResult(ResultCode.SERVICE_INTERNAL_ERROR.getCode(), ResultCode.SERVICE_INTERNAL_ERROR.getMsg());}
}
package com.angel.ocean.common;import io.swagger.annotations.ApiModelProperty;public class PageDomain {/**** 页码*/@ApiModelProperty(value = "页码,1-首页")private Integer pageNum;/**** 页面条数*/@ApiModelProperty(value = "每页数据大小")private Integer pageSize;@ApiModelProperty(hidden = true)private Integer limit;@ApiModelProperty(hidden = true)private Integer offset;public int getLimit() {limit = (getPageNum() - 1) * getPageSize();return limit;}public int getOffset() {offset = getPageSize();return offset;}public Integer getPageNum() {if(null == pageNum || pageNum < 1) {return 1;}return pageNum;}public void setPageNum(Integer pageNum) {this.pageNum = pageNum;}public Integer getPageSize() {if(null == pageSize || pageSize < 1) {pageSize = 10;}return pageSize;}public void setPageSize(Integer pageSize) {this.pageSize = pageSize;}
}

定义代码生成执行方法

package com.angel.ocean;import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;@SpringBootTest
class ApplicationTests {@Testvoid generate() {// 1、创建代码生成器AutoGenerator mpg = new AutoGenerator();// 表名String tableName = "sys_role";// 表前缀String tablePrefix = "";// 包名String packages = "com.angel.ocean";// 2、全局配置GlobalConfig gc = new GlobalConfig();String projectPath = System.getProperty("user.dir");gc.setOutputDir(projectPath + "/src/main/java");gc.setAuthor("Jaime.yu");gc.setOpen(false);  //生成后是否打开资源管理器gc.setFileOverride(false);  //重新生成时文件是否覆盖gc.setServiceName("%sService"); //去掉Service接口的首字母Igc.setIdType(IdType.AUTO);  //主键策略gc.setDateType(DateType.ONLY_DATE); //定义生成的实体类中日期类型gc.setSwagger2(true);   //开启Swagger2模式gc.setBaseResultMap(true);  //启用通用查询映射结果gc.setBaseColumnList(true);  //启用通用查询结果列mpg.setGlobalConfig(gc);// 3、数据源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://localhost:3306/db-ocean?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false");dsc.setDriverName("com.mysql.cj.jdbc.Driver");dsc.setUsername("root");dsc.setPassword("123456");dsc.setDbType(DbType.MYSQL);mpg.setDataSource(dsc);// 4、包配置PackageConfig pc = new PackageConfig();pc.setModuleName(null); //模块名pc.setParent(packages);pc.setController("controller");pc.setEntity("domain.entity");pc.setService("service");pc.setMapper("mapper");mpg.setPackageInfo(pc);// 配置模板引擎TemplateConfig templateConfig = new TemplateConfig();templateConfig.setXml(null);mpg.setTemplate(templateConfig);// 注入自定义配置,可以在 VM 中使用 cfg.injectionConfig 设置的值InjectionConfig injectionConfig = new InjectionConfig() {@Overridepublic void initMap() {this.getConfig().getPackageInfo().put("base_packages", packages);this.getConfig().getPackageInfo().put("entity_packages", packages + StringPool.DOT + "domain" + StringPool.DOT + "entity");this.getConfig().getPackageInfo().put("dto_packages", packages + StringPool.DOT + "domain" + StringPool.DOT + "dto");this.getConfig().getPackageInfo().put("vo_packages", packages + StringPool.DOT + "domain" + StringPool.DOT + "vo");this.getConfig().getPackageInfo().put("qo_packages", packages + StringPool.DOT + "domain" + StringPool.DOT + "qo");}};//自定义文件输出位置(非必须)List<FileOutConfig> fileOutList = new ArrayList<FileOutConfig>();fileOutList.add(new FileOutConfig("/templates/qo.java.vm") {@Overridepublic String outputFile(TableInfo tableInfo) {return projectPath + getPath(packages) + "/domain/qo"+ "/" + tableInfo.getEntityName() + "QO" + StringPool.DOT_JAVA;}});fileOutList.add(new FileOutConfig("/templates/dto.java.vm") {@Overridepublic String outputFile(TableInfo tableInfo) {return projectPath + getPath(packages) + "/domain/dto"+ "/" + tableInfo.getEntityName() + "DTO" + StringPool.DOT_JAVA;}});fileOutList.add(new FileOutConfig("/templates/vo.java.vm") {@Overridepublic String outputFile(TableInfo tableInfo) {return projectPath + getPath(packages) + "/domain/vo"+ "/" + tableInfo.getEntityName() + "VO" +StringPool.DOT_JAVA;}});fileOutList.add(new FileOutConfig("/templates/mapper.xml.vm") {@Overridepublic String outputFile(TableInfo tableInfo) {return "src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}});fileOutList.add(new FileOutConfig("/templates/test/mapper.java.vm") {@Overridepublic String outputFile(TableInfo tableInfo) {return projectPath + getTestPath(packages) + "/mapper"+ "/" + tableInfo.getEntityName() + "MapperTest" + StringPool.DOT_JAVA;}});fileOutList.add(new FileOutConfig("/templates/test/service.java.vm") {@Overridepublic String outputFile(TableInfo tableInfo) {return projectPath + getTestPath(packages) + "/service"+ "/" + tableInfo.getEntityName() + "ServiceTest" + StringPool.DOT_JAVA;}});fileOutList.add(new FileOutConfig("/templates/test/controller.java.vm") {@Overridepublic String outputFile(TableInfo tableInfo) {return projectPath + getTestPath(packages) + "/controller"+ "/" + tableInfo.getEntityName() + "ControllerTest" + StringPool.DOT_JAVA;}});injectionConfig.setFileOutConfigList(fileOutList);mpg.setCfg(injectionConfig);// 5、策略配置StrategyConfig strategy = new StrategyConfig();strategy.setInclude(tableName);//对那一张表生成代码strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略strategy.setTablePrefix(tablePrefix); //生成实体时去掉表前缀strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作strategy.setRestControllerStyle(true); //restful api风格控制器strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符mpg.setStrategy(strategy);// 6、执行mpg.execute();}private String getPath(String packages) {String path = "";if(StrUtil.isNotEmpty(packages)) {path = "/src/main/java/" + packages.replace(".", "/") + "/";}return path;}private String getTestPath(String packages) {String path = "";if(StrUtil.isNotEmpty(packages)) {path = "/src/test/java/" + packages.replace(".", "/") + "/";}return path;}}

代码生成截图

红色框中为生成的代码,包含controller/service/mapper/entity/dto/qo/vo,以及单元测试代码
在这里插入图片描述

启动服务

在这里插入图片描述
Swagger配置:

package com.angel.ocean.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;/*** swagger配置文件*/
@Slf4j
@Configuration
@EnableSwagger2
public class Swagger2Config {@Beanpublic Docket createRestApi() {log.info("进入到swagger的配置中,swagger地址:http://<host>:<port>/swagger-ui.html");return new Docket(DocumentationType.SWAGGER_2)// 指定构建api文档的详细信息的方法:apiInfo().apiInfo(apiInfo()).select()// 指定要生成api接口的包路径,这里把controller作为包路径,生成controller中的所有接口.apis(RequestHandlerSelectors.basePackage("com.angel.ocean.controller")).paths(PathSelectors.any()).build();}/*** 构建api文档的详细信息* @return*/private ApiInfo apiInfo() {return new ApiInfoBuilder()// 设置页面标题.title("Spring Boot集成Swagger2接口总览")// 设置接口描述.description("Swagger接口学习")// 设置联系方式.contact(new Contact("测试swagger","http://localhost:8000/","861565437@qq.com"))// 设置版本.version("1.0")// 构建.build();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/861682.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何利用静力水准仪进行地形沉降测量

地形沉降测量在建筑工程和地质研究中起着至关重要的作用。准确的地形沉降测量可以帮助工程师预测和预防潜在的地基问题&#xff0c;从而保障建筑物的安全和稳定。本文将详细介绍如何利用静力水准仪进行地形沉降测量&#xff0c;并探讨其在实际应用中的优势。 静力水准仪的基本原…

关于数字化营销中做好拓新裂变活动的策划探讨

一、引言 在当今数字化时代&#xff0c;企业面临着日益激烈的市场竞争和不断变化的消费者需求。数字化营销作为一种高效的营销方式&#xff0c;能够以较低的成本触达更广泛的目标受众。而拓新裂变活动则是数字化营销中的关键环节&#xff0c;对于企业快速扩大用户群体、提升品…

购物商城系统

摘要 随着互联网的快速发展&#xff0c;网上购物已经成为人们日常生活中不可或缺的一部分。越来越多的消费者选择在网上购物&#xff0c;享受随时随地的便利和丰富多样的商品选择。然而&#xff0c;随着网上购物用户数量的不断增加&#xff0c;传统的线下商店已经无法满足用户…

国家地表水水质自动监测数据(整理版)

国家地表水水质自动检测实时数据发布系统&#xff0c;发布的数据。含省份、城市、河流、流域、断面名称、监测时间、水温、pH、DO、CODMn、TP、TN、NH3-N、浊度等。 数据介绍&#xff1a; 2014年4月-2020年11月每月60-140个左右的站点有数据&#xff0c;从2020年11月开始&#…

MHA、MMM高可用方案及故障切换

目录 一、MHA高可用方案 1、MHA的组成 2、MHA的工作原理 3、部署MHA架构 第一部分&#xff1a;一主两从数据库架构部署 1、全部更改主机名、初始化操作、开启mysql服务、设置主机名管理、时间同步 2、MySQL服务器做主从复制 3、测试主从效果 第二部分&#xff1a;MHA架…

Python25 Numpy基础

1.什么是Numpy NumPy&#xff08;Numerical Python 的简称&#xff09;是 Python 语言的一个扩展程序库&#xff0c;支持大量的维度数组与矩阵运算&#xff0c;此外也针对数组运算提供大量的数学函数库。NumPy 的前身是 Numeric&#xff0c;这是一个由 Jim Hugunin 等人开发的…

SAP ALV 负号提前

FUNCTION CONVERSION_EXIT_ZSIGN_OUTPUT. *"---------------------------------------------------------------------- *"*"本地接口&#xff1a; *" IMPORTING *" REFERENCE(INPUT) *" EXPORTING *" REFERENCE(OUTPUT) *"…

PNAS|这样也可以?拿别人数据发自己Paper?速围观!

还在为数据量小&#xff0c;说服力不足发愁&#xff1f; 想研究脱颖而出、眼前一亮&#xff1f; 想从更高层次的探索微生物的奥秘&#xff0c;发出一篇好文章&#xff1f; 近期&#xff0c;有一篇发表在PNAS(IF11.1)的文章“Deforestation impacts soil biodiversity and ecos…

量子计算与AI融合:IBM引领未来计算新纪元

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

docker-本地部署-后端

前置条件 后端文件 这边是一个简单项目的后端文件目录 docker服务 镜像文件打包 #命令行 docker build -t author/chatgpt-ai-app:1.0 -f ./Dockerfile .红框是docker所在文件夹 author&#xff1a;docker用户名chatgpt-ai-app&#xff1a;打包的镜像文件名字:1.0 &#…

YOLOv10改进 | 卷积模块 | 将Conv替换为轻量化的GSConv【轻量又涨点】

秋招面试专栏推荐 &#xff1a;深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转 &#x1f4a1;&#x1f4a1;&#x1f4a1;本专栏所有程序均经过测试&#xff0c;可成功执行&#x1f4a1;&#x1f4a1;&#x1f4a1; 专栏目录&#xff1a;《YOLOv8改进有效…

技术流 | 运维平台大型“生产事故”录播和实战重现

【本文作者&#xff1a;擎创科技 资深专家禹鼎侯】 本文写于2021年&#xff0c;最近重读觉得特别有现场感。这也是运维人面对生产环境时遇到的各种惊心动魄的事件之一。惊险&#xff0c;但又顺利解决。是最好的结果。 事情是酱紫的。 那天上午&#xff0c;轻轻松松完成了一个新…

昇思MindSpore基本介绍

昇思MindSpore是一个全场景深度学习框架&#xff0c;旨在实现易开发、高效执行、全场景统一部署三大目标。 其中&#xff0c;易开发表现为API友好、调试难度低&#xff1b;高效执行包括计算效率、数据预处理效率和分布式训练效率&#xff1b;全场景则指框架同时支持云、边缘以…

C语言之进程学习

进程打开的文件列表&#xff1a;就是0 1 2 stdin stdout stderro等 类似于任务管理器是动态分ps是静态的 Zombie状态&#xff1a; 在Linux进程的状态中&#xff0c;僵尸进程是非常特殊的一种&#xff0c;它是已经结束了的进程&#xff0c;但是没有从进程表中删除。太多了会导…

轻量级仿 SpringBoot 程序

但凡 Java 程序&#xff0c;想必就是 Spring 程序&#xff1b;但凡 Spring 程序&#xff0c;想必就是 SpringBoot 程序——且慢&#xff0c;当今尚有不是 SpringBoot 即 SpringMVC 的程序不&#xff1f;有——老旧的遗留系统不就是嘛~——不&#xff0c;其实只要稍加“调教”&a…

TikTok网页版使用指南:如何登录TikTok网页版?

海外版抖音TikTok&#xff0c;已成为连接全球观众的重要平台。据统计&#xff0c;在美国&#xff0c;TikTok的用户数量已达到近1.3亿&#xff0c;并且在国外的95后用户群体中很受欢迎。 TikTok网页版也提供了一个广阔的平台&#xff0c;让品牌和创作者在电脑端与全球观众互动&…

智能语音抽油烟机:置入WTK6900L离线语音识别芯片 掌控厨房新风尚

一、抽油烟机语音识别芯片开发背景 在繁忙的现代生活中&#xff0c;人们对于家居生活的便捷性和舒适性要求越来越高。传统的抽油烟机操作方式往往需要用户手动调节风速、开关等功能&#xff0c;不仅操作繁琐&#xff0c;而且在烹饪过程中容易分散注意力&#xff0c;增加安全隐…

大数据处理引擎选型之 Hadoop vs Spark vs Flink

随着大数据时代的到来&#xff0c;处理海量数据成为了各个领域的关键挑战之一。为了应对这一挑战&#xff0c;多个大数据处理框架被开发出来&#xff0c;其中最知名的包括Hadoop、Spark和Flink。本文将对这三个大数据处理框架进行比较&#xff0c;以及在不同场景下的选择考虑。…

ubuntu篇---添加环境变量并且在pycharm中使用

ubuntu篇—添加环境变量并且在pycharm中使用 一. 添加环境变量 vim ~/.bashrc 在文件末尾加上 保存退出 source ~/.bashrc二. 在pycharm中添加环境变量 1.打开pycharm&#xff0c;并打开你的项目 2.点击菜单栏中的“Run”&#xff0c; 选择“Edit Configurations” 3.在弹…

pytorch为自己的extension backend添加profiler功能

pytorch为自己的extension backend添加profiler功能 1.参考文档2.your-extension-for-pytorch需要增加的代码3.pytorch demo及如何调整chrome trace json文件4.[可视化](https://ui.perfetto.dev/) 本文演示了pytorch如何为自己的extension backend添加profiler功能 背景介绍 …