easyui增删改查
今天我们要实现用easyui来做增删改查:
其中代码如下:
我以前写过通用的增删改查:不知道的可以去看看我以前的博客
现在直接调用方法就好,非常的方便,相比以前的代码量没有这么多
/**
* 通过中间表查询登录用户所对应的权限
* @param paMap
* @param pb
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Map<String, Object>> listMenu(String uid,PageBean pb) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from t_easyui_usermenu where true ";
if(StringUtils.isNotBlank(uid)) {
sql= sql+" and uid = "+uid;
}
return super.executeQuery(sql, pb);
}
/**
* 修改
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int edit(Map<String, String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "update t_easyui_user_version2 set uid=?, uname=?,upwd=? where SerialNo=?";
return super.executeUpdate(sql, new String[] {"uid","uname","upwd","SerialNo"}, paMap);
}
/**
* 删除
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int del(Map<String, String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "delete from t_easyui_user_version2 where serialNo=?";
return super.executeUpdate(sql, new String[] {"SerialNo"}, paMap);
}
/**
* 增加
* @param paMap
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int add(Map<String, String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql = "insert into t_easyui_user_version2 values(?,?,?,?)";
return super.executeUpdate(sql, new String[] {"SerialNo","uid","uname","upwd"}, paMap);
}
}
然后在子控制器种调用方法:
package com.wt.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.wt.dao.UserDao;
import com.wt.util.PageBean;
import com.wt.util.ResponseUtil;
import com.zking.framework.ActionSupport;
public class UserAction extends ActionSupport {
private UserDao ud=new UserDao();
public String login(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
List<Map<String, Object>> list = this.ud.list(req.getParameterMap(), null);
if(list!=null&&list.size()>0) {
List<Map<String, Object>> listMenu = this.ud.listMenu(req.getParameter("uid"), null);
StringBuilder sb=new StringBuilder();
for (Map<String, Object> map : listMenu) {
sb.append(","+map.get("menuId"));
}
//,001,002,.....
req.setAttribute("menuHid", sb.substring(1));
}
else {
return "login";
}
return "index";
}
/**
* datagrid所需数据后端程序员开发完毕
* @param req
* @param resp
* @return
* @throws Exception
*/
public String list(HttpServletRequest req,HttpServletResponse resp) throws Exception {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
List<Map<String, Object>> list = this.ud.list(req.getParameterMap(),pageBean);
ObjectMapper om = new ObjectMapper();
Map<String, Object> map = new HashMap<>();
map.put("total",pageBean.getTotal());
map.put("rows", list);
ResponseUtil.write(resp, om.writeValueAsString(map));
return null;
}
/**
* form组件提交所需数据后端处理完毕
* @param req
* @param resp
* @return
* @throws Exception
*/
public String edit(HttpServletRequest req,HttpServletResponse resp) throws Exception {
int edit = this.ud.edit(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(edit));
return null;
}
/**
* 增加
* @param req
* @param resp
* @return
* @throws Exception
*/
public String add(HttpServletRequest req,HttpServletResponse resp) throws Exception {
int add = this.ud.edit(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(add));
return null;
}
public String del(HttpServletRequest req,HttpServletResponse resp) throws Exception {
int del = this.ud.edit(req.getParameterMap());
ObjectMapper om = new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(del));
return null;
}
}
看看我userManage界面的代码:
<%@ 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">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/easyui5/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/easyui5/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/easyui5/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/userManage.js"></script>
<title>Insert title here</title>
</head>
<body>
<table id="dg"></table>
<input type="hidden" id="ctx" value="${pageContext.request.contextPath}">
<!-- 弹出框提交表单所用 -->
<div id="dd" class="easyui-dialog" title="编辑处理" style="width:600px;height:500px;"
data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#bb'">
<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>
<input type="hidden" id="wt">
<div id="bb">
<a href="#" class="easyui-linkbutton" onclick="ok();">保存</a>
<a href="#" class="easyui-linkbutton">关闭</a>
</div>
<!-- </div>
<div id="tb">
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true"/a>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-help',plain:true"/a>
</div> -->
</form>
</div>
</body>
</html>
最后看看easyui的组件式开发的代码:
js文件:
$(function(){
$('#dg').datagrid({
url:$('#ctx').val()+'/userAction.action?methodName=list',
fitColumns:true, //填充列
fit:true, //填充行
pagination:true, //分页
columns:[[
{field:'uid',title:'代码',width:100},
{field:'uname',title:'名称',width:100},
{field:'upwd',title:'价格',width:100,align:'right'}
]],
toolbar: [{
iconCls: 'icon-edit',
handler: function(){ //修改
var row = $("#dg").datagrid('getSelected');//返回第一个被选中的行或如果没有选中的行则返回null。
if(row){
$('#ff').form('load',row);
$('#dd').dialog('open');
$("#wt").val("edit");
}else{
alert("请先选中行");
}
}
},'-',{
iconCls: 'icon-add',
handler: function(){
$('#ff').form('clear');
$('#dd').dialog('open');
$("#wt").val("add");
}
},'-',{
iconCls: 'icon-remove',
handler: function(){ //删除
var row = $("#dg").datagrid('getSelected');
if(row){
$.ajax({
url:$('#ctx').val()+'/userAction.action?methodName=del&&SerialNo='+row.SerialNo,
success: function(param){
$('#dg').datagrid('reload');
}
})
}
else{
alert("dsfasdf");
}
}
}]
});
})
function ok(){
$('#ff').form('submit',{
url:$('#ctx').val()+'/userAction.action?methodName='+$('#wt').val(),
success: function(param){
$('#dd').dialog('close');
$('#dg').datagrid('reload');
$('#ff').form('clear');
}
});
}
下面的就是我们的数据库:
以上就是本篇博客的内容,要是对你有用就点点关注哟