IDEA插件Easy Code自定義模板

EasyCode 介紹

EasyCode是基於IntelliJ IDEA Ultimate版開發的一個代碼生成插件,主要通過自定義模板(基於velocity)來生成各種你想要的代碼。
通常用於生成Entity、Dao、Service、Controller。如果你動手能力強還可以用於生成HTML、JS、PHP等代碼。理論上來說只要是與數據有關的代碼都是可以生成的。

EasyCode 使用教程

https://gitee.com/makejava/EasyCode/wikis/pages?sort_id=725063&doc_id=166248

EasyCode 自定義模板

dao模板

##定義初始變量
#set($tableName = $tool.append($tableInfo.name, "Dao"))
##設置回調
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))

##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;

@Mapper
public interface $!{tableName} {

    $!{tableInfo.name} getById($!pk.shortType $!pk.name);

    List<$!{tableInfo.name}> listByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    $!{tableInfo.name} getByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    List<$!{tableInfo.name}> listByIds(List<$!pk.shortType> list);

    int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    int insertBatch(List<$!{tableInfo.name}> list);

    int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    int updateByField(@Param("where") $!{tableInfo.name} where, @Param("set") $!{tableInfo.name} set);

    int updateBatch(List<$!{tableInfo.name}> list);

    int deleteById($!pk.shortType $!pk.name);

    int deleteByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
  
    int deleteByIds(List<$!pk.shortType> list);
    
    int countAll();
    
    int countByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
    
}
mapper.xml模板
 
##引入mybatis支持
$!mybatisSupport

##設置保存名稱與保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))

##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="$!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao">

    <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}ResultMap">
#foreach($column in $tableInfo.fullColumn)
        <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
    </resultMap>

    <sql id="table_field">
      #allSqlColumn()
      
    </sql>
       
    <!--通過Id查詢單個-->
    <select id="getById" resultMap="$!{tableInfo.name}ResultMap" parameterType="$pk.type">
        select
          <include refid="table_field" />
        from $!tableInfo.obj.name
        where $!pk.obj.name = #{$!pk.name,jdbcType=$!pk.ext.jdbcType}
    </select>


    <!--通過實體不爲空的屬性作爲篩選條件查詢列表-->
    <select id="listByEntity" resultMap="$!{tableInfo.name}ResultMap" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
        select
          <include refid="table_field" />
        from $!tableInfo.obj.name
        <where>
#foreach($column in $tableInfo.fullColumn)
            <if test="$!column.name != null">
                and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType}
            </if>
#end
        </where>
    </select>

    <!--通過實體不爲空的屬性作爲篩選條件查詢單個-->
    <select id="getByEntity" resultMap="$!{tableInfo.name}ResultMap" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
        select
          <include refid="table_field" />
        from $!tableInfo.obj.name
        <where>
#foreach($column in $tableInfo.fullColumn)
            <if test="$!column.name != null">
                and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType}
            </if>
#end
        </where>
    </select>

    <!--通過Id列表作爲篩選條件查詢列表,列表長度不爲0-->
    <select id="listByIds" resultMap="$!{tableInfo.name}ResultMap" parameterType="list">
        select
          <include refid="table_field" />
        from $!tableInfo.obj.name
        where $!pk.obj.name in
        <foreach item="item" collection="list" separator="," open="(" close=")" index="index">
            #{item}
        </foreach>
    </select>

    <!--新增實體屬性不爲null的列-->
    <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
        insert into $!{tableInfo.obj.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
#foreach($column in  $tableInfo.fullColumn)
          <if test="$!column.name != null">
             $!column.obj.name,
          </if>
#end          
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
#foreach($column in  $tableInfo.fullColumn)
          <if test="$!column.name != null">
             #{$!column.name,jdbcType=$!column.ext.jdbcType},
          </if>
#end
        </trim>
    </insert>

    <!--批量新增所有列,列表長度不能爲0,且列表id統一爲null或者統一不爲null-->
    <insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true" parameterType="list">
        insert into $!{tableInfo.obj.name}
         (#foreach($column in $tableInfo.fullColumn)$!{column.obj.name}#if($velocityHasNext), #end#end)
        values
        <foreach item="item" collection="list" separator="," open="" close="" index="index">
         (#foreach($column in $tableInfo.fullColumn)#{item.$!{column.name}}#if($velocityHasNext), #end#end)
        </foreach>
    </insert>

    <!--通過主鍵修改實體屬性不爲null的列-->
    <update id="update" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
        update $!{tableInfo.obj.name}
        <set>
#foreach($column in $tableInfo.otherColumn)
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
                $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType},
            </if>
#end
        </set>
        where $!pk.obj.name = #{$!pk.name,jdbcType=$!pk.ext.jdbcType}
    </update>

    <!--通過表字段修改實體屬性不爲null的列-->
    <update id="updateByField">
        update $!{tableInfo.obj.name}
        <set>
#foreach($column in $tableInfo.otherColumn)
            <if test="where.$!{column.name} == null and set.$!{column.name} != null#if($column.type.equals("java.lang.String")) and set.$!{column.name} != ''#end">
                $!column.obj.name = #{set.$!{column.name},jdbcType=$!column.ext.jdbcType},
            </if>
#end
        </set>
        <where>
#foreach($column in $tableInfo.fullColumn)
            <if test="where.$!{column.name} != null">
                and $!column.obj.name = #{where.$!{column.name},jdbcType=$!column.ext.jdbcType}
            </if>
#end
        </where>
    </update>

    <!--通過主鍵修改實體列表,列表長度不能爲0,注意:當實體屬性爲null時,對應的列也會別更新爲null-->
    <update id="updateBatch" parameterType="list">
        update $!{tableInfo.obj.name}
        <trim prefix="set" suffixOverrides=",">
#foreach($column in $tableInfo.otherColumn)
            <trim prefix="$!{column.obj.name} = case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                  when $!pk.obj.name = #{item.$!pk.name} then #{item.$!column.name}
                 </foreach>
            </trim>
#end
        </trim>
        where $!pk.obj.name in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.$!pk.name,jdbcType=$!pk.ext.jdbcType}
        </foreach>
    </update>
    
    <!--通過主鍵刪除-->
    <delete id="deleteById" parameterType="$pk.type">
        delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name,jdbcType=$!pk.ext.jdbcType}
    </delete>

    <!--通過實體非空屬性刪除-->
    <delete id="deleteByEntity" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
        delete from $!{tableInfo.obj.name}
        <where>
#foreach($column in $tableInfo.otherColumn)
            <if test="$!column.name != null">
                and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType}
            </if>
#end
        </where>
    </delete>
    
    <!--通過主鍵列表刪除,列表長度不能爲0-->
    <delete id="deleteByIds" parameterType="list">
        delete from $!{tableInfo.obj.name} where $!pk.obj.name in
        <foreach item="item" collection="list" separator="," open="(" close=")" index="index">
            #{item}
        </foreach>
    </delete>
    
    <select id="countAll" resultType="int">
        select count(*) from $!{tableInfo.obj.name}
    </select>
    
    <select id="countByEntity" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" resultType="int">
        select count(*) from $!{tableInfo.obj.name}
        <where>
#foreach($column in $tableInfo.fullColumn)
            <if test="$!column.name != null">
                and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType}
            </if>
#end
        </where>
    </select>
</mapper>
 

entity模板(使用lombok)

複製代碼
##引入宏定義
$!define

##使用宏定義設置回調(保存位置與文件後綴)
#save("/entity", ".java")

##使用宏定義設置包後綴
#setPackageSuffix("entity")

##使用全局變量實現默認包導入
$!autoImport
import lombok.Data;
#foreach($column in $tableInfo.fullColumn)
#if($column.type.equals("java.util.Date"))
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
#break
#end
#end
/**
 * $!{tableInfo.comment}($!{tableInfo.name})實體類
 *
 * @author xiaoG
 * @since $!time.currTime()
 */
@Data 
public class $!{tableInfo.name} {
#foreach($column in $tableInfo.fullColumn)
    #if(${column.comment})/**
    * ${column.comment}
    */#end

#if($column.type.equals("java.util.Date"))
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
#end
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end

}
複製代碼

service模板

複製代碼
##定義初始變量
#set($tableName = $tool.append($tableInfo.name, "ServiceI"))
##設置回調
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))

##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;

import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};

import java.util.List;

public interface $!{tableName} {
    
    $!{tableInfo.name}Dao get$!{tableInfo.name}Dao();
   
    $!{tableInfo.name} getById($!pk.shortType $!pk.name);

    $!{tableInfo.name} getByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    List<$!{tableInfo.name}> listByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    List<$!{tableInfo.name}> listByIds(List<$!pk.shortType> ids);

    int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    int insertBatch(List<$!{tableInfo.name}> list);

    int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    int updateBatch(List<$!{tableInfo.name}> list);

    int deleteById($!pk.shortType $!pk.name);

    int deleteByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
  
    int deleteByIds(List<$!pk.shortType> list);
    
    int countAll();
    
    int countByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
}
 

serviceImpl模板

 
##定義初始變量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##設置回調
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))

##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;

import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}ServiceI;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class $!{tableName} implements $!{tableInfo.name}ServiceI {

    @Resource(type = $!{tableInfo.name}Dao.class)
    private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;

    @Override
    public $!{tableInfo.name}Dao get$!{tableInfo.name}Dao() {
        return $!tool.firstLowerCase($!{tableInfo.name})Dao;
    }

    public $!{tableInfo.name} getById($!pk.shortType $!pk.name) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.getById($!{pk.name});
    }

    public $!{tableInfo.name} getByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.getByEntity($!{tool.firstLowerCase($!{tableInfo.name})});
    }

    public List<$!{tableInfo.name}> listByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.listByEntity($!{tool.firstLowerCase($!{tableInfo.name})});
    }

    public List<$!{tableInfo.name}> listByIds(List<$!pk.shortType> ids) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.listByIds(ids);
    }

    public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!{tool.firstLowerCase($!{tableInfo.name})});
    }

    public int insertBatch(List<$!{tableInfo.name}> list) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.insertBatch(list);
    }

    public int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.update($!{tool.firstLowerCase($!{tableInfo.name})});
    }

    public int updateBatch(List<$!{tableInfo.name}> list) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.updateBatch(list);
    }

    public int deleteById($!pk.shortType $!pk.name) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name);
    }

    public int deleteByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteByEntity($!{tool.firstLowerCase($!{tableInfo.name})});
    }
  
    public int deleteByIds(List<$!pk.shortType> list) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteByIds(list);
    }

    public int countAll() {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.countAll();
    }
    
    public int countByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.countByEntity($!tool.firstLowerCase($!{tableInfo.name}));
    }

}
 

controller模板

 
##定義初始變量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##設置回調
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}ServiceI;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
    
    @Autowired
    private $!{tableInfo.name}ServiceI $!tool.firstLowerCase($tableInfo.name)Service;

    @GetMapping("/get/{$!pk.name}")
    public $!{tableInfo.name} getById(@PathVariable $!pk.shortType $!pk.name) {
        $tableInfo.name $!tool.firstLowerCase($tableInfo.name) = $!{tool.firstLowerCase($tableInfo.name)}Service.getById(id);
        return $!tool.firstLowerCase($tableInfo.name)!=null?$!tool.firstLowerCase($tableInfo.name):new $!{tableInfo.name}();
    }

    @GetMapping("/get")
    public $!{tableInfo.name} getByEntity($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
        return $!{tool.firstLowerCase($tableInfo.name)}Service.getByEntity($!{tool.firstLowerCase($!{tableInfo.name})});
    }

    @GetMapping("/list")
    public List<$!{tableInfo.name}> list($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
        List<$tableInfo.name> $!{tool.firstLowerCase($tableInfo.name)}List = $!{tool.firstLowerCase($tableInfo.name)}Service.listByEntity($!{tool.firstLowerCase($!{tableInfo.name})});
        return $!{tool.firstLowerCase($tableInfo.name)}List;
    }

    @PostMapping("/insert")
    public $tableInfo.name insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)){
        $!{tool.firstLowerCase($tableInfo.name)}Service.insert($!tool.firstLowerCase($tableInfo.name));
        return $!tool.firstLowerCase($tableInfo.name);
    }

    @PutMapping("/update")
    public int update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)){
        return $!{tool.firstLowerCase($tableInfo.name)}Service.update($!tool.firstLowerCase($tableInfo.name));
    }

    @DeleteMapping("/delete/{$!pk.name}")
    public int deleteOne(@PathVariable $!pk.shortType $!pk.name){
        return $!{tool.firstLowerCase($tableInfo.name)}Service.deleteById($!pk.name);
    }

    @DeleteMapping("/delete")
    public int deleteBatch(@RequestBody List<$!pk.shortType> $!{pk.name}s){
        int result = 0;
        if ($!{pk.name}s!=null&&$!{pk.name}s.size()>0) result = $!{tool.firstLowerCase($tableInfo.name)}Service.deleteByIds($!{pk.name}s);
        return result;
    }

}
 

vue模板

 
##引入mybatis支持
$!mybatisSupport

##設置保存名稱與保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Table.vue"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/vue"))
#foreach($column in $tableInfo.fullColumn)
    #if($column.name.startsWith("is") && $column.type.equals("java.lang.Boolean"))
        $!column.setName($tool.firstLowerCase($column.name.substring(2)))
    #end
#end
##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end
<template>
  <div class="app-container">
    <el-button :disabled="multipleSelection.length==0" class="el-icon-delete" type="danger" size="small" @click="deleteRows">刪除</el-button>
    <el-button :disabled="multipleSelection.length!=1" class="el-icon-edit" type="primary" size="small" @click="openForm(true)">修改</el-button>
    <el-button class="el-icon-plus" type="success" size="small" @click="openForm(false)">添加</el-button>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="40"/>
      <el-table-column label="ID" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.$!{pk.name} }}
        </template>
      </el-table-column>
      #foreach($column in $tableInfo.otherColumn)
      #if(${column.name.equals('createTime')} || ${column.name.equals('updateTime')})
      <el-table-column align="center" prop="${column.name}" label="${column.name}">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.${column.name} }}</span>
        </template>
      </el-table-column>
      #else
      #if($column.type.equals("java.lang.Boolean"))
      <el-table-column label="${column.name}" align="center">
        <template slot-scope="scope">
          <i :style="'color:'+(scope.row.${column.name} ? '#67C23A' : '#F56C6C')" :class="scope.row.${column.name} ? 'el-icon-check' : 'el-icon-close'"></i>
        </template>
      </el-table-column>
      #elseif($column.type.equals("java.lang.Short"))
      <el-table-column label="${column.name}" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.${column.name} | ${column.name}TypeFilter">{{ scope.row.${column.name} | ${column.name}NameFilter }}</el-tag>
        </template>
      </el-table-column>
      #else
      <el-table-column label="${column.name}" align="center">
        <template slot-scope="scope">
          {{ scope.row.${column.name} }}
        </template>
      </el-table-column>
      #end
      #end
      #end
    </el-table>
    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!--form表單-->
    <el-dialog
      :title="form.$!{pk.name}?'修改':'添加'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="30%">
      <el-form ref="form" :model="form" label-width="120px">
        #foreach($column in $tableInfo.otherColumn)
        #if(!${column.name.equals('createTime')} && !${column.name.equals('updateTime')})
        #if($column.type.equals("java.lang.Boolean"))
        <el-form-item label="${column.name}">
          <el-radio-group v-model="form.${column.name}">
            <el-radio :label="true" />
            <el-radio :label="false" />
          </el-radio-group>
        </el-form-item>
        #elseif($column.type.equals("java.lang.Short"))
        <el-form-item label="${column.name}">
          <el-select v-model="form.${column.name}" placeholder="請選擇">
            #set($startIdx = ${column.comment.indexOf("[")}+1)
            #set($endIdx = ${column.comment.indexOf("]")})
            #set($enumComments = ${column.comment.substring($startIdx,$endIdx).split(",")})
            #foreach($enumComment in $enumComments)
            <el-option label="${enumComment}" :value="${foreach.index}" />
            #end
          </el-select>
        </el-form-item>
        #else
        <el-form-item label="${column.name}">
          <el-input v-model="form.${column.name}" />
        </el-form-item>
        #end
        #end
        #end
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRow">確 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getList, deleteRows, saveRow } from '@/api/$!{tool.firstLowerCase($tableInfo.name)}'

export default {
filters: {
#foreach($column in $tableInfo.otherColumn)
#if($column.type.equals("java.lang.Short"))
#set($startIdx = ${column.comment.indexOf("[")}+1)
#set($endIdx = ${column.comment.indexOf("]")})
#set($enumComments = ${column.comment.substring($startIdx,$endIdx).split(",")})
  ${column.name}TypeFilter(${column.name}) {
    const ${column.name}Map = {
#foreach($enumComment in $enumComments)
      ${foreach.index}: 'success',
#end
    }
    return ${column.name}Map[${column.name}]
  },
  ${column.name}NameFilter(${column.name}) {
    const ${column.name}Map = {
#foreach($enumComment in $enumComments)
      ${foreach.index}: '${enumComment}',
#end
    }
    return ${column.name}Map[${column.name}]
  },
#end
#end
  },
  data() {
    return {
      list: [],
      listLoading: true,
      multipleSelection: [],
      current: 1,
      total: 0,
      size: 10,
      dialogVisible: false,
      form: {}
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.multipleSelection.length = 0
      this.listLoading = true
      const params = {
        size: this.size,
        current: this.current
      }
      getList(params).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteRows() {
      if (this.multipleSelection.length > 0) {
        this.$confirm('此操作將永久刪除該數據, 是否繼續?', '提示', {
          confirmButtonText: '確定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.listLoading = true
          const ids = []
          this.multipleSelection.map(row => ids.push(row.$!{pk.name}))
          deleteRows(ids).then(response => {
            this.listLoading = false
            if (response.data) {
              this.$message({
                type: 'success',
                message: '刪除成功!'
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: '刪除失敗!'
              })
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消刪除'
          })
        })
      } else {
        this.$message.info('請至少選擇一項')
      }
    },
    openForm(hasId) {
      if (hasId) {
        this.form = JSON.parse(JSON.stringify(this.multipleSelection[0]))
      } else {
        this.form = {}
      }
      this.dialogVisible = true
    },
    saveRow() {
      let msg = '添加'
      if (this.form.$!{pk.name}) {
        msg = '修改'
        this.form.createTime = null
        this.form.updateTime = null
      }
      saveRow(this.form).then(response => {
        this.listLoading = false
        if (response.data) {
          this.$message({
            type: 'success',
            message: msg + '成功'
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: msg + '失敗!'
          })
        }
      })
      this.dialogVisible = false
    },
    handleSizeChange(val) {
      this.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.current = val
      this.fetchData()
    }
  }
}
</script>
 

ajax模板

 
##引入mybatis支持
$!mybatisSupport
##設置保存名稱與保存位置
$!callback.setFileName($tool.append($!tool.firstLowerCase($tableInfo.name), "Table.js"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/js"))
##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end
import request from '@/utils/request'

const url = '/$!{tool.firstLowerCase($tableInfo.name)}'

export function getList(params) {
  return request({
    url: url,
    method: 'get',
    params
  })
}

export function deleteRows(data) {
  return request({
    url: url,
    method: 'delete',
    data
  })
}

export function saveRow(data) {
  return request({
    url: url,
    method: data.$!{pk.name} ? 'put' : 'post',
    data
  })
}
 

BaseController

 
public abstract class BaseController {

    public <T> Response<T> success(T data) {
        return new Response<>(data,20000,"請求成功");
    }

    @Data
    @AllArgsConstructor
    class Response<T> {
        private T data;
        private int code;
        private String message;
    }
}
 

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章