超实用EasyCode模板(手把手教你)_easycode template-程序员宅基地

技术标签: idea的easycode  java  

为了能更快的生成代码,减少项目的开发时间,增加效率,在idea中使用自定义模板技术,来自动生成domain,repository,service(接口与实现),controller,js,jsp,query,

注意

在创建easycode模板时,要将关联的表对象全部删除,以及相应的方法

domain

##引入宏定义
$!define

##使用宏定义设置回调(保存位置与文件后缀)
#save("/domain", ".java")

##使用宏定义设置包后缀
#setPackageSuffix("domain")

##使用全局变量实现默认包导入
$!autoImport
import javax.persistence.*;

##使用宏定义实现类注释信息
#tableComment("实体类")
@Entity
@Table(name="$!{tableInfo.obj.name}")
public class $!{tableInfo.name} extends BaseDomain {

#foreach($column in $tableInfo.otherColumn)
    #if(${column.comment})/**
    * ${column.comment}
    */#end
##遍历数据库中字段,这里的otherColumn是除了主键外的字段
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
##增加get与set方法
#foreach($column in $tableInfo.otherColumn)
##使用宏定义实现get,set方法
    #getSetMethod($column)
#end

}

repository

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Repository"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/repository"))

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

$!autoImport
import cn.xf.domain.$tableInfo.name;
//import cn.xf.domain.Employee;

public interface $!{tableName} extends BaseRepository<$!{tableInfo.name},Long>{}

//public interface EmployeeRepository extends BaseRepository<Employee,Long>{}

query

##定义初始变量(指的是类名)
#set($tableName = $tool.append($tableInfo.name, "Query"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/query"))

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

import cn.xf.domain.$tableInfo.name;
import com.github.wenhao.jpa.Specifications;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

public class $!{tableName} extends BaseQuery{
    private String name;

    //返回咱们的查询条件 where username = ?
    @Override
    public Specification createSpec(){
        Specification<$!{tableInfo.name}> spec = Specifications.<$!{tableInfo.name}>and()
                .like(StringUtils.isNotBlank(name),"name", "%"+name+"%")
                .build();
        return spec;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

service

##定义初始变量
#set($tableName = $tool.append("I",$tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))

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

$!autoImport
import cn.xf.domain.$tableInfo.name;


public interface $!{tableName} extends IBaseService<$!{tableInfo.name},Long>{


}

serviceimpl

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))

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

$!autoImport
import cn.xf.domain.$tableInfo.name;
import cn.xf.repository.$!{tableInfo.name}Repository;
import cn.xf.service.I$!{tableInfo.name}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class $!{tableName} extends BaseServiceImpl<$!{tableInfo.name},Long> implements I$!{tableInfo.name}Service {

    @Autowired
    private $!{tableInfo.name}Repository $!{tableInfo.obj.name}Repository;

}

controller

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/web/controller"))

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

$!autoImport
import cn.xf.domain.$tableInfo.name;
import cn.xf.common.JsonResult;
import cn.xf.common.UIPage;
import cn.xf.domain.$!{tableInfo.name};
import cn.xf.query.$!{tableInfo.name}Query;
import cn.xf.service.I$!{tableInfo.name}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("$!{tableInfo.obj.name}")
public class $!{tableName} extends BaseController{
   @Autowired
    private I$!{tableInfo.name}Service $!{tableInfo.obj.name}Service;


    @RequestMapping("/index")
    public String index(){
        return "$!{tableInfo.obj.name}/$!{tableInfo.obj.name}";
    }


    @RequestMapping("/page")
    @ResponseBody
    public UIPage<$!{tableInfo.name}> page($!{tableInfo.name}Query query){
        Page page = $!{tableInfo.obj.name}Service.findPageByQuery(query);
        return new UIPage(page);
    }


    /**
     *ModelAttribute:路径访问Controller的每个方法,都会先执行它里面的代码
     */
    @ModelAttribute("edit$!{tableInfo.name}")
    public $!{tableInfo.name} beforeEdit(Long id,String cmd){
        if(id!=null && "_update".equals(cmd)){
            //修改才执行这个代码
            $!{tableInfo.name} db$!{tableInfo.name} = $!{tableInfo.obj.name}Service.findOne(id);
            //解决n-to-n的问题,把关联对象设置为null
            
            return  db$!{tableInfo.name};
        }
        return null;
    }

    //添加
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save($!{tableInfo.name} $!{tableInfo.obj.name}){
        try {
            $!{tableInfo.obj.name}Service.save($!{tableInfo.obj.name});
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }
    }
    //修改
    @RequestMapping("/update")
    @ResponseBody
    public JsonResult update(@ModelAttribute("edit$!{tableInfo.name}")$!{tableInfo.name} $!{tableInfo.obj.name}){
        try {
            $!{tableInfo.obj.name}Service.save($!{tableInfo.obj.name});
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }
    }
    /**
     * 删除功能,前台要求返回{success:true/false,msg:xxx}
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            $!{tableInfo.obj.name}Service.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,e.getMessage());
        }
    }

}

jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/7/5
  Time: 10:44
  To change this template use File | Settings | File Templates.
--%>
##设置回调
$!callback.setFileName($tool.append($tableInfo.obj.name, ".jsp"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/main/webapp/WEB-INF/view/${tableInfo.obj.name}"))
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>Title</title>
    <%--引入相应的css与js--%>
    <%@include file="/WEB-INF/views/head.jsp" %>
    <script src="/js/model/$!{tableInfo.obj.name}.js"></script>
</head>
<body>

<%--
    pagination:分页条支持
--%>
<table id="$!{tableInfo.obj.name}Grid" class="easyui-datagrid"
       data-options="url:'/$!{tableInfo.obj.name}/page',fitColumns:true,singleSelect:true,fit:true,pagination:true,toolbar:'#gridTools'">
    <thead>
    <tr>
        #foreach($column in $tableInfo.otherColumn)
        <th data-options="field:'$!{column.name}',width:100">$!{column.name}</th>
        #end
    </tr>
    </thead>
</table>
<%--grid顶部工具栏--%>
<div id="gridTools" style="padding:5px;height:auto">
    <%--功能条--%>
    <div style="margin-bottom:5px">
        <a href="#" data-method="add" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
        <a href="#" data-method="update" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
        <shiro:hasPermission name="$!{tableInfo.obj.name}:delete">
            <a href="#" data-method="del" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
        </shiro:hasPermission>
    </div>
     <%--查询条--%>
    <form id="searchForm" action="/$!{tableInfo.obj.name}/download">
        用户名: <input name="name" class="easyui-textbox" style="width:80px">
        <a href="#" data-method="search" class="easyui-linkbutton" iconCls="icon-search">查询</a>
    </form>
</div>

<%--添加与修改的表单对话框--%>
<div id="editDialog" class="easyui-dialog" title="功能编辑" style="width:400px;"
     data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true">
    <form id="editForm" method="post">
        <input id="$!{tableInfo.obj.name}Id" type="hidden" name="id" />
        <table cellpadding="5">
            #foreach($column in $tableInfo.otherColumn)
                <tr>
                    <td>$!{column.name}:</td>
                    <td><input class="easyui-validatebox" type="text" name="$!{column.name}"
                           data-options="required:true"></input></td>
                </tr>
            #end
        </table>
    </form>
    <div style="text-align:center;padding:5px">
        <a href="javascript:void(0)" class="easyui-linkbutton" data-method="save">提交</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" data-method="closeDialog">关闭</a>
    </div>
</div>

</body>
</html>



js

##设置回调
$!callback.setFileName($tool.append($tableInfo.obj.name, ".js"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/main/webapp/js/model/"))

$(function () {

    //1.获取常用的一些组件
    var $!{tableInfo.obj.name}Grid = $("#$!{tableInfo.obj.name}Grid");
    var searchForm = $("#searchForm");
    var editDialog = $("#editDialog");
    var editForm = $("#editForm");

    //2.绑定相应的事件
    $("*[data-method]").on("click",function(){
        var methodName = $(this).data("method");
        window.xf[methodName]();
    })


    xf={
        //添加
        add(){
            //让密码框失效且隐藏起来
            $("*[data-edit]").show();
            $("*[data-edit] input").validatebox("enable");
            //1.清空form中的数据
            editForm.form("clear");
            //2.打开弹出框(居中)
            editDialog.dialog("center").dialog("open");
        },
        //修改
        update(){
            //1.获取到选中的那一行数据
            let row = $!{tableInfo.obj.name}Grid.datagrid("getSelected");
            //2.如果没有选中,给出提示,后面的代码就不再执行了
            if(!row){
                $.messager.alert('警告','没选中,改个鬼啊!',"warning");
                return ;
            }
            //清空form中的数据
            editForm.form("clear");
            //让密码框失效且隐藏起来
            $("*[data-edit]").hide();
            $("*[data-edit] input").validatebox("disable");

            //把结果进行回显
            /**
             * 部门:<input name="department.id"
             * row:  department.id = 4 ->  "department.id"= 4
             */
             /*   
             if(row.department){
                row["department.id"] = row.department.id;
              }
              */
            editForm.form("load",row);
            //打开弹出框(居中)
            editDialog.dialog("center").dialog("open");

        },
        //保存功能
        save(){
            var url = "/$!{tableInfo.obj.name}/save";
            //获到id的值
            var $!{tableInfo.obj.name}Id = $("#$!{tableInfo.obj.name}Id").val();
            if($!{tableInfo.obj.name}Id){
                url = "/$!{tableInfo.obj.name}/update?cmd=_update";
            }
            //easyui的form提交
            editForm.form('submit', {
                //提交的路径
                url:url,
                //提交之前的操作
                onSubmit: function(){
                    // 做一些检查
                    // 返回false可以阻止提交;
                    return $(this).form('validate');
                },
                //data : {success:true/false,msg:xxx} -> 字符串
                success:function(data){
                   var result = JSON.parse(data);
                   if(result.success){
                       $!{tableInfo.obj.name}Grid.datagrid("reload");
                   }else{
                       $.messager.alert('错误',`失败了,打我啊! 原因是:${result.msg}`,"error");
                   }
                   //关闭弹出框
                    xf.closeDialog();
                }
            });
        },
        //删除
        del(){
            //1.获取到选中的那一行数据
            let row = $!{tableInfo.obj.name}Grid.datagrid("getSelected");
            //2.如果没有选中,给出提示,后面的代码就不再执行了
            if(!row){
                $.messager.alert('警告','没选中,删个鬼啊!',"warning");
                return ;
            }
            //3.如果选中,确定是否要执行删除
            $.messager.confirm('确认','您确认想要删除记录吗?',function(r){
                if (r){
                    //4.如果确定删除,把id传到后台,后台删除数据
                    // jQuery自动帮我们把字符串转成JSON
                    $.get("/$!{tableInfo.obj.name}/delete",{id:row.id},function (result) {
                        //5.后台会返回 {success:true/false,msg:xxx}
                        console.debug(result);
                        //6.后台返回true:刷新数据  / 后台返回false:提示错误信息
                        if(result.success){
                            $!{tableInfo.obj.name}Grid.datagrid("reload");
                        }else{
                            $.messager.alert('错误',`失败了,打我啊! 原因是:${result.msg}`,"error");
                        }
                    })

                }
            });

        },
        //查询
        search(){
            //serializeObject:拿到一个form中的所有数据,封装成json对象
            var params = searchForm.serializeObject();
            $!{tableInfo.obj.name}Grid.datagrid("load",params);
        },
        //关闭窗口
        closeDialog(){
            editDialog.dialog("close");
        }
    }

})
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44353972/article/details/103703878

智能推荐

攻防世界_难度8_happy_puzzle_攻防世界困难模式攻略图文-程序员宅基地

文章浏览阅读645次。这个肯定是末尾的IDAT了,因为IDAT必须要满了才会开始一下个IDAT,这个明显就是末尾的IDAT了。,对应下面的create_head()代码。,对应下面的create_tail()代码。不要考虑爆破,我已经试了一下,太多情况了。题目来源:UNCTF。_攻防世界困难模式攻略图文

达梦数据库的导出(备份)、导入_达梦数据库导入导出-程序员宅基地

文章浏览阅读2.9k次,点赞3次,收藏10次。偶尔会用到,记录、分享。1. 数据库导出1.1 切换到dmdba用户su - dmdba1.2 进入达梦数据库安装路径的bin目录,执行导库操作  导出语句:./dexp cwy_init/[email protected]:5236 file=cwy_init.dmp log=cwy_init_exp.log 注释:   cwy_init/init_123..._达梦数据库导入导出

js引入kindeditor富文本编辑器的使用_kindeditor.js-程序员宅基地

文章浏览阅读1.9k次。1. 在官网上下载KindEditor文件,可以删掉不需要要到的jsp,asp,asp.net和php文件夹。接着把文件夹放到项目文件目录下。2. 修改html文件,在页面引入js文件:<script type="text/javascript" src="./kindeditor/kindeditor-all.js"></script><script type="text/javascript" src="./kindeditor/lang/zh-CN.js"_kindeditor.js

STM32学习过程记录11——基于STM32G431CBU6硬件SPI+DMA的高效WS2812B控制方法-程序员宅基地

文章浏览阅读2.3k次,点赞6次,收藏14次。SPI的详情简介不必赘述。假设我们通过SPI发送0xAA,我们的数据线就会变为10101010,通过修改不同的内容,即可修改SPI中0和1的持续时间。比如0xF0即为前半周期为高电平,后半周期为低电平的状态。在SPI的通信模式中,CPHA配置会影响该实验,下图展示了不同采样位置的SPI时序图[1]。CPOL = 0,CPHA = 1:CLK空闲状态 = 低电平,数据在下降沿采样,并在上升沿移出CPOL = 0,CPHA = 0:CLK空闲状态 = 低电平,数据在上升沿采样,并在下降沿移出。_stm32g431cbu6

计算机网络-数据链路层_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输-程序员宅基地

文章浏览阅读1.2k次,点赞2次,收藏8次。数据链路层习题自测问题1.数据链路(即逻辑链路)与链路(即物理链路)有何区别?“电路接通了”与”数据链路接通了”的区别何在?2.数据链路层中的链路控制包括哪些功能?试讨论数据链路层做成可靠的链路层有哪些优点和缺点。3.网络适配器的作用是什么?网络适配器工作在哪一层?4.数据链路层的三个基本问题(帧定界、透明传输和差错检测)为什么都必须加以解决?5.如果在数据链路层不进行帧定界,会发生什么问题?6.PPP协议的主要特点是什么?为什么PPP不使用帧的编号?PPP适用于什么情况?为什么PPP协议不_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输

软件测试工程师移民加拿大_无证移民,未受过软件工程师的教育(第1部分)-程序员宅基地

文章浏览阅读587次。软件测试工程师移民加拿大 无证移民,未受过软件工程师的教育(第1部分) (Undocumented Immigrant With No Education to Software Engineer(Part 1))Before I start, I want you to please bear with me on the way I write, I have very little gen...

随便推点

Thinkpad X250 secure boot failed 启动失败问题解决_安装完系统提示secureboot failure-程序员宅基地

文章浏览阅读304次。Thinkpad X250笔记本电脑,装的是FreeBSD,进入BIOS修改虚拟化配置(其后可能是误设置了安全开机),保存退出后系统无法启动,显示:secure boot failed ,把自己惊出一身冷汗,因为这台笔记本刚好还没开始做备份.....根据错误提示,到bios里面去找相关配置,在Security里面找到了Secure Boot选项,发现果然被设置为Enabled,将其修改为Disabled ,再开机,终于正常启动了。_安装完系统提示secureboot failure

C++如何做字符串分割(5种方法)_c++ 字符串分割-程序员宅基地

文章浏览阅读10w+次,点赞93次,收藏352次。1、用strtok函数进行字符串分割原型: char *strtok(char *str, const char *delim);功能:分解字符串为一组字符串。参数说明:str为要分解的字符串,delim为分隔符字符串。返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。其它:strtok函数线程不安全,可以使用strtok_r替代。示例://借助strtok实现split#include <string.h>#include <stdio.h&_c++ 字符串分割

2013第四届蓝桥杯 C/C++本科A组 真题答案解析_2013年第四届c a组蓝桥杯省赛真题解答-程序员宅基地

文章浏览阅读2.3k次。1 .高斯日记 大数学家高斯有个好习惯:无论如何都要记日记。他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?高斯出生于:1777年4月30日。在高斯发现的一个重要定理的日记_2013年第四届c a组蓝桥杯省赛真题解答

基于供需算法优化的核极限学习机(KELM)分类算法-程序员宅基地

文章浏览阅读851次,点赞17次,收藏22次。摘要:本文利用供需算法对核极限学习机(KELM)进行优化,并用于分类。

metasploitable2渗透测试_metasploitable2怎么进入-程序员宅基地

文章浏览阅读1.1k次。一、系统弱密码登录1、在kali上执行命令行telnet 192.168.26.1292、Login和password都输入msfadmin3、登录成功,进入系统4、测试如下:二、MySQL弱密码登录:1、在kali上执行mysql –h 192.168.26.129 –u root2、登录成功,进入MySQL系统3、测试效果:三、PostgreSQL弱密码登录1、在Kali上执行psql -h 192.168.26.129 –U post..._metasploitable2怎么进入

Python学习之路:从入门到精通的指南_python人工智能开发从入门到精通pdf-程序员宅基地

文章浏览阅读257次。本文将为初学者提供Python学习的详细指南,从Python的历史、基础语法和数据类型到面向对象编程、模块和库的使用。通过本文,您将能够掌握Python编程的核心概念,为今后的编程学习和实践打下坚实基础。_python人工智能开发从入门到精通pdf

推荐文章

热门文章

相关标签