目錄
概述
使用演示
模板代碼?
? 實體類pojo?
?表現層controller
業(yè)務層service接口
?業(yè)務層serviceImpl實現類
持久層dao
Vue組件?
?
概述
本片博客用于分享EasyCode的自定義模板(模板在篇末),用于簡化開發(fā),免去重復性的工作。
作用:
1.根據數據庫表,后端生成基于MyBatisPlus結構下的實體類pojo,持久層dao,業(yè)務層service,表現層controller和前端vue實現增刪改查功能的組件
2.表現層controller中生成基本增刪改分頁查詢方法。
3.vue組件組件基于后端controller接口實現基本增刪改查功能結構。
?
使用演示
操作前提:?
① 創(chuàng)建好一個springboot基本項目。
?
?② 導入數據源連接相關坐標
(mysql,mybatis-plus,druid, lombok,spring-boot-starter-web)。
?
?③ 配置連接信息
?
④ 運行項目,查看是否啟動成功
?
1.準備一張數據表?
在此用一張學生信息表做演示,為了生成可讀性更好的代碼最好寫"表注釋"。
2.在IntelliJ IDEA中下載EasyCode插件
?3.ItelliJ IDEA 連接數據庫?
?
?4.選擇表,生成代碼。
?
可用多選數據表,一起生成。?
-- 彈窗勾選,點擊ok。
?
?
至此,代碼就生成完畢了。
** 但是,后端還缺少聯(lián)調對象R,和分頁攔截器的配置類沒有生成,在下方會有提供。
請根據自身習慣放到目錄結構中。
前端組件可以直接放到vue的項目中注冊引用。
生成的前端組件展示
總結:我們只需要有一張數據表,那么通過此插件和模板,就能夠根據表字段自動生成MyBatisPlus格式的后端基本增刪改分頁查接口,以及生成一個前端增刪改分頁查可用的組件,簡化了開發(fā)和減少了我們重復性的工作。?
模板代碼?
如下兩個類,請自己放到自定義目錄文件中。一個是R.java,用于前后端數據聯(lián)調的,一個是MyBatisPlusConfig.java,里面配置了分頁攔截器,用于實現分頁查詢功能。
放到目錄中哪問題不大,其他文件能引用即可。
?數據聯(lián)調對象R?
@Data
public class R<T> {
private Integer code; //編碼:1成功,0和其它數字為失敗
private String msg; //錯誤信息
private T data; //數據
public static <T> R<T> success(T object) {
R<T> r = new R<T>();
r.data = object;
r.code = 1;
return r;
}
public static <T> R<T> error(String msg) {
R r = new R();
r.msg = msg;
r.code = 0;
return r;
}
}
分頁攔截器的配置
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
// 創(chuàng)建攔截器對象
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
// 添加分頁攔截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
模板結構 (沒有就創(chuàng)建,然后復制粘貼)
文章來源:http://www.zghlxwxcb.cn/news/detail-496287.html
? 實體類pojo?
##導入宏定義
$!{define.vm}
##保存文件(宏定義)
#save("/pojo", ".java")
##包路徑(宏定義)
#setPackageSuffix("pojo")
##自動導入包(全局變量)
$!{autoImport.vm}
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import lombok.Data;
##表注釋(宏定義)
#tableComment("表實體類")
@SuppressWarnings("serial")
@Data
public class $!{tableInfo.name} {
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})//${column.comment}#end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}
?表現層controller
##導入宏定義
$!{define.vm}
##設置表后綴(宏定義)
#setTableSuffix("Controller")
##保存文件(宏定義)
#save("/controller", "Controller.java")
##包路徑(宏定義)
#setPackageSuffix("controller")
##定義服務名
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
##定義實體對象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import $!{tableInfo.savePackageName}.pojo.$!tableInfo.name;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
##表注釋(宏定義)
#tableComment("表控制層")
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/$!tool.firstLowerCase($!tableInfo.name)s")
public class $!{tableName} {
/**
* 服務對象
*/
@Autowired
private $!{tableInfo.name}Service $!{serviceName};
/**
* 分頁查詢
* @param page 查詢頁數
* @param size 一頁顯示條數
* @return ·
*/
@GetMapping("/page")
public R<Page<$!{tableInfo.name}>> getAllByPage(int page, int size){
Page<$!{tableInfo.name}> $!tool.firstLowerCase($!tableInfo.name)Page = new Page<>(page, size);
LambdaQueryWrapper<$!{tableInfo.name}> queryWrapper = new LambdaQueryWrapper<>();
//TODO 查詢條件定制
//執(zhí)行查詢
$!{serviceName}.page($!tool.firstLowerCase($!tableInfo.name)Page);
return R.success($!tool.firstLowerCase($!tableInfo.name)Page);
}
/**
* 通過主鍵查詢單條數據
*
* @param id 主鍵
* @return 單條數據
*/
@GetMapping("{id}")
public R<$!tableInfo.name> selectOne(@PathVariable Serializable id) {
return R.success(this.$!{serviceName}.getById(id));
}
/**
* 新增數據
*
* @param $!entityName 實體對象
* @return 新增結果
*/
@PostMapping
public R<String> insert(@RequestBody $!tableInfo.name $!entityName) {
return R.success(this.$!{serviceName}.save($!entityName) + "");
}
/**
* 修改數據
*
* @param $!entityName 實體對象
* @return 修改結果
*/
@PutMapping
public R<String> update(@RequestBody $!tableInfo.name $!entityName) {
return R.success(this.$!{serviceName}.updateById($!entityName) + "");
}
/**
* 刪除數據
*
* @param id 主鍵結合
* @return 刪除結果
*/
@DeleteMapping("/{id}")
public R<String> delete(@PathVariable("id") String id) {
return R.success(this.$!{serviceName}.removeById(id) + "");
}
}
業(yè)務層service接口
##導入宏定義
$!{define.vm}
##設置表后綴(宏定義)
#setTableSuffix("Service")
##保存文件(宏定義)
#save("/service", "Service.java")
##包路徑(宏定義)
#setPackageSuffix("service")
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.pojo.$!tableInfo.name;
##表注釋(宏定義)
#tableComment("表服務接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {
}
?業(yè)務層serviceImpl實現類
##導入宏定義
$!{define.vm}
##設置表后綴(宏定義)
#setTableSuffix("ServiceImpl")
##保存文件(宏定義)
#save("/service/impl", "ServiceImpl.java")
##包路徑(宏定義)
#setPackageSuffix("service.impl")
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.pojo.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
##表注釋(宏定義)
#tableComment("表服務實現類")
@Service
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
}
持久層dao
##導入宏定義
$!{define.vm}
##設置表后綴(宏定義)
#setTableSuffix("Dao")
##保存文件(宏定義)
#save("/dao", "Dao.java")
##包路徑(宏定義)
#setPackageSuffix("dao")
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.pojo.$!tableInfo.name;
import org.apache.ibatis.annotations.Mapper;
##表注釋(宏定義)
#tableComment("表數據庫訪問層")
@Mapper
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {
}
Vue組件?
##導入宏定義
$!{define.vm}
##保存文件(宏定義)
#save("/vueConponent", "vueConponent.vue")
<!-- 基于$!{tableInfo.name}表的基礎增刪改查組件 -->
<template>
<div id="container">
<!-- 新增按鈕 -->
<el-button type="primary" @click="addRow">新增</el-button>
<!-- 表格 -->
<el-table :data="tableData" style="width: 100%" stripe>
<el-table-column
v-for="(item, index) in tableTitle"
:key="index"
:prop="item.prop"
:label="item.label"
>
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="text" @click="editRow(scope.$index, scope.row)">編輯</el-button>
<el-button type="text" @click="deleteRow(scope.row)">刪除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 編輯/新增對話框 -->
<el-dialog :visible.sync="dialogVisible" title="編輯/新增" @close="closeDialog">
<el-form ref="form" :model="currentRow" :rules="formRules" label-width="80px">
<el-form-item v-for="(item, index) in tableTitle"
:key="index"
:rules="formRules"
:label="item.label"
:prop="item.prop">
<el-input v-model="currentRow[item.prop]"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="closeDialog">取消</el-button>
<el-button type="primary" @click="saveRow">保存</el-button>
</div>
</el-dialog>
<!-- 分頁組件 -->
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page.sync="currentPage"
:page-size="pageSize"
layout="total, prev, pager, next"
:total="total">
</el-pagination>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
// 表格標題
tableTitle:[
#foreach($column in $tableInfo.fullColumn)
{prop:"$!{column.name}",label:"#if(${column.comment})${column.comment}#else$!{column.name} #end"},
#end
],
tableData: [], // 表格數據
dialogVisible: false, // 新增/編輯框是否出現
currentRow: {}, // 當前編輯的行數據
// 分頁組件相關數據
//當前顯示頁
currentPage: 1,
// 每頁顯示條數
pageSize:10,
// 總條數
total:100,
// 判斷彈出的模態(tài)框是否是新增
isAddFlag:false,
// 表單校驗規(guī)則-必填
formRules: [
{ required: true, message: '該字段不能為空', trigger: 'blur' }
]
};
},
mounted(){
// 獲取表格數據(分頁查詢)
this.getTableData();
},
methods: {
// 獲取表格數據(分頁查詢)
async getTableData(){
// 封裝請求參數
// 拼接條件參數(默認查詢第1頁的10條數據)
const param = `page=${this.currentPage}&size=${this.pageSize}`;
const {data: {data: res}} = await axios.get("http://localhost:8066/$!{tool.firstLowerCase($!tableInfo.name)}s/page?" + param);
this.tableData = res.records;
this.total = res.total;
},
// 分頁組件-改變size
handleSizeChange(val) {
this.pageSize = val;
this.getTableData();
},
// 分頁組件-改變page
handleCurrentChange(val) {
this.currentPage = val;
this.getTableData();
},
// 添加按鈕觸發(fā)
addRow() {
this.isAddFlag = true;
this.currentRow = {};
this.dialogVisible = true;
},
// 編輯按鈕觸發(fā)
editRow(index, row) {
this.isAddFlag = false;
this.currentRow = {
...row
};
this.dialogVisible = true;
},
// 刪除按鈕觸發(fā)
async deleteRow(row) {
if(!confirm("永久刪除此條數據, 是否繼續(xù)?")){
this.$message({
type: 'info',
message: '已取消刪除'
});
return false;
}
// 確定刪除,根據uid刪除
const res = await axios.delete("http://localhost:8066/$!{tool.firstLowerCase($!tableInfo.name)}s/" + row.uid)
// TODO 自定義判斷狀態(tài)
this.$message.success("刪除成功!"); // 成功提示
// 處理刪除頁最后一個數據不跳轉上一頁問題
if(this.tableData.length - 1 == 0 && this.currentPage != 1){
this.currentPage -= 1;
}
this.getTableData(); // 重新獲取表格數據
},
// 關閉"新增/編輯框"
closeDialog() {
this.dialogVisible = false;
},
// 新增/編輯框內點擊"保存"按鈕
async saveRow() {
// 驗證表單,是否輸入為空
const valid = await this.$refs.form.validate();
if (!valid) {
return false;
}
// 判斷當前模態(tài)框是否是"添加"模態(tài)框
if(this.isAddFlag){
// 添加邏輯
const res = await axios.post("http://localhost:8066/$!{tool.firstLowerCase($!tableInfo.name)}s", this.currentRow);
// TODO自定義判斷返回,是否成功。
this.$message.success("添加成功!"); // 成功提示。
this.getTableData(); // 重新獲取數據
this.dialogVisible = false; // 關閉新增窗口
}else{
// 編輯邏輯
const res = await axios.put("http://localhost:8066/$!{tool.firstLowerCase($!tableInfo.name)}s", this.currentRow);
// TODO自定義判斷返回,是否成功。
this.$message.success("修改成功!"); // 成功提示。
this.getTableData(); // 重新獲取數據
this.dialogVisible = false; // 關閉修改窗口
}
}
}
};
</script>
<style scoped>
</style>
?文章來源地址http://www.zghlxwxcb.cn/news/detail-496287.html
到了這里,關于EasyCode代碼生成插件-模板分享(基于數據表生成MyBatisPlus格式的dao,service,controller和vue組件)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!