easyui实现增删查改
在实现增删查改之前,我们先要完成前端的布局,需要使用到easyui的datagrid,dialog,form这三个布局。
roleInfo.jsp
<body>
<!-- 展示数据 -->
<table id="dg"></table>
<input type="hidden" id="ctx" value="${pageContext.request.contextPath}">
<input type="hidden" id="shl" value="11">
<!-- 弹出框提交表单 -->
<div id="dd" class="easyui-dialog" title="编辑窗体" style="width:400px;height:200px;"
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>
<div id="bb">
<a href="#" class="easyui-linkbutton" onclick="ok();">保存</a>
<a href="#" class="easyui-linkbutton">关闭</a>
</div>
</body>
roleInfo.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");
if(row){
$('#shl').val("edit");//设置修改方法的路径
$('#ff').form("load",row);
$('#dd').dialog("open");
alert($('#shl').val());
}
}
},'-',{
iconCls: 'icon-add',
handler: function(){
$('#ff').form("clear");
$('#dd').dialog("open");
$('#shl').val("add");//设置增加方法的路径
$('#dg').datagrid("reload");
}
},'-',{
iconCls: 'icon-remove',
handler: function(){
var row=$('#dg').datagrid("getSelected");
$.ajax({
url:$("#ctx").val()+'/userAction.action?methodName=del&&SerialNo='+row.SerialNo,
success: function(param){
$('#dg').datagrid("reload");
}
})
}
}]
});
})
//表单提交
function ok(){
$('#ff').form('submit', {
url:$("#ctx").val()+'/userAction.action?methodName='+$('#shl').val(),
success: function(param){
$('#dd').dialog("close");
$('#dg').datagrid("reload");
$('#ff').form("clear");
}
});
}
UserDao
package com.shl.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.shl.util.JsonBaseDao;
import com.shl.util.JsonUtils;
import com.shl.util.PageBean;
import com.shl.util.StringUtils;
public class UserDao extends JsonBaseDao{
/**
* 登录查询用户表 登录
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public List<Map<String, Object>> list(Map<String, String[]> paMap,PageBean pb) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_user_version2 where true ";
String uid=JsonUtils.getParamVal(paMap, "uid");
String upwd=JsonUtils.getParamVal(paMap, "upwd");
if(StringUtils.isNotBlank(uid)) {
sql=sql+" and uid = "+uid;
}
if(StringUtils.isNotBlank(upwd)) {
sql=sql+" and upwd = "+upwd;
}
return super.executeQuery(sql, pb);
}
/**
* 通过中间表查询登录用户所对应的权限
* @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_user_version2 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 (uid,uname,upwd) values(?,?,?)";
return super.executeUpdate(sql, new String[] {"uid","uname","upwd"}, paMap);
}
}
UserAction
package com.shl.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.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shl.dao.UserDao;
import com.shl.util.PageBean;
import com.shl.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 JsonProcessingException
* @throws Exception
*/
public String list(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
PageBean pb=new PageBean();
pb.setRequest(req);
List<Map<String, Object>> list = this.ud.list(req.getParameterMap(), pb);
ObjectMapper om=new ObjectMapper();
Map<String, Object> map=new HashMap<>();
map.put("total", pb.getTotal());
map.put("rows", list);
ResponseUtil.write(resp, om.writeValueAsString(map));
return null;
}
/**
* form组件提交所需数据(修改)
* @param req
* @param resp
* @return
* @throws JsonProcessingException
* @throws Exception
*/
public String edit(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, 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 JsonProcessingException
* @throws Exception
*/
public String del(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
int del=this.ud.del(req.getParameterMap());
ObjectMapper om=new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(del));
return null;
}
/**
* 增加
* @param req
* @param resp
* @return
* @throws JsonProcessingException
* @throws Exception
*/
public String add(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
int add=this.ud.add(req.getParameterMap());
ObjectMapper om=new ObjectMapper();
ResponseUtil.write(resp, om.writeValueAsString(add));
return null;
}
}
这样我们的增删查改就完成了
效果: