目录
实体类
package com.zking.entity;
public class Permission {
private long id;
private String name;
private String description;
private String url;
private long pid;
private int ismenu;
private long displayno;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public long getPid() {
return pid;
}
public void setPid(long pid) {
this.pid = pid;
}
public int getIsmenu() {
return ismenu;
}
public void setIsmenu(int ismenu) {
this.ismenu = ismenu;
}
public long getDisplayno() {
return displayno;
}
public void setDisplayno(long displayno) {
this.displayno = displayno;
}
public Permission() {
// TODO Auto-generated constructor stub
}
public Permission(long id, String name, String description, String url, long pid, int ismenu, long displayno) {
super();
this.id = id;
this.name = name;
this.description = description;
this.url = url;
this.pid = pid;
this.ismenu = ismenu;
this.displayno = displayno;
}
public Permission( String name, String description, String url, long pid, int ismenu, long displayno) {
this.name = name;
this.description = description;
this.url = url;
this.pid = pid;
this.ismenu = ismenu;
this.displayno = displayno;
}
@Override
public String toString() {
return "Permission [id=" + id + ", name=" + name + ", description=" + description + ", url=" + url + ", pid="
+ pid + ", ismenu=" + ismenu + ", displayno=" + displayno + "]";
}
}
/**
*@author 作者:chenchen
*@version 创作时间:2022年8月2日上午10:18:30
*/
Action
package com.zking.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.dao.PermissionDao;
import com.zking.entity.Permission;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
public class PermissionAction extends ActionSupport implements ModelDriver<Permission>{
private Permission p = new Permission();
private PermissionDao pd = new PermissionDao();
@Override
public Permission getModel() {
// TODO Auto-generated method stub
return p;
}
// 增加
public String add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
pd.add(p);
return "aaa";
}
// 删除
public String del(HttpServletRequest req, HttpServletResponse resp) throws Exception {
pd.del(p);
return "aaa";
}
// 修改
public String update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
pd.update(p);
return "aaa";
}
// 查看所有
public String list(HttpServletRequest req, HttpServletResponse resp){
String sql="select * from t_easyui_permission where 1=1 ";
String xlk = req.getParameter("xlk");
String gjz = req.getParameter("gjz");
if(xlk==null) {
xlk="name";
}
if(gjz==null) {
gjz="";
}
sql +=" and "+xlk+" like '%"+gjz+"%'";
List<Permission> list;
try {
list = pd.list(sql, p, null);
req.setAttribute("list", list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
// 修改绑值
public String updateBz(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Permission pre = pd.getOne(p, null).get(0);
req.setAttribute("pre", pre);
return "updateBz";
}
// 查看绑值
public String checkBz(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Permission pre = pd.getOne(p, null).get(0);
req.setAttribute("pre", pre);
return "checkBz";
}
}
/**
*@author 作者:chenchen
*@version 创作时间:2022年8月2日上午10:29:56
*/
Dao方法
package com.zking.dao;
import java.util.List;
import com.zking.entity.Permission;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
public class PermissionDao extends BaseDao<Permission>{
// 查询所有
public List<Permission> list(String sql, Permission p, PageBean pageBean) throws Exception {
return super.executeQuery(sql, Permission.class, null);
}
//查询单个
public List<Permission> getOne( Permission p, PageBean pageBean) throws Exception {
long id = p.getId();
String sql="select * from t_easyui_permission where id="+id;
return super.executeQuery(sql, Permission.class, null);
}
// 增加
public int add(Permission p) throws Exception {
String sql="insert into t_easyui_permission(name,description,url,pid,ismenu,displayno) values(?,?,?,?,?,?)";
return super.executeUpdate(sql,p,new String[] {"name","description","url","pid","ismenu","displayno"});
}
//删除
public int del(Permission p) throws Exception {
String sql="delete from t_easyui_permission where id=?";
return super.executeUpdate(sql,p,new String[] {"id"});
}
//修改
public int update(Permission p) throws Exception {
String sql="update t_easyui_permission set name=?,description=?,url=?,pid=?,ismenu=?,displayno=? where id=?";
return super.executeUpdate(sql,p,new String[] {"name","description","url","pid","ismenu","displayno","id"});
}
}
/**
*@author 作者:chenchen
*@version 创作时间:2022年8月2日上午10:19:50
*/
BaseDao.jsp
package com.zking.util;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 鎵�鏈塂ao灞傜殑鐖剁被 BookDao UserDao OrderDao ...
*
* @author Administrator
*
* @param <T>
*/
public class BaseDao<T> {
/**
* 閫傚悎澶氳〃鑱旀煡鐨勬暟鎹繑鍥�
* @param sql
* @param pageBean
* @return
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public List<Map<String, Object>> executeQuery(String sql, PageBean pageBean)
throws SQLException, InstantiationException, IllegalAccessException {
List<Map<String, Object>> list = new ArrayList<>();
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
/*
* 鏄惁闇�瑕佸垎椤碉紵 鏃犻渶鍒嗛〉锛堥」鐩腑鐨勪笅鎷夋锛屾煡璇㈡潯浠舵暀鍛樹笅鎷夋锛屾棤椤诲垎椤碉級 蹇呴』鍒嗛〉锛堥」鐩腑鍒楄〃绫婚渶姹傘�佽鍗曞垪琛ㄣ�佸晢鍝佸垪琛ㄣ�佸鐢熷垪琛�...锛�
*/
if (pageBean != null && pageBean.isPagination()) {
// 蹇呴』鍒嗛〉锛堝垪琛ㄩ渶姹傦級
String countSQL = getCountSQL(sql);
pst = con.prepareStatement(countSQL);
rs = pst.executeQuery();
if (rs.next()) {
pageBean.setTotal(String.valueOf(rs.getObject(1)));
}
// 鎸姩鍒颁笅闈紝鏄洜涓烘渶鍚庢墠澶勭悊杩斿洖鐨勭粨鏋滈泦
// -- sql=SELECT * FROM t_mvc_book WHERE bname like '%鍦e%'
// -- pageSql=sql limit (page-1)*rows,rows 瀵瑰簲鏌愪竴椤电殑鏁版嵁
// -- countSql=select count(1) from (sql) t 绗﹀悎鏉′欢鐨勬�昏褰曟暟
String pageSQL = getPageSQL(sql, pageBean);// 绗﹀悎鏉′欢鐨勬煇涓�椤垫暟鎹�
pst = con.prepareStatement(pageSQL);
rs = pst.executeQuery();
} else {
// 涓嶅垎椤碉紙select闇�姹傦級
pst = con.prepareStatement(sql);// 绗﹀悎鏉′欢鐨勬墍鏈夋暟鎹�
rs = pst.executeQuery();
}
// 鑾峰彇婧愭暟鎹�
ResultSetMetaData md = rs.getMetaData();
int count = md.getColumnCount();
Map<String, Object> map = null;
while (rs.next()) {
map = new HashMap<>();
for (int i = 1; i <= count; i++) {
// map.put(md.getColumnName(i), rs.getObject(i));
map.put(md.getColumnLabel(i), rs.getObject(i));
}
list.add(map);
}
return list;
}
/**
*
* @param sql
* @param attrs
* map涓殑key
* @param paMap
* jsp鍚戝悗鍙颁紶閫掔殑鍙傛暟闆嗗悎
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int executeUpdate(String sql, String[] attrs, Map<String, String[]> paMap) throws SQLException,
NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Connection con = DBAccess.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
for (int i = 0; i < attrs.length; i++) {
pst.setObject(i + 1, JsonUtils.getParamVal(paMap, attrs[i]));
}
return pst.executeUpdate();
}
/**
* 鎵瑰鐞�
* @param sqlLst
* @return
*/
public static int executeUpdateBatch(String[] sqlLst) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DBAccess.getConnection();
// 璁剧疆涓嶈嚜鍔ㄦ彁浜�
conn.setAutoCommit(false);
for (String sql : sqlLst) {
stmt = conn.prepareStatement(sql);
stmt.executeUpdate();
}
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
throw new RuntimeException(e1);
}
e.printStackTrace();
throw new RuntimeException(e);
} finally {
DBAccess.close(conn, stmt, null);
}
return 0;
}
/**
* 閫氱敤鐨勫鍒犳敼鏂规硶
*
* @param book
* @throws Exception
*/
public int executeUpdate(String sql, T t, String[] attrs) throws Exception {
// String[] attrs = new String[] {"bid", "bname", "price"};
Connection con = DBAccess.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
// pst.setObject(1, book.getBid());
// pst.setObject(2, book.getBname());
// pst.setObject(3, book.getPrice());
/*
* 鎬濊矾锛� 1.浠庝紶杩涙潵鐨則涓鍙栧睘鎬у�� 2.寰�棰勫畾涔夊璞′腑璁剧疆浜嗗��
*
* t->book f->bid
*/
for (int i = 0; i < attrs.length; i++) {
Field f = t.getClass().getDeclaredField(attrs[i]);
f.setAccessible(true);
pst.setObject(i + 1, f.get(t));
}
return pst.executeUpdate();
}
/**
* 閫氱敤鍒嗛〉鏌ヨ
*
* @param sql
* @param clz
* @return
* @throws Exception
*/
public List<T> executeQuery(String sql, Class<T> clz, PageBean pageBean) throws Exception {
List<T> list = new ArrayList<T>();
Connection con = DBAccess.getConnection();
;
PreparedStatement pst = null;
ResultSet rs = null;
/*
* 鏄惁闇�瑕佸垎椤碉紵 鏃犻渶鍒嗛〉锛堥」鐩腑鐨勪笅鎷夋锛屾煡璇㈡潯浠舵暀鍛樹笅鎷夋锛屾棤椤诲垎椤碉級 蹇呴』鍒嗛〉锛堥」鐩腑鍒楄〃绫婚渶姹傘�佽鍗曞垪琛ㄣ�佸晢鍝佸垪琛ㄣ�佸鐢熷垪琛�...锛�
*/
if (pageBean != null && pageBean.isPagination()) {
// 蹇呴』鍒嗛〉锛堝垪琛ㄩ渶姹傦級
String countSQL = getCountSQL(sql);
pst = con.prepareStatement(countSQL);
rs = pst.executeQuery();
if (rs.next()) {
pageBean.setTotal(String.valueOf(rs.getObject(1)));
}
// 鎸姩鍒颁笅闈紝鏄洜涓烘渶鍚庢墠澶勭悊杩斿洖鐨勭粨鏋滈泦
// -- sql=SELECT * FROM t_mvc_book WHERE bname like '%鍦e%'
// -- pageSql=sql limit (page-1)*rows,rows 瀵瑰簲鏌愪竴椤电殑鏁版嵁
// -- countSql=select count(1) from (sql) t 绗﹀悎鏉′欢鐨勬�昏褰曟暟
String pageSQL = getPageSQL(sql, pageBean);// 绗﹀悎鏉′欢鐨勬煇涓�椤垫暟鎹�
pst = con.prepareStatement(pageSQL);
rs = pst.executeQuery();
} else {
// 涓嶅垎椤碉紙select闇�姹傦級
pst = con.prepareStatement(sql);// 绗﹀悎鏉′欢鐨勬墍鏈夋暟鎹�
rs = pst.executeQuery();
}
while (rs.next()) {
T t = clz.newInstance();
Field[] fields = clz.getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
f.set(t, rs.getObject(f.getName()));
}
list.add(t);
}
return list;
}
/**
* 灏嗗師鐢烻QL杞崲鎴愮鍚堟潯浠剁殑鎬昏褰曟暟countSQL
*
* @param sql
* @return
*/
private String getCountSQL(String sql) {
// -- countSql=select count(1) from (sql) t 绗﹀悎鏉′欢鐨勬�昏褰曟暟
return "select count(1) from (" + sql + ") t";
}
/**
* 灏嗗師鐢烻QL杞崲鎴恜ageSQL
*
* @param sql
* @param pageBean
* @return
*/
private String getPageSQL(String sql, PageBean pageBean) {
// (this.page - 1) * this.rows
// pageSql=sql limit (page-1)*rows,rows
return sql + " limit " + pageBean.getStartIndex() + "," + pageBean.getRows();
}
}
mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/pre" type="com.zking.web.PermissionAction">
<forward name="list" path="/index.jsp" redirect="false"/>
<forward name="aaa" path="/pre.action?methodName=list" redirect="false"/>
<forward name="checkBz" path="/check.jsp" redirect="false"/>
<forward name="updateBz" path="/update.jsp" redirect="false"/>
</action>
</config>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/pre.action?methodName=add" method="post">
<table border="1px">
<tr>
<td>名字:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>描述:</td>
<td><input type="text" name="description"></td>
</tr>
<tr>
<td>路径:</td>
<td><input type="text" name="url"></td>
</tr>
<tr>
<td>父权限:</td>
<td><input type="text" name="pid"></td>
</tr>
<tr>
<td>菜单:</td>
<td><input type="text" name="ismenu"></td>
</tr>
<tr>
<td>顺序:</td>
<td><input type="text" name="displayno"></td>
</tr>
</table>
<input type="submit" value="提交">
<input type="reset"value="重置">
<a href="${pageContext.request.contextPath}/pre.action?methodName=list">返回</a>
</form>
</body>
</html>
check.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<tr>
<td>名字:</td>
<td><input type="text" value="${pre.name}" readonly="readonly"></td>
</tr>
<tr>
<td>描述:</td>
<td><input type="text" value="${pre.description}" readonly="readonly"></td>
</tr>
<tr>
<td>路径:</td>
<td><input type="text" value="${pre.url}" readonly="readonly"></td>
</tr>
<tr>
<td>父权限:</td>
<td><input type="text" value="${pre.pid}" readonly="readonly"></td>
</tr>
<tr>
<td>菜单:</td>
<td><input type="text" value="${pre.ismenu}" readonly="readonly"></td>
</tr>
<tr>
<td>顺序:</td>
<td><input type="text" value="${pre.displayno}" readonly="readonly"></td>
</tr>
</table>
<a href="${pageContext.request.contextPath}/pre.action?methodName=list">返回</a>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/pre.action?methodName=list" method="post">
<select name="xlk">
<option value="name">名称</option>
<option value="url">url</option>
</select>
<input type="text" name="gjz">
<input type="submit" value="查询">
</form>
<table border="1px">
<tr>
<td>权限id</td>
<td>名称</td>
<td>子名称</td>
<td>路径</td>
<td>父权限</td>
<td>权限菜单</td>
<td>展示顺序</td>
<td>操作</td>
</tr>
<c:forEach items="${list}" var="c">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
<td>${c.description}</td>
<td>${c.url}</td>
<td>${c.pid}</td>
<td>${c.ismenu}</td>
<td>${c.displayno}</td>
<td>
<a href="add.jsp">增加</a>
<a onclick="return confirm('你确定要删除吗')" href="${pageContext.request.contextPath}/pre.action?methodName=del&id=${c.id}">删除</a>
<a href="${pageContext.request.contextPath}/pre.action?methodName=updateBz&id=${c.id}">修改</a>
<a href="${pageContext.request.contextPath}/pre.action?methodName=checkBz&id=${c.id}">查看</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/pre.action?methodName=update" method="post">
<table border="1px">
<tr>
<td>名字:</td>
<td><input type="text" value="${pre.name}" name="name"></td>
</tr>
<tr>
<td>描述:</td>
<td><input type="text" value="${pre.description}" name="description"></td>
</tr>
<tr>
<td>路径:</td>
<td><input type="text" value="${pre.url}" name="url"></td>
</tr>
<tr>
<td>父权限:</td>
<td><input type="text" value="${pre.pid}" name="pid"></td>
</tr>
<tr>
<td>菜单:</td>
<td><input type="text" value="${pre.ismenu}" name="ismenu"></td>
</tr>
<tr>
<td>顺序:</td>
<td><input type="text" value="${pre.displayno}" name="displayno"></td>
</tr>
</table>
<input type="hidden" name="id" value="${pre.id}">
<input type="submit" value="提交">
<input type="reset"value="重置">
<a href="${pageContext.request.contextPath}/pre.action?methodName=list">返回</a>
</form>
</body>
</html>