专三、mcg-helper一键生成jeecg框架单表功能模块

来源:互联网 发布:润网数据 编辑:程序博客网 时间:2024/05/17 01:54
今天给大家展示mcg-helper的魅力,既然是万能代码生成,那就小试牛刀一下,本次选择java开源快速开发框架jeecg(3.7.1版本)作为示例,一键生成jeecg单表业务模块,达到生成后无需二次编码即可运行的程度。首先,我们来看看mcg-helper流程图的实现:

        从流程图中“data控件”读取表结构信息自动转换映射为关系对象,“json控件”扩展数据(如:定义列表页面的有哪些查询条件字段,列表页面表格显示哪些字段,增加页面需要录入的字段,修改页面需要编辑的字段等等),再通过“js脚本控件”进行数据处理, 通常一个模块的业务数据紧密度较高,所以原则上使用一个js脚本控件就能够满足,当然你可以自己来设计,比如多个json控件或多个js脚本控件来将数据处理或划分得更细。“文本控件”获取到参数后,可自定义组装成你需要的代码。

        假设一个表结构,如下:

[sql] view plain copy
  1. CREATE TABLE `mcg_helper_user` (  
  2.   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',  
  3.   `user_name` varchar(50) NOT NULL COMMENT '用户名',  
  4.   `user_pwd` varchar(255) DEFAULT NULL COMMENT '密码',  
  5.   `age` int(11) DEFAULT NULL COMMENT '年龄',  
  6.   `reg_time` datetime DEFAULT NULL COMMENT '注册时间',  
  7.   `update_time` timestamp NULL DEFAULT NULL COMMENT '更新时间',  
  8.   PRIMARY KEY (`id`,`user_name`)  
  9. ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;  


        指定生成的目录路径,一键生成后我们直接把jeecg运行起来,实际效果如下图!



        查询、新增、修改、详情、删除、分页功能均可以正常使用,在生成流程中定义哪些列表查询条件字段,表格显示哪些字段,新增显示哪些字段,修改显示哪些字段等等。当然只要你想做,实质上可以做得更细,更到位。单表业务生成相当简单,多表原理也一样。只要有标准、有规范、有规律可循,我们就能生成,越复杂麻烦的功能模块反而更值得去实现自动生成,假如一键生成后,这功能可以减少三天或以上的工作量,那就更值得拥有。

        

        我们来看看生成的代码(这里贴出几个生成后的完整代码文件):

                              


[java] view plain copy
  1. package com.jeecg.mcghelperuser.entity;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. /** 
  7.  *  
  8.  * @Description: 业务表基本信息 
  9.  * @author:      mcg-helper 
  10.  * @date:        2017-11-21 17:49:08 
  11.  * 
  12.  */  
  13. public class McgHelperUserEntity implements Serializable {  
  14.       
  15.     private static final long serialVersionUID = 1L;  
  16.     /* 自增主键  */  
  17.     private Integer id;  
  18.     /* 用户名  */  
  19.     private String userName;  
  20.     /* 密码  */  
  21.     private String userPwd;  
  22.     /* 年龄  */  
  23.     private Integer age;  
  24.     /* 注册时间  */  
  25.     private Date regTime;  
  26.     /* 更新时间  */  
  27.     private Date updateTime;  
  28.       
  29.     public Integer getId() {  
  30.         return this.id;  
  31.     }  
  32.     public String getUserName() {  
  33.         return this.userName;  
  34.     }  
  35.     public String getUserPwd() {  
  36.         return this.userPwd;  
  37.     }  
  38.     public Integer getAge() {  
  39.         return this.age;  
  40.     }  
  41.     public Date getRegTime() {  
  42.         return this.regTime;  
  43.     }  
  44.     public Date getUpdateTime() {  
  45.         return this.updateTime;  
  46.     }  
  47.      
  48.     public void setId(Integer id) {  
  49.         this.id = id;  
  50.     }  
  51.     public void setUserName(String userName) {  
  52.         this.userName = userName;  
  53.     }  
  54.     public void setUserPwd(String userPwd) {  
  55.         this.userPwd = userPwd;  
  56.     }  
  57.     public void setAge(Integer age) {  
  58.         this.age = age;  
  59.     }  
  60.     public void setRegTime(Date regTime) {  
  61.         this.regTime = regTime;  
  62.     }  
  63.     public void setUpdateTime(Date updateTime) {  
  64.         this.updateTime = updateTime;  
  65.     }  
  66.     
  67. }  

 
[java] view plain copy
  1. package com.jeecg.mcghelperuser.dao;  
  2.   
  3. import org.jeecgframework.minidao.annotation.Param;  
  4. import org.jeecgframework.minidao.annotation.ResultType;  
  5. import org.jeecgframework.minidao.annotation.Sql;  
  6. import org.jeecgframework.minidao.pojo.MiniDaoPage;  
  7. import org.springframework.stereotype.Repository;  
  8.   
  9. import com.jeecg.mcghelperuser.entity.McgHelperUserEntity;  
  10.   
  11. @Repository  
  12. public interface McgHelperUserDao{  
  13.   
  14.     @Sql("SELECT * FROM mcg_helper_user WHERE id = :id and user_name = :userName")  
  15.     McgHelperUserEntity get(@Param("id") Integer id, @Param("userName") String userName);  
  16.       
  17.     int update(@Param("mcgHelperUserEntity") McgHelperUserEntity mcgHelperUserEntity);  
  18.       
  19.     void insert(@Param("mcgHelperUserEntity") McgHelperUserEntity mcgHelperUserEntity);  
  20.       
  21.     @ResultType(McgHelperUserEntity.class)  
  22.     public MiniDaoPage<McgHelperUserEntity> getAll(@Param("mcgHelperUserEntity") McgHelperUserEntity mcgHelperUserEntity, @Param("page"int page, @Param("rows"int rows);  
  23.       
  24.     @Sql("DELETE from mcg_helper_user WHERE id = :mcgHelperUserEntity.id and user_name = :mcgHelperUserEntity.userName")  
  25.     public void delete(@Param("mcgHelperUserEntity") McgHelperUserEntity mcgHelperUserEntity);  
  26.       
  27. }  

[java] view plain copy
  1. package com.jeecg.mcghelperuser.web;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5. import org.apache.velocity.VelocityContext;  
  6. import org.jeecgframework.minidao.pojo.MiniDaoPage;  
  7. import org.jeecgframework.p3.core.common.utils.AjaxJson;  
  8. import org.jeecgframework.p3.core.page.SystemTools;  
  9. import org.jeecgframework.p3.core.util.plugin.ViewVelocity;  
  10. import org.jeecgframework.p3.core.web.BaseController;  
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12. import org.springframework.stereotype.Controller;  
  13. import org.springframework.web.bind.annotation.ModelAttribute;  
  14. import org.springframework.web.bind.annotation.RequestMapping;  
  15. import org.springframework.web.bind.annotation.RequestMethod;  
  16. import org.springframework.web.bind.annotation.RequestParam;  
  17. import org.springframework.web.bind.annotation.ResponseBody;  
  18.   
  19. import com.jeecg.mcghelperuser.entity.McgHelperUserEntity;  
  20. import com.jeecg.mcghelperuser.service.McgHelperUserService;  
  21.   
  22. @Controller  
  23. @RequestMapping("/demo/mcgHelperUserController")  
  24. public class McgHelperUserController extends BaseController {  
  25.     @Autowired  
  26.     private McgHelperUserService mcgHelperUserService;  
  27.   
  28.     @RequestMapping(params = "list", method = { RequestMethod.GET, RequestMethod.POST })  
  29.     public void list(@ModelAttribute McgHelperUserEntity mcgHelperUserEntity, HttpServletRequest request, HttpServletResponse response,  
  30.             @RequestParam(required = false, value = "pageNo", defaultValue = "1"int pageNo, @RequestParam(required = false, value = "pageSize", defaultValue = "10"int pageSize)  
  31.             throws Exception {  
  32.         try {  
  33.             // 分页数据  
  34.             MiniDaoPage<McgHelperUserEntity> list = mcgHelperUserService.getAll(mcgHelperUserEntity, pageNo, pageSize);  
  35.             VelocityContext velocityContext = new VelocityContext();  
  36.             velocityContext.put("mcgHelperUserEntity", mcgHelperUserEntity);  
  37.             velocityContext.put("pageInfos", SystemTools.convertPaginatedList(list));  
  38.             String viewName = "demo/mcghelperuser/mcghelperuser-list.vm";  
  39.             ViewVelocity.view(request, response, viewName, velocityContext);  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.     }  
  44.   
  45.     @RequestMapping(params = "toDetail", method = RequestMethod.GET)  
  46.     public void mcgHelperUserEntityDetail(@RequestParam(required = true, value = "id") Integer id, @RequestParam(required = true, value = "userName") String userName, HttpServletResponse response, HttpServletRequest request) throws Exception {  
  47.         VelocityContext velocityContext = new VelocityContext();  
  48.         String viewName = "demo/mcghelperuser/mcghelperuser-detail.vm";  
  49.         McgHelperUserEntity mcgHelperUserEntity = mcgHelperUserService.get(id, userName);  
  50.         velocityContext.put("mcgHelperUserEntity", mcgHelperUserEntity);  
  51.         ViewVelocity.view(request, response, viewName, velocityContext);  
  52.     }  
  53.   
  54.     @RequestMapping(params = "toAdd", method = { RequestMethod.GET, RequestMethod.POST })  
  55.     public void toAddDialog(HttpServletRequest request, HttpServletResponse response) throws Exception {  
  56.         VelocityContext velocityContext = new VelocityContext();  
  57.         String viewName = "demo/mcghelperuser/mcghelperuser-add.vm";  
  58.         ViewVelocity.view(request, response, viewName, velocityContext);  
  59.     }  
  60.   
  61.     @RequestMapping(params = "doAdd", method = { RequestMethod.GET, RequestMethod.POST })  
  62.     @ResponseBody  
  63.     public AjaxJson doAdd(@ModelAttribute McgHelperUserEntity mcgHelperUserEntity) {  
  64.         AjaxJson j = new AjaxJson();  
  65.         try {  
  66.             mcgHelperUserService.insert(mcgHelperUserEntity);  
  67.             j.setMsg("保存成功");  
  68.         } catch (Exception e) {  
  69.             log.info(e.getMessage());  
  70.             j.setSuccess(false);  
  71.             j.setMsg("保存失败");  
  72.         }  
  73.         return j;  
  74.     }  
  75.   
  76.     @RequestMapping(params = "toEdit", method = RequestMethod.GET)  
  77.       
  78.     public void toEdit(@RequestParam(required = true, value = "id") Integer id, @RequestParam(required = true, value = "userName") String userName, HttpServletResponse response, HttpServletRequest request) throws Exception {  
  79.         VelocityContext velocityContext = new VelocityContext();  
  80.         McgHelperUserEntity mcgHelperUserEntity = mcgHelperUserService.get(id, userName);  
  81.         velocityContext.put("mcgHelperUserEntity", mcgHelperUserEntity);  
  82.         String viewName = "demo/mcghelperuser/mcghelperuser-edit.vm";  
  83.         ViewVelocity.view(request, response, viewName, velocityContext);  
  84.     }  
  85.   
  86.     @RequestMapping(params = "doEdit", method = { RequestMethod.GET, RequestMethod.POST })  
  87.     @ResponseBody  
  88.     public AjaxJson doEdit(@ModelAttribute McgHelperUserEntity mcgHelperUserEntity) {  
  89.         AjaxJson j = new AjaxJson();  
  90.         try {  
  91.             mcgHelperUserService.update(mcgHelperUserEntity);  
  92.             j.setMsg("编辑成功");  
  93.         } catch (Exception e) {  
  94.             log.info(e.getMessage());  
  95.             j.setSuccess(false);  
  96.             j.setMsg("编辑失败");  
  97.         }  
  98.         return j;  
  99.     }  
  100.   
  101.     @RequestMapping(params = "doDelete", method = RequestMethod.GET)  
  102.     @ResponseBody  
  103.     public AjaxJson doDelete(@RequestParam(required = true, value = "id") Integer id, @RequestParam(required = true, value = "userName") String userName) {  
  104.         AjaxJson j = new AjaxJson();  
  105.         try {  
  106.             McgHelperUserEntity mcgHelperUserEntity = new McgHelperUserEntity();  
  107.             mcgHelperUserEntity.setId(id);  
  108.             mcgHelperUserEntity.setUserName(userName);  
  109.                
  110.             mcgHelperUserService.delete(mcgHelperUserEntity);  
  111.             j.setMsg("删除成功");  
  112.         } catch (Exception e) {  
  113.             log.info(e.getMessage());  
  114.             j.setSuccess(false);  
  115.             j.setMsg("删除失败");  
  116.         }  
  117.         return j;  
  118.     }  
  119.   
  120. }  

[html] view plain copy
  1. #parse("content/base/back/common/macro.vm")  
  2. <!DOCTYPE html>  
  3. <html lang="en">  
  4. #parse("content/base/back/common/head.vm")  
  5. <link rel="stylesheet" href="$!{basePath}/plug-in-ui/hplus/css/font-awesome.css"/>  
  6. <body style='overflow:scroll;overflow-x:hidden'>  
  7.     <div class="container bs-docs-container" style="width:100%;">  
  8.         <div class="row">  
  9.             <form role="form" class="form-inline" action="$!{basePath}/demo/mcgHelperUserController.do?list" method="post" id="formSubmit">  
  10.                 <div  class="col-md-10" style="width:100%">  
  11.                     <div class="panel panel-default">  
  12.                         <div class="panel-heading">列表</div>  
  13.                         <div class="panel-body">  
  14.                             <div class="search">  
  15.                               
  16.                                  <div class="form-group col-sm-3">  
  17.                                     <label for="name" class="control-label col-sm-3 line34">用户名</label>  
  18.                                     <div class="col-sm-8">  
  19.                                         <input type="text" name="userName" id="userName" value="$!{mcgHelperUserEntity.userName}" class="form-control">  
  20.                                     </div>  
  21.                                  </div>  
  22.                                            
  23.                                 <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> 搜  索</button>  
  24.                                 <div class="clearfix"></div>  
  25.                             </div>  
  26.                             <div id="legend">  
  27.                                 <legend  class="le"><button type="button" class="btn btn-primary" onclick="doUrl('$!{basePath}/demo/mcgHelperUserController.do?toAdd')" ><i class="fa fa-plus"></i> 新增</button></legend>   
  28.                             </div>  
  29.                             <table class="table table-striped">  
  30.                                 <thead>  
  31.                                     <th>  
  32.                                      <input type="checkbox" name="ckAll" id="ckAll" />  
  33.                                     </th>  
  34.                                         <th>用户名</th>  
  35.                                         <th>密码</th>  
  36.                                         <th>注册时间</th>  
  37.                                     <th>操作</th>  
  38.                                 </thead>  
  39.                                 <tbody>  
  40.                                 #if($!{pageInfos})  
  41.                                     #foreach($!{info} in $!{pageInfos})  
  42.                                         <tr>            
  43.                                             <td><input type="checkbox" name="ck" value="$!{info.id}" /></td>  
  44.                                             <td>$!{info.userName}</td>              
  45.                                             <td>$!{info.userPwd}</td>               
  46.                                             <td>$!{info.regTime}</td>               
  47.                                             <td class="last">  
  48.                                             <a class="btn btn-success btn-xs" href="javascript:doUrl('$!{basePath}/demo/mcgHelperUserController.do?toEdit&id=$!{info.id}&userName=$!{info.userName}')" ><i class="fa fa-edit"></i> 编辑</a>  
  49.                                             <a class="btn btn-danger btn-xs" href="javascript:delData('$!{basePath}/demo/mcgHelperUserController.do?doDelete&id=$!{info.id}&userName=$!{info.userName}')"><i class="fa fa-trash-o"></i> 删除</a>  
  50.                                             <a class="btn btn-success btn-xs" href="javascript:doUrl('$!{basePath}/demo/mcgHelperUserController.do?toDetail&id=$!{info.id}&userName=$!{info.userName}')"><i class="fa fa-file-o"></i> 详情</a>  
  51.                                             </td>  
  52.                                         </tr>  
  53.                                      #end  
  54.                                  #end  
  55.                                 </tbody>  
  56.                             </table>  
  57.                             <div class="text-right">  
  58.                                 <!--公用翻页代码-->  
  59.                                 #set($attr='formSubmit')  
  60.                                 #showPageList($pageInfos $attr)  
  61.                                 <!--END公用翻页代码-->  
  62.                             </div>  
  63.                         </div>  
  64.                     </div>  
  65.                 </div>    
  66.             </form>  
  67.         </div>  
  68.     </div>  
  69. </body>  
  70. </html>  
  71. <script>  
  72. $("#ckAll").click(function(){   
  73.     if($(this).prop("checked")){   
  74.      $(":checkbox").prop("checked",true)   
  75.     }else{   
  76.     $(":checkbox").prop("checked",false)    
  77.     }   
  78. });   
  79.   
  80. //jquery获取复选框值      
  81. function getCkValue(){    
  82.   var chk_value =[];      
  83.   $('input[name="ck"]:checked').each(function(){      
  84.    chk_value.push($(this).val());      
  85.   });      
  86.   //alert(chk_value.length==0 ?'你还没有选择任何内容!':chk_value);      
  87. }      
  88.   
  89. </script>  
       

        从代码生成实现的角度上,我相信优化的空间还很大,我们能够不断的摸索提炼,让代码生成的实现设计更简单,而且能够支持更复杂的需求。以后要是换公司或换研发框架,针对业务编写几套自动生成,虽然谈不上高枕无忧,但也会轻松不少。为让大家更好的认识和使用mcg-helper,现推出更具营养的“使用指南”视频教程,https://pan.baidu.com/s/1sliEqhJ#list/path=%2F%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97 , 感谢大家的支持!最后贴上一份百度云盘的分享截图:




版权声明:欢迎大家交流探讨,QQ群:619815829,软件下载:https://pan.baidu.com/s/1bOe1mY#list/path=%2Fmcg-helper,使用指南:https://pan.baidu.com/s/1sliEqhJ#list/path=%2F%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97
阅读全文
0 0
原创粉丝点击