- 编写
dao.ftl
数据访问模板
package ${daoPackageName}; import com.example.generator.core.BaseMapper; import java.util.List; import ${entityPackageName}.${entityName}; import ${dtoPackageName}.${dtoName}; /** * * @ClassName: ${daoName} * @Description: 数据访问接口 * @author ${authorName} * @date ${currentTime} * */ public interface ${daoName} extends BaseMapper<${entityName}>{ int countPage(${dtoName} ${dtoName?uncap_first}); List<${entityName}> selectPage(${dtoName} ${dtoName?uncap_first}); }
- 编写
service.ftl
服务接口模板
package ${servicePackageName}; import com.example.generator.core.BaseService; import com.example.generator.common.Pager; import ${voPackageName}.${voName}; import ${dtoPackageName}.${dtoName}; import ${entityPackageName}.${entityName}; /** * * @ClassName: ${serviceName} * @Description: ${entityName}业务访问接口 * @author ${authorName} * @date ${currentTime} * */ public interface ${serviceName} extends BaseService<${entityName}> { /** * 分页列表查询 * @param request */ Pager<${voName}> getPage(${dtoName} request); }
- 编写
serviceImpl.ftl
服务实现类模板
package ${serviceImplPackageName}; import com.example.generator.common.Pager; import com.example.generator.core.BaseServiceImpl; import com.example.generator.test.service.TestEntityService; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; import ${daoPackageName}.${daoName}; import ${entityPackageName}.${entityName}; import ${dtoPackageName}.${dtoName}; import ${voPackageName}.${voName}; @Service public class ${serviceImplName} extends BaseServiceImpl<${daoName}, ${entityName}> implements ${serviceName} { private static final Logger log = LoggerFactory.getLogger(${serviceImplName}.class); /** * 分页列表查询 * @param request */ public Pager<${voName}> getPage(${dtoName} request) { List<${voName}> resultList = new ArrayList(); int count = super.baseMapper.countPage(request); List<${entityName}> dbList = count > 0 ? super.baseMapper.selectPage(request) : new ArrayList<>(); if(!CollectionUtils.isEmpty(dbList)){ dbList.forEach(source->{ ${voName} target = new ${voName}(); BeanUtils.copyProperties(source, target); resultList.add(target); }); } return new Pager(request.getCurrPage(), request.getPageSize(), count, resultList); } }
- 编写
controller.ftl
控制层模板
package ${controllerPackageName}; import com.example.generator.common.IdRequest; import com.example.generator.common.Pager; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; 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 java.util.Objects; import ${servicePackageName}.${serviceName}; import ${entityPackageName}.${entityName}; import ${dtoPackageName}.${dtoName}; import ${voPackageName}.${voName}; /** * * @ClassName: ${controllerName} * @Description: 外部访问接口 * @author ${authorName} * @date ${currentTime} * */ @RestController @RequestMapping("/${entityName?uncap_first}") public class ${controllerName} { @Autowired private ${serviceName} ${serviceName?uncap_first}; /** * 分页列表查询 * @param request */ @PostMapping(value = "/getPage") public Pager<${voName}> getPage(@RequestBody ${dtoName} request){ return ${serviceName?uncap_first}.getPage(request); } /** * 查询详情 * @param request */ @PostMapping(value = "/getDetail") public ${voName} getDetail(@RequestBody IdRequest request){ ${entityName} source = ${serviceName?uncap_first}.selectById(request.getId()); if(Objects.nonNull(source)){ ${voName} result = new ${voName}(); BeanUtils.copyProperties(source, result); return result; } return null; } /** * 新增操作 * @param request */ @PostMapping(value = "/save") public void save(${dtoName} request){ ${entityName} entity = new ${entityName}(); BeanUtils.copyProperties(request, entity); ${serviceName?uncap_first}.insert(entity); } /** * 编辑操作 * @param request */ @PostMapping(value = "/edit") public void edit(${dtoName} request){ ${entityName} entity = new ${entityName}(); BeanUtils.copyProperties(request, entity); ${serviceName?uncap_first}.updateById(entity); } /** * 删除操作 * @param request */ @PostMapping(value = "/delete") public void delete(IdRequest request){ ${serviceName?uncap_first}.deleteById(request.getId()); } }
- 编写
entity.ftl
实体类模板
package ${entityPackageName}; import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; /** * * @ClassName: ${entityName} * @Description: ${tableDes!}实体类 * @author ${authorName} * @date ${currentTime} * */ public class ${entityName} implements Serializable { private static final long serialVersionUID = 1L; <#--属性遍历--> <#list columns as pro> <#--<#if pro.proName != primaryId && pro.proName != 'remarks' && pro.proName != 'createBy' && pro.proName != 'createDate' && pro.proName != 'updateBy' && pro.proName != 'updateDate' && pro.proName != 'delFlag' && pro.proName != 'currentUser' && pro.proName != 'page' && pro.proName != 'sqlMap' && pro.proName != 'isNewRecord' ></#if>--> /** * ${pro.proDes!} */ private ${pro.proType} ${pro.proName}; </#list> <#--属性get||set方法--> <#list columns as pro> public ${pro.proType} get${pro.proName?cap_first}() { return this.${pro.proName}; } public ${entityName} set${pro.proName?cap_first}(${pro.proType} ${pro.proName}) { this.${pro.proName} = ${pro.proName}; return this; } </#list> }
- 编写
dto.ftl
实体类模板
package ${dtoPackageName}; import com.example.generator.core.BaseDTO; import java.io.Serializable; /** * @ClassName: ${dtoName} * @Description: 请求实体类 * @author ${authorName} * @date ${currentTime} * */ public class ${dtoName} extends BaseDTO { }
- 编写
vo.ftl
视图实体类模板
package ${voPackageName}; import java.io.Serializable; /** * @ClassName: ${voName} * @Description: 返回视图实体类 * @author ${authorName} * @date ${currentTime} * */ public class ${voName} implements Serializable { private static final long serialVersionUID = 1L; }
可能细心的网友已经看到了,在模板中我们用到了BaseMapper
、BaseService
、BaseServiceImpl
等等服务类。
之所以有这三个类,是因为在模板中,我们有大量的相同的方法名包括逻辑也相似,除了所在实体类不一样以外,其他都一样,因此我们可以借助泛型类来将这些服务抽成公共的部分。