freemarker模板引擎+springboot实现vue前端页面的生成

生成配置文件及结构

 

 

引入freemarker依赖

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
            <scope>test</scope>
        </dependency>

vue前端页面生成模板

根据需求可自行调整

<template>
    <!--createTime:${date} ,author:${author} -->
    <div class="app-container">

        <!--搜索框-->
        <el-form :inline="false" size="medium" v-if="showSearchBox" :style="{ 'height': searchBoxHeight + 'px' }"
                 label-position="right" label-width="130px">
            <el-row >
            <#list searchModels as item>
                <#if item_index!=0>
            <el-col :span="6">

                <el-form-item label="${item.label}">
                    <#if item.datePicker==true >
                        <div class="block">
                            <el-date-picker
                                    v-model="serverQuery.${item.value}"
                                    type="daterange"
                                    range-separator="至"
                                    start-placeholder="开始日期"
                                    value-format="yyyy-MM-dd"
                                    class="dialog_input"
                                    @change="search"
                                    end-placeholder="结束日期">
                            </el-date-picker>
                        </div>
                    </#if>
                    <#if item.checkState==true >
                        <div class="block">
                            <el-select v-model="serverQuery.${item.value}" placeholder="请选择" style="width:100%;" clearable @change="search">
                                <el-option
                                        v-for="item in stateList"
                                        :label="item.label"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </#if>
                    <#if item.dropDown==true >
                        <div class="block">
                            <el-select v-model="serverQuery.${item.dropdownInfo.value}" placeholder="请选择" clearable class="dialog_input">
                                <el-option
                                        v-for="item in ${item.dropdownInfo.value}Options"
                                        :label="item.${item.dropdownInfo.label}"
                                        :value="item.${item.dropdownInfo.value}">
                                </el-option>
                            </el-select>
                        </div>
                    </#if>
                    <#if item.checkState==false&&item.datePicker==false&&item.dropDown==false >
                        <el-input placeholder="请输入" v-model="serverQuery.${item.value}" clearable style="width:100%;"
                                  @keyup.enter.native="search"></el-input>
                    </#if>
                </el-form-item>
            </el-col>
                </#if>
            </#list>

                <el-col :span="6" style="text-align: right;">
                    <el-form-item>
                        <el-button @click="resetSearch">清空</el-button>
                        <el-button type="success" @click="search">搜索</el-button>
                    </el-form-item>
                </el-col>
            </el-row>

            <!--样式分割线-->
            <el-row>
                <el-col class="search-bar-height">
                    <el-divider></el-divider>
                </el-col>
            </el-row>

        </el-form>


        <!--按钮框-->
        <el-form :inline="true">

            <el-row>
                <el-col :span="12">
                    <!--左侧按钮-->
                    <el-form-item class="fixed_left">
                        <PermissionButton size="medium" label="添加${modelName}(两列)" icon="el-icon-plus" perms="1000" type="success" @click="openDialog(true)" />
                        <PermissionButton size="medium" label="添加${modelName}(三列)" icon="el-icon-plus" perms="1000" type="success" @click="openDialogThree(true)" />
                    </el-form-item>
                </el-col>


                <el-col :span="12">
                    <!--右侧按钮-->
                    <el-form-item class="fixed_right">
                        <!--展示/关闭搜索框按钮-->
                        <el-button size="medium" icon="el-icon-search" circle @click="clickSearch"></el-button>
                    </el-form-item>
                </el-col>

            </el-row>

        </el-form>

        <!--数据展示-->
        <div>
            <el-table v-loading="listLoading" :height="tableHeight - (showSearchBox ? searchBoxHeight : 0)" :data="listData" border
                      el-table-column fit highlight-current-row>
    <#list searchModels as item>
        <#if item_index = 0 >
            <el-table-column align="center" prop="${item.value}" label="${item.label}" :show-overflow-tooltip='true' >
                <template slot-scope="scope">
                    <el-link type="primary" @click="getById(scope.row.${item.value}, true)">{{ scope.row.${item.value} }}</el-link>
                </template>
            </el-table-column>
        </#if>
        <#if item_index != 0 >
            <el-table-column align="center" prop="${item.value}" label="${item.label}" :show-overflow-tooltip='true'  min-width="200">
                <#if item.checkState==true>
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.${item.value}" type="success">是</el-tag>
                    <el-tag v-else type="info">否</el-tag>
                </template>
                </#if>
            </el-table-column>
        </#if>
    </#list>

            <el-table-column align="center" label="操作" fixed="right" width="200px">
                <template slot-scope="scope">
                    <#list searchModels as item>
                        <#if item_index = 0 >
                    <PermissionButton size="mini" perms="1000" type="primary" label="编辑"
                                      @click.native.stop="getById(scope.row.${item.value}, true)" />
                    <PermissionButton size="mini" perms="1000" type="danger" label="删除"
                                      @click.native.stop="delById(scope.row.${item.value})" />
                        </#if>
                    </#list>
                </template>
            </el-table-column>

            </el-table>

            <el-pagination
                    style="margin-top:15px"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page.sync="serverQuery.page"
                    :page-sizes="[20,40, 60, 80, 100]"
                    :page-size.sync="serverQuery.size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="serverQuery.total"
            ></el-pagination>

        </div>



        <!--添加/${modelName}信息两列-->
        <el-dialog v-dialogDrag top="1%" :modal-append-to-body="false" :title="dialog_title" width="1100px" :visible.sync="dialogBaseFlag"
                   draggable :lock-scroll="false" :modal="true"
                   @before-close="this.$refs['ruleForm'].clearValidate()" class="custom_append">


            <div class="div_center" style="max-height:700px;">

                <!--添加${modelName}信息-->
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" size="medium" label-position="right"
                         label-width="135px">

                    <div class="divider"><span>${modelName}信息</span><p></p></div>
                    <el-row>
                    <#list searchModels as item>
                        <#if item_index!=0>
                        <el-col :span="12">
                            <el-form-item label="${item.label}" prop="${item.value}">

                                <#if item.datePicker==true >
                                    <div class="block">
                                        <el-date-picker
                                                v-model="ruleForm.${item.value}"
                                                type="date"
                                                value-format="yyyy-MM-dd"
                                                class="dialog_input"
                                                placeholder="选择日期时间">
                                        </el-date-picker>
                                    </div>
                                </#if>
                                <#if item.checkState==true >
                                    <div class="block">
                                        <el-switch v-model="ruleForm.${item.value}" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
                                    </div>
                                </#if>
                                <#if item.dropDown==true >
                                    <div class="block">
                                        <el-select v-model="ruleForm.${item.dropdownInfo.value}" placeholder="请选择" class="dialog_input">
                                            <el-option
                                                    v-for="item in ${item.dropdownInfo.value}Options"
                                                    :label="item.${item.dropdownInfo.label}"
                                                    :value="item.${item.dropdownInfo.value}">
                                            </el-option>
                                        </el-select>
                                    </div>
                                </#if>
                                <#if item.checkState==false&&item.datePicker==false&&item.dropDown==false >
                                    <el-input placeholder="请输入" v-model="ruleForm.${item.value}" class="dialog_input"></el-input>
                                </#if>
                            </el-form-item>
                        </el-col>
                        </#if>
                    </#list>
                    </el-row>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer" style="text-align: center">
                <el-button size="medium" @click="dialogBaseFlag = false">取 消</el-button>
                <el-button size="medium" type="success" :disabled="submitButtonFlag" @click="submitForm('ruleForm')">提 交
                </el-button>
            </div>
        </el-dialog>

        <!--添加/${modelName}信息三列-->
        <el-dialog v-dialogDrag :title="dialog_title" width="1350px" top="1%"
                   :visible.sync="dialogBaseFlagThree" :modal="true" @before-close="this.$refs['ruleForm'].clearValidate()"
                   class="custom_append">


            <div class="div_center" style="max-height:700px;">

                <!--添加${modelName}信息-->
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" size="medium" label-position="right"
                         label-width="135px">

                    <div class="divider"><span>${modelName}信息</span><p></p></div>
                    <el-row>
                        <#list searchModels as item>
                            <#if item_index!=0>
                                <el-col :span="12">
                                    <el-form-item label="${item.label}" prop="${item.value}">
                                        <#if item.datePicker==true >
                                            <div class="block">
                                                <el-date-picker
                                                        v-model="ruleForm.${item.value}"
                                                        type="date"
                                                        value-format="yyyy-MM-dd"
                                                        class="dialog_input"
                                                        placeholder="选择日期时间">
                                                </el-date-picker>
                                            </div>
                                        </#if>
                                        <#if item.checkState==true >
                                            <div class="block">
                                                <el-switch v-model="ruleForm.${item.value}" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
                                            </div>
                                        </#if>
                                        <#if item.dropDown==true >
                                            <div class="block">
                                                <el-select v-model="ruleForm.${item.dropdownInfo.value}" placeholder="请选择" class="dialog_input">
                                                    <el-option
                                                            v-for="item in ${item.dropdownInfo.value}Options"
                                                            :label="item.${item.dropdownInfo.label}"
                                                            :value="item.${item.dropdownInfo.value}">
                                                    </el-option>
                                                </el-select>
                                            </div>
                                        </#if>
                                        <#if item.checkState==false&&item.datePicker==false&&item.dropDown==false >
                                            <el-input placeholder="请输入" v-model="ruleForm.${item.value}" class="dialog_input"></el-input>
                                        </#if>
                                    </el-form-item>
                                </el-col>
                            </#if>
                        </#list>
                    </el-row>




                </el-form>
            </div>
            <div slot="footer" class="dialog-footer" style="text-align: center">
                <el-button size="medium" @click="dialogBaseFlagThree = false">取 消</el-button>
                <el-button size="medium" type="success" :disabled="submitButtonFlag" @click="submitForm('ruleForm')">提 交
                </el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import { sendPostByJson, sendPostByKeyValue,sendGet } from '@/utils/httpUtils'
    import FormValidate from '@/utils/validate'
    import Tinymce from '@/components/Tinymce'

    export default {

        components: { Tinymce },

        data() {
            return {

                baseImgURL: undefined,
                headers: undefined,
                //展示表格高度
                tableHeight: window.innerHeight - 230,
                //搜索框展示高度
                searchBoxHeight: 230,
                <#list searchModels as item>
               <#if item.dropDown==true>
                //${item.label}集合
                ${item.dropdownInfo.value}Options:[],
               </#if>
                </#list>
                //是否显示搜索框
                showSearchBox: false,
                //查询数据容器
                serverQuery: {
                <#list searchModels as item>
                    //${item.label}
                    ${item.value} :undefined,
                </#list>
                    total: 0,
                    page: 1,
                    size: 50
                },
                dialog_title: '添加${modelName}',
                dialogBaseFlag: false,
                dialogBaseFlagThree: false,
                listLoading: false,
                //按钮置灰
                submitButtonFlag: false,
                //${modelName}集合容器
                listData: [],
                //${modelName}容器
                ruleForm: {
                    <#list searchModels as item>
                    <#if item.checkState==false>
                    //${item.label}
                    ${item.value} :false,
                    </#if>
                    <#if item.checkState!=false>
                    //${item.label}
                    ${item.value} :undefined,
                    </#if>
                    </#list>
                },
                //是否状态查询
                stateList: [
                    { value: undefined, label: '全部' },
                    { value: true, label: '是' },
                    { value: false, label: '否' },
                ],
                //修改规则
                rules: {
                <#list searchModels as item>
                ${item.value}: [
                    { required: true, message: "${item.label}不能为空", trigger: "change" },
                ],
                </#list>
                },

                //${modelName}类容器
                ruleFormProductClass: {},
                //${modelName}类集合
                productClassList: [],
                //${modelName}类集合还原
                productClassListRestoreData: [],
                //${modelName}类是否加载
                listLoadingProductClass: false,
                //按钮置灰
                submitButtonFlagProductClass: false,
            };
        },
        mounted: function () {
            this.baseImgURL = process.env.VUE_APP_BASE_IMG
            this.headers = {
                Token: this.$store.state.user.token,
            };
            //初始化数据
            this.search()
            this.initFun()
        },
        methods: {
            //初始化选择查询的集合
            async initFun(){
                <#list searchModels as item>
                <#if item.dropDown==true>
                const res${item_index} = await sendPostByKeyValue("/${item.dropdownInfo.tableName}/listNoPage")
                if (res${item_index}&&res${item_index}.code == 200) {
                    this.${item.dropdownInfo.value}Options = res${item_index}.data;
                }
                </#if>
                </#list>
            },
            handleSizeChange(val) {
                //查询
                this.serverQuery.size = val;
                this.search();
            },

            handleCurrentChange(val) {
                //查询
                this.serverQuery.page = val;
                this.search();
            },

            /**
             * 触发查询图标
             */
            clickSearch() {
                this.showSearchBox = !this.showSearchBox
            },

            /**
             * 重置${modelName}容器
             */
            resetRuleForm() {
                this.ruleForm = {
                    <#list searchModels as item>
                    //${item.label}
                    ${item.value} :undefined,
                    </#list>
                }
            },

            /**
             * 重置${modelName}类容器
             */
            resetRuleFormProductClass() {
                this.ruleFormProductClass = {
                    <#list searchModels as item>
                    //${item.label}
                    ${item.value} :undefined,
                    </#list>
                }
            },


            /**
             * 查询${modelName}信息
             * @param productId
             * @param detail
             * @returns {Promise<void>}
             */
            async getById(productId, detail = false) {
                this.openDialogThree(false)
                try {
                    const res = await sendPostByKeyValue('/${apiName}/detail', { id: productId })
                    if (res && res.code === 200) {
                        this.ruleForm = res.data
                    } else {
                        this.$message({ type: 'error', message: res && res.message ? res.message : '获取详情失败' })
                    }
                } catch (e) {
                    console.log(e.toString())
                }
            },

            /**
             *保存${modelName}
             * @returns {Promise<void>}
             */
            async save() {
                this.submitButtonFlag = true
                try {
                    let res
                    var message
                    if(this.ruleForm.${id}){
                        res = await sendPostByJson('/${apiName}/update', this.ruleForm)
                        message='更新'
                    }else{
                        res = await sendPostByJson('/${apiName}/add', this.ruleForm)
                        message='保存'
                    }
                    this.submitButtonFlag = false
                    if (res && res.code === 200) {
                        await this.search()
                        this.$message({ type: 'success', message: message+'成功' })
                    } else {
                        this.$message({ type: 'error', message: res && res.message ? res.message : message+'失败' })
                    }
                    this.dialogBaseFlag = false
                } catch (e) {
                    console.log(e.toString())
                } finally {
                    this.submitButtonFlag = false
                    this.dialogBaseFlagThree = false

                }
            },
            /**
             * 打开添加/修改${modelName}弹窗s
             * @param flag
             */
            openDialog(flag) {
                this.dialogBaseFlag = true
                this.dialog_title = flag ? '添加${modelName}' : '${modelName}信息'
                this.resetRuleForm()
                this.$nextTick(() => {
                    this.$refs['ruleForm'].clearValidate()
                })
            },

            openDialogThree(flag) {
                this.dialogBaseFlagThree = true
                this.dialog_title = flag ? '添加${modelName}' : '${modelName}信息'
                this.resetRuleForm()
                this.$nextTick(() => {
                    this.$refs['ruleForm'].clearValidate()
                })
            },

            //删除
            delById(id) {
                this.$confirm("确定要删除该记录?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    lockScroll: false,
                    type: "warning",
                    center: true,
                    beforeClose: (action, instance, done) => {
                        // 确定
                        if (action === "confirm") {
                            // 删除操作
                            sendGet("/${apiName}/delete?id=" + id)
                              .then((response) => {
                                if (response.code == 200) {
                                  this.search();
                                  this.$message({
                                    type: "success",
                                    message: "删除成功",
                                  });
                                } else {
                                  this.$message({
                                    type: "error",
                                    message: response.message,
                                  });
                                }
                              })
                              .catch((err) => {
                                console.log(err);
                              });
                            done();
                        } else {
                            done();
                        }
                    },
                }).catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消",
                    });
                });
            },


            /**
             * 保存按钮
             * @param formName
             */
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.save()
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            },
            /**
             * 重置查询条件
             * @returns {Promise<void>}
             */
            async resetSearch() {
                this.serverQuery = {
                    <#list searchModels as item>
                    //${item.label}
                    ${item.value} :undefined,
                    </#list>
                    total: 0,
                    page: 1,
                    size: 50
                }
                await this.search();
            },
            /**
             * 查询
             * @returns {Promise<void>}
             */
            async search() {
                this.listLoading = true
                try {
                    const url = '/${apiName}/list'
                    const res = await sendPostByJson(url, this.serverQuery)
                    if (res && res.code === 200) {
                        this.listData = res.data.list
                        this.serverQuery.total = res.data.total
                    }
                } catch (e) {
                    console.log(e.toString())
                } finally {
                    this.listLoading = false
                }
            },
            /**
             * 点击弹出修改框
             * @param row
             * @param column
             * @param event
             */
            rowClick: function (row, column, event) {
                this.getById(row.productId, true)
            }
        }
    }
</script>
<style scoped>

</style>

java生成执行文件

    public static void main(String[] args) {
//        String generatorInfoExample="{
//        "vueFileUrl": "C:/Users/adminDesk/Documents/webadmin/src/views/test-role",
//                "dropDownInfo": [
//        {
//            "value": "product_id",
//                "label": "product_name",
//                "tableName": "product"
//        },
//        {
//            "value": "product_id",
//                "label": "product_name",
//                "tableName": "product"
//        }
//    ]
//    }";

        String generatorTableOtherInfo="{\n" +
                "    \"vueFileUrl\": \"C:/Users/adminDesk/Documents/jfl-java-seed/webadmin/src/views/test-role\",\n" +
                "    \"dropDownInfo\": [\n" +
                "        {\n" +
                "            \"value\": \"product_id\",\n" +
                "            \"label\": \"product_name\",\n" +
                "            \"tableName\": \"product\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
                   //实体类          表名            其它参数
        genVue2(AnotherTest.class,"another_test",generatorTableOtherInfo);
    }
    private static void genVue2(Class<?> modelClass,String tableName,String generatorTableOtherInfo) {
            genView(tableName,modelClass,generatorTableOtherInfo);
    }

    private static void genView(String tableName,Class<?> modelClass,String generatorTableOtherInfo) {
        try {
            freemarker.template.Configuration cfg = getConfiguration();
            Map<String, Object> data = new HashMap<>();
            List<SearchModel> searchModels = new ArrayList<>();
            String apiName =CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName);
            data.put("date", DATE);
            data.put("apiName",apiName);
            data.put("author", AUTHOR);
            data.put("tableName", tableName);//给vue文件名赋值
            //生成到对应的路径去
            GeneratorOtherInfo info =JSONObject.parseObject(generatorTableOtherInfo, GeneratorOtherInfo.class);
            File file = new File(info.getVueFileUrl()+"/" + tableName + ".vue");
            int lastSlashIndex = info.getVueFileUrl().lastIndexOf('/');
            //获取上级目录example:usr-role
            String parentRole=null;
            if (lastSlashIndex != -1) {
                parentRole = info.getVueFileUrl().substring(lastSlashIndex + 1);
            }
  
            //搜索框赋值
            generatorSearchModels(modelClass,searchModels,info);
            data.put("searchModels",searchModels);
            data.put("id",searchModels.get(0).getValue());
            data.put("modelName",searchModels.get(0).getLabel().replaceAll("id",""));
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            cfg.getTemplate("vue.ftl").process(data, new FileWriter(file));


        }catch (Exception e) {
            throw new RuntimeException("生成Vue2页面失败失败", e);
        }


    }

前端页面需要的参数值,根据需要调整

    private static void generatorSearchModels(Class<?> modelClass,List<SearchModel> searchModels,GeneratorOtherInfo info) {
        Field[] allFields = FieldUtils.getAllFields(modelClass);
        List<Field> fields = Arrays.asList(allFields);
        for (Field field: fields) {
            field.setAccessible(true);
            //通过注解反射获取字段信息
            ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
            SearchModel searchModel = new SearchModel();
            if(field.getType()==Boolean.class){
                searchModel.setCheckState(true);
            }
            if(field.getType()== Date.class){
                searchModel.setDatePicker(true);
            }
            if(CollUtil.isNotEmpty(info.getDropDownInfo())){
                boolean isDropDown = info.getDropDownInfo().stream().anyMatch(item -> CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,item.getValue()).equals(field.getName()));
                if(isDropDown){
                    searchModel.setDropDown(true);
                }
                info.getDropDownInfo().stream().forEach(item->{
                    if((CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, item.getValue())).equals(field.getName())){
                        Dropdown dropdown = new Dropdown();
                        dropdown.setTableName(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, item.getTableName()));
                        dropdown.setValue(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, item.getValue()));
                        dropdown.setLabel(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, item.getLabel()));
                        searchModel.setDropdownInfo(dropdown);
                    }
                });
            }


            //处理下拉框选项属性
            searchModel.setLabel(annotation!=null? annotation.value():"搜索标题");
            searchModel.setValue(field.getName());
            searchModels.add(searchModel);
        }


    }

参数类

public class SearchModel {
    private String label;//标题
    private String value;//值

    private Dropdown dropdownInfo;


    private Boolean datePicker=false;//是否时间字段

    private Boolean checkState=false;//是否判断字段

    private Boolean dropDown=false;//是否下拉字段

    public Dropdown getDropdownInfo() {
        return dropdownInfo;
    }

    public void setDropdownInfo(Dropdown dropdownInfo) {
        this.dropdownInfo = dropdownInfo;
    }

    public Boolean getDropDown() {
        return dropDown;
    }

    public void setDropDown(Boolean dropDown) {
        this.dropDown = dropDown;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Boolean getDatePicker() {
        return datePicker;
    }

    public void setDatePicker(Boolean datePicker) {
        this.datePicker = datePicker;
    }

    public Boolean getCheckState() {
        return checkState;
    }

    public void setCheckState(Boolean checkState) {
        this.checkState = checkState;
    }
}

执行main方法即可生成页面,生成vue文件位置可通过vuefileurl调整.

这个博客主要是提供一个思路,可以更加这个文章进行调整,同时也是一个解放开发生产力的工具

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是的,我可以用mybatisplus+springboot+freemarker模板引擎写一个代码生成器,满足您的需求。 首先,我们需要在pom.xml文件中添加mybatis-plus、freemarker和分页插件的依赖。 ```xml <!-- mybatis-plus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!-- freemarker依赖 --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> <!-- mybatis-plus分页插件依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.4.0</version> </dependency> ``` 然后,我们需要定义一个模板,用于生成代码。模板中可以使用freemarker的语法,通过变量来传递参数。例如,我们可以定义一个controller模板: ```java package ${basePackage}.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import ${basePackage}.entity.${className}; import ${basePackage}.service.${className}Service; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; @RestController @RequestMapping("/${basePath}/${className}") @RequiredArgsConstructor public class ${className}Controller { private final ${className}Service ${lowerClassName}Service; @GetMapping("/{id}") public ${className} getById(@PathVariable Long id) { return ${lowerClassName}Service.getById(id); } @GetMapping public List<${className}> getAll() { return ${lowerClassName}Service.list(); } @GetMapping("/page") public Page<${className}> getPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) String keyword) { return ${lowerClassName}Service.getPage(pageNum, pageSize, keyword); } @PostMapping public void add(@RequestBody @Valid ${className} ${lowerClassName}) { ${lowerClassName}Service.save(${lowerClassName}); } @PutMapping public void update(@RequestBody @Valid ${className} ${lowerClassName}) { ${lowerClassName}Service.updateById(${lowerClassName}); } @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { ${lowerClassName}Service.removeById(id); } } ``` 接下来,我们可以编写一个代码生成器的工具类,来生成代码。该工具类需要实现以下功能: 1. 解析模板文件,获取模板内容。 2. 根据模板内容,替换变量,生成代码文件。 3. 将生成的代码文件写入磁盘。 代码生成器的实现如下: ```java package com.example.codegenerator; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import lombok.RequiredArgsConstructor; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @RequiredArgsConstructor public class CodeGenerator { private final String basePackage; // 基础包名 private final String basePath; // 基础路径 private final String templatePath; // 模板路径 private final String templateName; // 模板名称 private final String className; // 类名 public void generate() throws IOException, TemplateException { // 创建freemarker配置对象 Configuration configuration = new Configuration(Configuration.VERSION_2_3_30); // 设置模板加载路径 configuration.setClassForTemplateLoading(getClass(), "/"); // 设置编码方式 configuration.setDefaultEncoding(StandardCharsets.UTF_8.name()); // 加载模板文件 Template template = configuration.getTemplate(templatePath + "/" + templateName); // 设置模板变量 Map<String, Object> model = new HashMap<>(); model.put("basePackage", basePackage); model.put("basePath", basePath); model.put("className", className); model.put("lowerClassName", className.substring(0, 1).toLowerCase() + className.substring(1)); // 生成代码文件 String code = processTemplate(template, model); writeToFile(basePath + "/" + className + ".java", code); } private String processTemplate(Template template, Map<String, Object> model) throws IOException, TemplateException { StringBuilder sb = new StringBuilder(); template.process(model, new FileWriter(sb)); return sb.toString(); } private void writeToFile(String fileName, String content) throws IOException { File file = new File(fileName); file.getParentFile().mkdirs(); try (FileWriter writer = new FileWriter(file)) { writer.write(content); } } } ``` 最后,我们可以在main方法中调用代码生成器,来生成代码: ```java public static void main(String[] args) throws IOException, TemplateException { // 基础包名 String basePackage = "com.example"; // 基础路径 String basePath = "src/main/java/com/example"; // 模板路径 String templatePath = "templates"; // 模板名称 String templateName = "controller.ftl"; // 类名 String className = "User"; CodeGenerator generator = new CodeGenerator(basePackage, basePath, templatePath, templateName, className); generator.generate(); } ``` 通过以上步骤,我们就可以生成一个UserController。您可以根据需要编写其他模板,生成其他文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值