JAVA實現編寫平臺程式碼生成器

愛餘星痕發表於2018-04-13

[專案中經常寫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新增自動建表,自動加欄位的功能來加欄位。
如果想要完整的程式碼生成器,請打賞一注彩票錢再聯絡我。


相關文章