IDEA插件Easy Code自定义模板
猛然发现自己的文章居然有4000的阅读量,于是想着重新编辑这篇文章,但是它不支持用markdown重新编辑,真是要吐槽下博客园
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; } }
不积跬步无以至千里