[项目中经常写CRUD,但实际这些工作,我觉得如果有一个完整的代码规范,完全可以自动生成,加快开发效率.
代码生成器技术原理不复杂,一般就是写好一个模板生成一系列的代码而已。我看到mybatis_plus的代码生成器就相当不错,就自己拿过来改造了一下
1.项目中,需先引入vm库,用来生成代码
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>${velocity.version}</version>
</dependency>
2.model的代码如下
/**
* <p>
* 考试题目表
* </p>
*
* @author starmark
* @since 2018-04-12
*/
@Data
@EqualsAndHashCode(callSuper=false)
public class Exam implements Serializable {
/**
* 主键
*/
@TableId("id")
private Long id;
/**
* 题目
*/
@TableField("subject")
private String subject;
/**
* 答案
*/
@TableField("answer")
private String answer;
/**
* 类别
*/
@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;
}
vm就改造成如下:
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.ServiceImpl实现类如下:
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>
* 考试题目表 服务实现类
* </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就写成如下:
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} 服务实现类
* </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.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;
import com.starmark.core.web.base.AbstractBaseController;
/**
* <p>
* 考试题目表 前端控制器
* </p>
*
* @author starmark
* @since 2018-04-12
*/
@RestController
@RequestMapping("/exam/exam")
public class ExamController extends AbstractBaseController<IExamService> {
@ApiOperation(value = "查询考试题目表列表")
@PutMapping(value = "/page")
public Object query(@RequestBody PageVo pageVo) {
return super.queryPage(pageVo);
}
@ApiOperation(value = "新增考试题目表")
@PostMapping
public Object add(@RequestBody SysOrgDept param) {
return super.add(param);
}
@ApiOperation(value = "考试题目表详情")
@GetMapping(value = "/{id}")
public Object get(@PathVariable("id") Long id) {
return super.get(id);
}
@PutMapping
@ApiOperation(value = "修改考试题目表")
public Object update(@RequestBody Exam param) {
return super.update(param);
}
@DeleteMapping(value = "/{id}")
@ApiOperation(value = "删除考试题目表")
public Object delete(@PathVariable("id") Long id) {
return super.delete(id);
}
}
VM就编写如下:
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} 前端控制器
* </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 = "查询${table.comment}列表")
@PutMapping(value = "/page")
public Object query(@RequestBody PageVo pageVo) {
return super.queryPage(pageVo);
}
@ApiOperation(value = "新增${table.comment}")
@PostMapping
public Object add(@RequestBody SysOrgDept param) {
return super.add(param);
}
@ApiOperation(value = "${table.comment}详情")
@GetMapping(value = "/{id}")
public Object get(@PathVariable("id") Long id) {
return super.get(id);
}
@PutMapping
@ApiOperation(value = "修改${table.comment}")
public Object update(@RequestBody ${entity} param) {
return super.update(param);
}
@DeleteMapping(value = "/{id}")
@ApiOperation(value = "删除${table.comment}")
public Object delete(@PathVariable("id") Long id) {
return super.delete(id);
}
}
以上就是各个类之间对应的模板。
代码就是通过读数据库的表信息,含字段名称,注解难来生成相关的文件。
现在我项目开发是通过代码生成器生成代码,然后中途加字段是通过我另一文章
给mybatis添加自动建表,自动加字段的功能来加字段。
如果想要完整的代码生成器,请打赏一注彩票钱再联系我。