代码生成器使用 mybatisplus 默认
也可以直接查看github 上的代码
项目地址
在项目中新建templates 文件夹
controller.java.ftl 模版
package ${package.Controller};
import com.dacheng.info.bim.business.base.common.JsonBean;
import com.dacheng.info.bim.business.base.common.ResultCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.*;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
/**
* <p>
* ${table.comment!} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
<#if restControllerStyle>
@Api(tags = {"${table.comment!}"})
@RestController
<#else>
@Controller
</#if>@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>class ${table.controllerName}<#if superControllerClass??>:${superControllerClass}()</#if><#else><#if superControllerClass??>public class ${table.controllerName} extends ${superControllerClass}{<#else>public class ${table.controllerName} {</#if>
private Logger log = LoggerFactory.getLogger(getClass());
@Resource
private ${table.serviceName} ${(table.serviceName?substring(1))?uncap_first};
@ApiOperation(value = "查询分页数据")
@ApiImplicitParams({
@ApiImplicitParam(name = "projectId", value = "项目id"),
@ApiImplicitParam(name = "page", value = "页码"),
@ApiImplicitParam(name = "pageCount", value = "每页条数")
})
@GetMapping("{projectId}")
public JsonBean findListByPage(@PathVariable Long projectId,
@RequestParam Integer page,
@RequestParam Integer pageCount){
try {
return ${(table.serviceName?substring(1))?uncap_first}.findListByPage(projectId, page, pageCount);
} catch (Exception e) {
log.error(e.getMessage(), e);
return JsonBean.returnResponse(false, ResultCode.SERVICE_ERR);
}
}
@ApiOperation(value = "新增数据")
@PostMapping()
public JsonBean add(@RequestBody ${entity} ${entity?uncap_first}){
try {
return ${(table.serviceName?substring(1))?uncap_first}.add(${entity?uncap_first});
} catch (Exception e) {
log.error(e.getMessage(), e);
return JsonBean.returnResponse(false, ResultCode.SERVICE_ERR);
}
}
@ApiOperation(value = "删除数据")
@DeleteMapping("{id}")
public JsonBean delete(@RequestParam("id") Long id){
try {
return ${(table.serviceName?substring(1))?uncap_first}.delete(id);
} catch (Exception e) {
log.error(e.getMessage(), e);
return JsonBean.returnResponse(false, ResultCode.SERVICE_ERR);
}
}
@ApiOperation(value = "更新数据")
@PutMapping()
public JsonBean update(@RequestBody ${entity} ${entity?uncap_first}){
try {
return ${(table.serviceName?substring(1))?uncap_first}.updateData(${entity?uncap_first});
} catch (Exception e) {
log.error(e.getMessage(), e);
return JsonBean.returnResponse(false, ResultCode.SERVICE_ERR);
}
}
}
</#if>
service.java.ftl 模版
package ${package.Service};
import ${package.Entity}.${entity};
import ${superServiceClassPackage};
import com.dacheng.info.bim.business.base.common.JsonBean;
/**
* <p>
* ${table.comment!} 服务类
* </p>
*
* @author ${author}
* @since ${date}
*/
<#if kotlin>
interface ${table.serviceName} : ${superServiceClass}<${entity}>
<#else>
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
/**
* 查询分页数据
*
* @param projectId 项目id
* @param page 页码
* @param pageCount 每页条数
* @return JsonBean
*/
JsonBean findListByPage(Long projectId, Integer page, Integer pageCount);
/**
* 添加
*
* @param ${entity?uncap_first} 信息
* @return JsonBean
*/
JsonBean add(${entity} ${entity?uncap_first});
/**
* 删除
*
* @param id 主键
* @return JsonBean
*/
JsonBean delete(Long id);
/**
* 修改信息
*
* @param ${entity?uncap_first} 信息
* @return JsonBean
*/
JsonBean updateData(${entity} ${entity?uncap_first});
}
</#if>
serviceImpl.java.ftl 模版
package ${package.ServiceImpl};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import com.dacheng.info.bim.business.base.common.JsonBean;
import com.dacheng.info.bim.business.base.common.ReturnPageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
/**
* <p>
* ${table.comment!} 服务实现类
* </p>
*
* @author ${author}
* @since ${date}
*/
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {
}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
@Override
public JsonBean findListByPage(Long projectId, Integer page, Integer pageCount){
IPage<${entity}> wherePage = new Page<>(page, pageCount);
${entity} where = new ${entity}();
IPage<${entity}> iPage = baseMapper.selectPage(wherePage, Wrappers.query(where));
return JsonBean.returnResponse(ReturnPageData.fillingData(iPage.getTotal(), iPage.getRecords()));
}
@Override
public JsonBean add(${entity} ${entity?uncap_first}){
baseMapper.insert(${entity?uncap_first});
return JsonBean.returnResponse();
}
@Override
public JsonBean delete(Long id){
baseMapper.deleteById(id);
return JsonBean.returnResponse();
}
@Override
public JsonBean updateData(${entity} ${entity?uncap_first}){
baseMapper.updateById(${entity?uncap_first});
return JsonBean.returnResponse();
}
}
</#if>
执行代码生成器
controller 代码
service 代码
实现类代码
swagger 页面
修改请求路径 可以通过swagger 操作单表了