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