EasyUi的增删改查

easyui的crud(dialog,datagrid、form讲解)
1、datagrid布局
2、dialog布局
3、form布局
4、通用的JsonBaseDao增删改方法
5、dao层
6、web层
7、功能完善

陈旧的开发模式
美工(ui工程师:出一个项目模型)
java工程师:将原有的html转成jsp,动态展示数据
缺点:
客户需要调节前端的展示效果
解决:由美工去重新排版,重新选色。
Vs
前后端分离
美工、java工程师都是独立工作的,彼此之间在开发过程中是没有任何交际。
在开发前约定数据交互的格式。
java工程师的工作:写方法返回数据如tree_data1.json
美工:只管展示tree_data1.json

我们要操作的表
在这里插入图片描述
然后我们先写前段的代码 其次再写后端代码:
userManage.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- ctrl+shift+r查找本工作区间打开的工程中的文件 -->
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/static/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
	<script type="text/javascript"
	src="${pageContext.request.contextPath }/static/js/userManage.js"></script>
<title>人员信息维护界面</title>
</head>
<body>
<table id="dg"></table>  

<!-- 新增修改对应的编辑窗体 -->
<div id="dd" class="easyui-dialog" title="编辑窗体" style="width:400px;height:200px;"   
        data-options="iconCls:'icon-save',resizable:true,modal:true,buttons:'#bb',closed:true">   
       
<!--        
	自定义正则属性
	$.extend({
	})

 -->
       <form id="ff" method="post">   
       	<input type="hidden" name="SerialNo" />
    <div>   
        <label for="uid">uid:</label>   
        <input class="easyui-validatebox" type="text" name="uid" data-options="required:true" />   
    </div>   
    <div>   
        <label for="uname">uname:</label>   
        <input class="easyui-validatebox" type="text" name="uname" data-options="required:true" />   
    </div>   
    <div>   
        <label for="upwd">upwd:</label>   
        <input class="easyui-validatebox" type="text" name="upwd" data-options="required:true" />   
    </div>   
</form>  
       
</div>  

<div id="bb">
<a href="#" class="easyui-linkbutton" onclick="ok()">保存</a>
<a href="#" class="easyui-linkbutton">关闭</a>
</div>


</body>
</html>

userManage.js:

$(function () {
 $('#dg').datagrid({    
     url:'../userAction.action?methodName=list',
      fit:true,
      fitColumns:true,
      pagination:true,
      singleSelect:true,
       columns:[[    
         {field:'uid',title:'ID',width:100},    
         {field:'uname',title:'用户名',width:100},    
         {field:'upwd',title:'密码',width:100,align:'right'}    
     ]],

toolbar: [{
   iconCls: 'icon-add',
   handler: function(){
    $('#ff').form('clear');//清空文本框的值
       $('#dd').dialog('open');//打开表格
       $("#dd").attr("title","增加用户");//增加信息
       $("#lin").val("add"); //通过隐藏ID来设置增加方法

      }
},'-',{
   iconCls: 'icon-edit',
   handler: function(){
    $('#dd').dialog('open')
    //到datagrid控件中找需要填数据
     var row =  $('#dg').datagrid('getSelected');
      if(row){
      //get_data.php指的是回填的数据
    $('#ff').form('load',row);
   }else{
    alert('请选择你要修改的数据!');
   }
   }   

 },'-',{
  iconCls: 'icon-remove',
  handler: function(){
   var row =$('#dg').datagrid('getSelected');//选择你要删除的行
   if(row){//是否选中
    $.ajax({  

    url:'../userAction.action?methodName=del&&SerialNo='+row.SerialNo  //传一个删除del方法跟serialNo列名值
       });   
    alert('删除成功');
    $('#dg').datagrid('reload');//刷新方法
   }else{
    alert('删除失败');
   }
}

  },'-',{
   iconCls: 'icon-reload',
   handler: function(){alert('刷新按钮')}
 }]
 });
})

function ok() {
  alert('ok');
  $('#ff').form('submit', { 

 url:'../userAction.action?methodName=edit',    
      success:function(data){
       $('#ff').form('clear');
       $('#dd').dialog('close');
        $('#dg').datagrid('reload');
 /*  alert(data)  */
          //针对于后端返回的结果集进行处理
      }    
  });  
  }

前端写完了 现在写后端:
优化Dao方法:

package com.LHJ.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.LHJ.util.JsonBaseDao;
import com.LHJ.util.JsonUtils;
import com.LHJ.util.PageBean;
import com.LHJ.util.StringUtils;

public class UserDao extends JsonBaseDao {
	/**
	 * 用于查询用户分页列表所用
	 * 用于用户登录所用
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<Map<String, Object>> list(Map<String, String[]> map, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from t_easyui_user_version2 where true ";
		String uid = JsonUtils.getParamVal(map, "uid");
		String upwd = JsonUtils.getParamVal(map, "upwd");
		if(StringUtils.isNotBlank(uid)) {
			sql += " and uid = "+uid;
		}
		if(StringUtils.isNotBlank(upwd)) {
			sql += " and upwd = "+upwd;
		}
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * 通过用户登录的唯一账号,在用户父权限中间表中获取菜单ID的集合
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> getMenusByUser(Map<String, String[]> map, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from t_easyui_usermenu where true ";
		String uid = JsonUtils.getParamVal(map, "uid");
		if(StringUtils.isNotBlank(uid)) {
			sql += " and uid = "+uid;
		}
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * 修改
	 * @param map
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 */
	public int edit(Map<String, String[]> map) throws InstantiationException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException{
		String sql = "update t_easyui_user_version2 set uid=?,uname=?,upwd=? where serialno = ?";
		return super.executeUpdate(sql, new String[] {"uid","uname","upwd","SerialNo"}, map);
	}
	/**
	  * 添加
	  * @param paMap
	  * @return
	  * @throws Exception
	  * @throws SecurityException
	  * @throws IllegalArgumentException
	  * @throws IllegalAccessException
	  * @throws SQLException
	  */

	public int add(Map<String, String[]> paMap) throws Exception, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
	  String sql ="insert  into  t_easyui_user_version2  (uid,uname,upwd)  values (?,?,?)";
	  return super.executeUpdate(sql, new String[] {"uid","uname","upwd"} , paMap);
	 }
	
	/**
	  * 删除
	  * @param paMap
	  * @return
	  * @throws NoSuchFieldException
	  * @throws SecurityException
	  * @throws IllegalArgumentException
	  * @throws IllegalAccessException
	  * @throws SQLException
	  */

	public int del(Map<String, String[]> paMap) throws Exception, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
	  String sql ="delete from t_easyui_user_version2 where SerialNo =? ";
	  return super.executeUpdate(sql, new String[] {"SerialNo"} , paMap);
	 }
}

然后是web层的UserAction:

package com.LHJ.web;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.LHJ.dao.UserDao;
import com.LHJ.framework.ActionSupport;
import com.LHJ.util.PageBean;
import com.LHJ.util.ResponseUtil;

public class UserAction extends ActionSupport {
	private UserDao userDao = new UserDao();

	public String login(HttpServletRequest req, HttpServletResponse resp) {
		String code = "index";
		// 登录
		try {
			List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), null);
			if (list != null && list.size() == 1) {
				// 用户存在
				List<Map<String, Object>> menuList = this.userDao.getMenusByUser(req.getParameterMap(), null);
				StringBuilder sb = new StringBuilder();
				for (Map<String, Object> map : menuList) {
					sb.append(","+map.get("menuId"));
				}
//				,001,002
				req.setAttribute("menuIds", sb.substring(1));
			} else {
				// 用户不存在
				req.setAttribute("msg", "用户不存在");
				code = "login";
			}
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
			code = "login";
		}
		return code;

	}
	
	/**
	 * 查询
	 * easyui的datagrid的数据来源
	 * @param req
	 * @param resp
	 * @return
	 */
	public String list(HttpServletRequest req, HttpServletResponse resp) {
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = new PageBean();
		pageBean.setRequest(req);
		try {
			List<Map<String, Object>> list = this.userDao.list(req.getParameterMap(), pageBean);
			map.put("total", pageBean.getTotal());
			map.put("rows", list);
			ObjectMapper om = new ObjectMapper();
			ResponseUtil.write(resp, om.writeValueAsString(map));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 修改
	 * @param req
	 * @param resp
	 * @return
	 */
	public String edit(HttpServletRequest req, HttpServletResponse resp) {
		try {
			this.userDao.edit(req.getParameterMap());
			ObjectMapper om = new ObjectMapper();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("code", 1);
			map.put("msg", "成功");
			ResponseUtil.write(resp, om.writeValueAsString(map));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 增加方法
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public String add(HttpServletRequest req,HttpServletResponse resp) throws Exception{
		int add = this.userDao.add(req.getParameterMap());
		ObjectMapper om = new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(add));
		return null;
	}
	/**
	 * 删除方法
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	public String del(HttpServletRequest req,HttpServletResponse resp) throws Exception{
		int del = this.userDao.del(req.getParameterMap());
		ObjectMapper om = new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(del));
		return null;
	}
}

效果如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值