[CRUD is often written in the project, but in practice, I think if there is a complete code specification, it can be generated automatically and speed up the development efficiency
The technical principle of code generator is not complicated. Generally, a template is written to generate a series of codes. I saw that the code generator of mybatis? Plus was quite good, so I took it and modified it myself
1. In the project, vm library should be introduced to generate code
<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>${velocity.version}</version> </dependency>
2. The model code is as follows
/** * <p> * Examination list * </p> * * @author starmark * @since 2018-04-12 */ @Data @EqualsAndHashCode(callSuper=false) public class Exam implements Serializable { /** * Primary key */ @TableId("id") private Long id; /** * subject */ @TableField("subject") private String subject; /** * Answer */ @TableField("answer") private String answer; /** * category */ @TableField("category") private String category; @TableField("key_point") private String keyPoint; @TableField("created_by") private String createdBy; @TableField("created_date") private Date createdDate; @TableField("last_updated_by") private String lastUpdatedBy; @TableField("last_updated_date") private Date lastUpdatedDate; }
The vm is transformed as follows:
package ${package.Entity}; #if(${activeRecord}) import lombok.Data; import lombok.EqualsAndHashCode; #end #foreach($pkg in ${table.importPackages}) import ${pkg}; #end /** * <p> * ${table.comment} * </p> * * @author ${author} * @since ${date} */ ${data} ${EqualsAndHashCode} #if(${table.convert}) @TableName("${table.name}") #end #if(${superEntityClass}) public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end { #elseif(${activeRecord}) public class ${entity} implements Serializable { #else public class ${entity} implements Serializable { #end #foreach($field in ${table.fields}) #if(${field.keyFlag}) #set($keyPropertyName=${field.propertyName}) #end #if("$!field.comment" != "") /** * ${field.comment} */ #end #if(${field.convert}) #if(${field.keyFlag}) @TableId("${field.name}") #else @TableField("${field.name}") #end #end private ${field.propertyType} ${field.propertyName}; #end }
2. The serviceimpl implementation class is as follows:
import com.starmark.exam.entity.Exam; import com.starmark.exam.mapper.ExamMapper; import com.starmark.exam.service.IExamService; import com.starmark.core.base.AbstractBaseService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; /** * <p> * Test question table service implementation class * </p> * * @author starmark * @since 2018-04-12 */ @Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public class ExamServiceImpl extends AbstractBaseService<ExamMapper, Exam> implements IExamService { }
VM is written as follows:
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 org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; /** * <p> * ${table.comment} Service implementation class * </p> * * @author ${author} * @since ${date} */ @Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} { }
3. The controller implementation class is as follows:
import io.swagger.annotations.ApiOperation; import com.starmark.common.base.PageVo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.starmark.core.web.base.AbstractBaseController; /** * <p> * Front end controller of examination list * </p> * * @author starmark * @since 2018-04-12 */ @RestController @RequestMapping("/exam/exam") public class ExamController extends AbstractBaseController<IExamService> { @ApiOperation(value = "Query the list of examination questions") @PutMapping(value = "/page") public Object query(@RequestBody PageVo pageVo) { return super.queryPage(pageVo); } @ApiOperation(value = "New test questions") @PostMapping public Object add(@RequestBody SysOrgDept param) { return super.add(param); } @ApiOperation(value = "Details of examination questions") @GetMapping(value = "/{id}") public Object get(@PathVariable("id") Long id) { return super.get(id); } @PutMapping @ApiOperation(value = "Revise the examination list") public Object update(@RequestBody Exam param) { return super.update(param); } @DeleteMapping(value = "/{id}") @ApiOperation(value = "Delete test questions table") public Object delete(@PathVariable("id") Long id) { return super.delete(id); } }
VM is written as follows:
package ${package.Controller}; import io.swagger.annotations.ApiOperation; import com.starmark.common.base.PageVo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; #if(${superControllerClassPackage}) import ${superControllerClassPackage}; #end /** * <p> * ${table.comment} Front end controller * </p> * * @author ${author} * @since ${date} */ @RestController @RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/${table.entityPath}") #if(${superControllerClass}) public class ${table.controllerName} extends ${superControllerClass}<${table.serviceName}> { #else public class ${table.controllerName} { #end @ApiOperation(value = "query ${table.comment}list") @PutMapping(value = "/page") public Object query(@RequestBody PageVo pageVo) { return super.queryPage(pageVo); } @ApiOperation(value = "Newly added ${table.comment}") @PostMapping public Object add(@RequestBody SysOrgDept param) { return super.add(param); } @ApiOperation(value = "${table.comment}details") @GetMapping(value = "/{id}") public Object get(@PathVariable("id") Long id) { return super.get(id); } @PutMapping @ApiOperation(value = "modify ${table.comment}") public Object update(@RequestBody ${entity} param) { return super.update(param); } @DeleteMapping(value = "/{id}") @ApiOperation(value = "delete ${table.comment}") public Object delete(@PathVariable("id") Long id) { return super.delete(id); } }
The above is the template corresponding to each class.
The code is to generate relevant files by reading the table information of the database, including field names and annotations.
Now my project development is to generate code through code generator, and then add fields midway through another article of mine
Add automatic table building and field adding function to mybatis To add fields.
If you want a complete code generator, please give me a lottery and contact me.