专三、mcg-helper一键生成jeecg框架单表功能模块
2017-11-22 17:04
274 查看
今天给大家展示mcg-helper的魅力,既然是万能代码生成,那就小试牛刀一下,本次选择java开源快速开发框架jeecg(3.7.1版本)作为示例,一键生成jeecg单表业务模块,达到生成后无需二次编码即可运行的程度。首先,我们来看看mcg-helper流程图的实现:
从流程图中“data控件”读取表结构信息自动转换映射为关系对象,“json控件”扩展数据(如:定义列表页面的有哪些查询条件字段,列表页面表格显示哪些字段,增加页面需要录入的字段,修改页面需要编辑的字段等等),再通过“js脚本控件”进行数据处理, 通常一个模块的业务数据紧密度较高,所以原则上使用一个js脚本控件就能够满足,当然你可以自己来设计,比如多个json控件或多个js脚本控件来将数据处理或划分得更细。“文本控件”获取到参数后,可自定义组装成你需要的代码。
假设一个表结构,如下:
指定生成的目录路径,一键生成后我们直接把jeecg运行起来,实际效果如下图!
查询、新增、修改、详情、删除、分页功能均可以正常使用,在生成流程中定义哪些列表查询条件字段,表格显示哪些字段,新增显示哪些字段,修改显示哪些字段等等。当然只要你想做,实质上可以做得更细,更到位。单表业务生成相当简单,多表原理也一样。只要有标准、有规范、有规律可循,我们就能生成,越复杂麻烦的功能模块反而更值得去实现自动生成,假如一键生成后,这功能可以减少三天或以上的工作量,那就更值得拥有。
我们来看看生成的代码(这里贴出几个生成后的完整代码文件):
从代码生成实现的角度上,我相信优化的空间还很大,我们能够不断的摸索提炼,让代码生成的实现设计更简单,而且能够支持更复杂的需求。以后要是换公司或换研发框架,针对业务编写几套自动生成,虽然谈不上高枕无忧,但也会轻松不少。为让大家更好的认识和使用mcg-helper,现推出更具营养的“使用指南”视频教程,https://pan.baidu.com/s/1sliEqhJ#list/path=%2F%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97 ,
感谢大家的支持!最后贴上一份百度云盘的分享截图:
从流程图中“data控件”读取表结构信息自动转换映射为关系对象,“json控件”扩展数据(如:定义列表页面的有哪些查询条件字段,列表页面表格显示哪些字段,增加页面需要录入的字段,修改页面需要编辑的字段等等),再通过“js脚本控件”进行数据处理, 通常一个模块的业务数据紧密度较高,所以原则上使用一个js脚本控件就能够满足,当然你可以自己来设计,比如多个json控件或多个js脚本控件来将数据处理或划分得更细。“文本控件”获取到参数后,可自定义组装成你需要的代码。
假设一个表结构,如下:
CREATE TABLE `mcg_helper_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键', `user_name` varchar(50) NOT NULL COMMENT '用户名', `user_pwd` varchar(255) DEFAULT NULL COMMENT '密码', `age` int(11) DEFAULT NULL COMMENT '年龄', `reg_time` datetime DEFAULT NULL COMMENT '注册时间', `update_time` timestamp NULL DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`,`user_name`) ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
指定生成的目录路径,一键生成后我们直接把jeecg运行起来,实际效果如下图!
查询、新增、修改、详情、删除、分页功能均可以正常使用,在生成流程中定义哪些列表查询条件字段,表格显示哪些字段,新增显示哪些字段,修改显示哪些字段等等。当然只要你想做,实质上可以做得更细,更到位。单表业务生成相当简单,多表原理也一样。只要有标准、有规范、有规律可循,我们就能生成,越复杂麻烦的功能模块反而更值得去实现自动生成,假如一键生成后,这功能可以减少三天或以上的工作量,那就更值得拥有。
我们来看看生成的代码(这里贴出几个生成后的完整代码文件):
package com.jeecg.mcghelperuser.entity; import java.io.Serializable; import java.util.Date; /** * * @Description: 业务表基本信息 * @author: mcg-helper * @date: 2017-11-21 17:49:08 * */ public class McgHelperUserEntity implements Serializable { private static final long serialVersionUID = 1L; /* 自增主键 */ private Integer id; /* 用户名 */ private String userName; /* 密码 */ private String userPwd; /* 年龄 */ private Integer age; /* 注册时间 */ private Date regTime; /* 更新时间 */ private Date updateTime; public Integer getId() { return this.id; } public String getUserName() { return this.userName; } public String getUserPwd() { return this.userPwd; } public Integer getAge() { return this.age; } public Date getRegTime() { return this.regTime; } public Date getUpdateTime() { return this.updateTime; } public void setId(Integer id) { this.id = id; } public void setUserName(String userName) { this.userName = userName; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public void setAge(Integer age) { this.age = age; } public void setRegTime(Date regTime) { this.regTime = regTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }
package com.jeecg.mcghelperuser.dao; import org.jeecgframework.minidao.annotation.Param; import org.jeecgframework.minidao.annotation.ResultType; import org.jeecgframework.minidao.annotation.Sql; import org.jeecgframework.minidao.pojo.MiniDaoPage; import org.springframework.stereotype.Repository; import com.jeecg.mcghelperuser.entity.McgHelperUserEntity; @Repository public interface McgHelperUserDao{ @Sql("SELECT * FROM mcg_helper_user WHERE id = :id and user_name = :userName") McgHelperUserEntity get(@Param("id") Integer id, @Param("userName") String userName); int update(@Param("mcgHelperUserEntity") McgHelperUserEntity mcgHelperUserEntity); void insert(@Param("mcgHelperUserEntity") McgHelperUserEntity mcgHelperUserEntity); @ResultType(McgHelperUserEntity.class) public MiniDaoPage<McgHelperUserEntity> getAll(@Param("mcgHelperUserEntity") McgHelperUserEntity mcgHelperUserEntity, @Param("page") int page, @Param("rows") int rows); @Sql("DELETE from mcg_helper_user WHERE id = :mcgHelperUserEntity.id and user_name = :mcgHelperUserEntity.userName") public void delete(@Param("mcgHelperUserEntity") McgHelperUserEntity mcgHelperUserEntity); }
package com.jeecg.mcghelperuser.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.velocity.VelocityContext; import org.jeecgframework.minidao.pojo.MiniDaoPage; import org.jeecgframework.p3.core.common.utils.AjaxJson; import org.jeecgframework.p3.core.page.SystemTools; import org.jeecgframework.p3.core.util.plugin.ViewVelocity; import org.jeecgframework.p3.core.web.BaseController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.jeecg.mcghelperuser.entity.McgHelperUserEntity; import com.jeecg.mcghelperuser.service.McgHelperUserService; @Controller @RequestMapping("/demo/mcgHelperUserController") public class McgHelperUserController extends BaseController { @Autowired private McgHelperUserService mcgHelperUserService; @RequestMapping(params = "list", method = { RequestMethod.GET, RequestMethod.POST }) public void list(@ModelAttribute McgHelperUserEntity mcgHelperUserEntity, HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo, @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception { try { // 分页数据 MiniDaoPage<McgHelperUserEntity> list = mcgHelperUserService.getAll(mcgHelperUserEntity, pageNo, pageSize); VelocityContext velocityContext = new VelocityContext(); velocityContext.put("mcgHelperUserEntity", mcgHelperUserEntity); velocityContext.put("pageInfos", SystemTools.convertPaginatedList(list)); String viewName = "demo/mcghelperuser/mcghelperuser-list.vm"; ViewVelocity.view(request, response, viewName, velocityContext); } catch (Exception e) { e.printStackTrace(); } } @RequestMapping(params = "toDetail", method = RequestMethod.GET) public void mcgHelperUserEntityDetail(@RequestParam(required = true, value = "id") Integer id, @RequestParam(required = true, value = "userName") String userName, HttpServletResponse response, HttpServletRequest request) throws Exception { VelocityContext velocityContext = new VelocityContext(); String viewName = "demo/mcghelperuser/mcghelperuser-detail.vm"; McgHelperUserEntity mcgHelperUserEntity = mcgHelperUserService.get(id, userName); velocityContext.put("mcgHelperUserEntity", mcgHelperUserEntity); ViewVelocity.view(request, response, viewName, velocityContext); } @RequestMapping(params = "toAdd", method = { RequestMethod.GET, RequestMethod.POST }) public void toAddDialog(HttpServletRequest request, HttpServletResponse response) throws Exception { VelocityContext velocityContext = new VelocityContext(); String viewName = "demo/mcghelperuser/mcghelperuser-add.vm"; ViewVelocity.view(request, response, viewName, velocityContext); } @RequestMapping(params = "doAdd", method = { RequestMethod.GET, RequestMethod.POST }) @ResponseBody public AjaxJson doAdd(@ModelAttribute McgHelperUserEntity mcgHelperUserEntity) { AjaxJson j = new AjaxJson(); try { mcgHelperUserService.insert(mcgHelperUserEntity); j.setMsg("保存成功"); } catch (Exception e) { log.info(e.getMessage()); j.setSuccess(false); j.setMsg("保存失败"); } return j; } @RequestMapping(params = "toEdit", method = RequestMethod.GET) public void toEdit(@RequestParam(required = true, value = "id") Integer id, @RequestParam(required = true, value = "userName") String userName, HttpServletResponse response, HttpServletRequest request) throws Exception { VelocityContext velocityContext = new VelocityContext(); McgHelperUserEntity mcgHelperUserEntity = mcgHelperUserService.get(id, userName); velocityContext.put("mcgHelperUserEntity", mcgHelperUserEntity); String viewName = "demo/mcghelperuser/mcghelperuser-edit.vm"; ViewVelocity.view(request, response, viewName, velocityContext); } @RequestMapping(params = "doEdit", method = { RequestMethod.GET, RequestMethod.POST }) @ResponseBody public AjaxJson doEdit(@ModelAttribute McgHelperUserEntity mcgHelperUserEntity) { AjaxJson j = new AjaxJson(); try { mcgHelperUserService.update(mcgHelperUserEntity); j.setMsg("编辑成功"); } catch (Exception e) { log.info(e.getMessage()); j.setSuccess(false); j.setMsg("编辑失败"); } return j; } @RequestMapping(params = "doDelete", method = RequestMethod.GET) @ResponseBody public AjaxJson doDelete(@RequestParam(required = true, value = "id") Integer id, @RequestParam(required = true, value = "userName") String userName) { AjaxJson j = new AjaxJson(); try { McgHelperUserEntity mcgHelperUserEntity = new McgHelperUserEntity(); mcgHelperUserEntity.setId(id); mcgHelperUserEntity.setUserName(userName); mcgHelperUserService.delete(mcgHelperUserEntity); j.setMsg("删除成功"); } catch (Exception e) { log.info(e.getMessage()); j.setSuccess(false); j.setMsg("删除失败"); } return j; } }
#parse("content/base/back/common/macro.vm") <!DOCTYPE html> <html lang="en"> #parse("content/base/back/common/head.vm") <link rel="stylesheet" href="$!{basePath}/plug-in-ui/hplus/css/font-awesome.css"/> <body style='overflow:scroll;overflow-x:hidden'> <div class="container bs-docs-container" style="width:100%;"> <div class="row"> <form role="form" class="form-inline" action="$!{basePath}/demo/mcgHelperUserController.do?list" method="post" id="formSubmit"> <div class="col-md-10" style="width:100%"> <div class="panel panel-default"> <div class="panel-heading">列表</div> <div class="panel-body"> <div class="search"> <div class="form-group col-sm-3"> <label for="name" class="control-label col-sm-3 line34">用户名</label> <div class="col-sm-8"> <input type="text" name="userName" id="userName" value="$!{mcgHelperUserEntity.userName}" class="form-control"> </div> </div> <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> 搜 索</button> <div class="clearfix"></div> </div> <div id="legend"> <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> </div> <table class="table table-striped"> <thead> <th> <input type="checkbox" name="ckAll" id="ckAll" /> </th> <th>用户名</th> <th>密码</th> <th>注册时间</th> <th>操作</th> </thead> <tbody> #if($!{pageInfos}) #foreach($!{info} in $!{pageInfos}) <tr> <td><input type="checkbox" name="ck" value="$!{info.id}" /></td> <td>$!{info.userName}</td> <td>$!{info.userPwd}</td> <td>$!{info.regTime}</td> <td class="last"> <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> <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> <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> </td> </tr> #end #end </tbody> </table> <div class="text-right"> <!--公用翻页代码--> #set($attr='formSubmit') #showPageList($pageInfos $attr) <!--END公用翻页代码--> </div> </div> </div> </div> </form> </div> </div> </body> </html> <script> $("#ckAll").click(function(){ if($(this).prop("checked")){ $(":checkbox").prop("checked",true) }else{ $(":checkbox").prop("checked",false) } }); //jquery获取复选框值 function getCkValue(){ var chk_value =[]; $('input[name="ck"]:checked').each(function(){ chk_value.push($(this).val()); }); //alert(chk_value.length==0 ?'你还没有选择任何内容!':chk_value); } </script>
从代码生成实现的角度上,我相信优化的空间还很大,我们能够不断的摸索提炼,让代码生成的实现设计更简单,而且能够支持更复杂的需求。以后要是换公司或换研发框架,针对业务编写几套自动生成,虽然谈不上高枕无忧,但也会轻松不少。为让大家更好的认识和使用mcg-helper,现推出更具营养的“使用指南”视频教程,https://pan.baidu.com/s/1sliEqhJ#list/path=%2F%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97 ,
感谢大家的支持!最后贴上一份百度云盘的分享截图:
相关文章推荐
- 专三、mcg-helper一键生成jeecg框架单表功能模块
- JEECG J2EE敏捷开发框架(代码一键生成)
- 十二、mcg-helper业务系统单表业务模块自动化生成model类
- 十三、mcg-helper业务系统单表业务模块自动化生成xml映射文件
- 十四、mcg-helper业务系统单表业务模块自动化生成dao接口
- 十五、mcg-helper业务系统单表业务模块自动化生成service接口
- 十六、mcg-helper业务系统单表业务模块自动化生成serviceImpl实现类
- 十七、mcg-helper业务系统单表业务模块自动化生成controller控制类
- Jeecg 套用框架生成的Excel导出功能
- 十八、mcg-helper业务系统单表业务模块自动化生成html页面
- 十九、mcg-helper业务系统单表业务模块自动化生成生成js文件
- RDIFramework.NET — 基于.NET的快速信息化系统开发框架- 5.4平台日志、异常管理、生成自动升级配置文件模块
- RDIFramework.NET — 基于.NET的快速信息化系统开发框架- 5.4平台日志、异常管理、生成自动升级配置文件模块
- [L-php]PHP功能模块——生成XML
- django框架模块功能初步了解
- 文件一键上传、汉字转拼音、excel文件上传下载功能模块的实现
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->新增模块管理界面导出功能(可按条件导出)
- Winform开发框架中的内容及文档管理模块功能介绍
- 网页上生成报表模块的功能
- Android Studio插件之MVPHelper,一键生成MVP代码