Layui的增删查改

首先我们写一个实体类

TreeNode
package com.wangshaoyang.crud.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeNode {
	private String id;
	private String name;
	private Map<String, Object> attributes = new HashMap<>();
	private List<TreeNode> children = new ArrayList<>();
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public Map<String, Object> getAttributes() {
		return attributes;
	}
	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}
	public List<TreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}

	public TreeNode(String id, String text, Map<String, Object> attributes, List<TreeNode> children) {
		super();
		this.id = id;
		this.name = name;
		this.attributes = attributes;
		this.children = children;
	}
	public TreeNode() {
		super();
	}
	@Override
	public String toString() {
		return "TreeNode [id=" + id + ", name=" + name + ", attributes=" + attributes + ", children=" + children + "]";
	}
}
dao层去访问数据
UserDao
package com.wangshaoyang.dao;

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

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

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

public class UserDao extends JsonBaseDao {
	/**
	 * 登录查询用户表
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> list(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from tb_user where true";
		String uid=JsonUtils.getParamVal(paMap, "uid");
		String upwd=JsonUtils.getParamVal(paMap, "upwd");
		if(StringUtils.isNotBlank(uid)) {
			sql+=" and uid="+uid;
		}
		if(StringUtils.isNotBlank(upwd)) {
			sql+=" and upwd="+upwd;
		}
		return super.executeQuery(sql, pageBean);
	}
	/**
	 * 通过中间表查询登录用户所对应的权限
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> listMenu(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from tb_usermenu where true";
		String uid = JsonUtils.getParamVal(paMap, "uid");
		if(StringUtils.isNotBlank(uid)) {
			sql+=" and uid="+uid;
		}
		return super.executeQuery(sql, pageBean);
	}
}
BookDao
package com.wangshaoyang.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import com.wangshaoyang.util.JsonBaseDao;
import com.wangshaoyang.util.JsonUtils;
import com.wangshaoyang.util.PageBean;
import com.wangshaoyang.util.StringUtils;
public class BookDao extends JsonBaseDao {
	/**
	 * 查所有
	 * @param paMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> list(Map<String, String[]> paMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_mvc_book where true ";
		String bname = JsonUtils.getParamVal(paMap, "bname");
		if(StringUtils.isNotBlank(bname)) {
			sql+=" and bname like '%"+bname+"%'";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * 增加
	 * @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_mvc_book(bid,bname,price) values(?,?,?)";
		return super.executeUpdate(sql, new String[] {"bid","bname","price"}, paMap);
	}
	public int del(Map<String, String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql="delete from t_mvc_book where bid=?";
		return super.executeUpdate(sql, new String[] {"bid"}, paMap);
	}
	public int edit(Map<String, String[]> paMap) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql="update t_mvc_book set bname=?,price=? where bid=?";
		return super.executeUpdate(sql, new String[] {"bname","price","bid"}, paMap);
	}
}
package com.wangshaoyang.dao;

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

import com.wangshaoyang.entity.TreeNode;
import com.wangshaoyang.util.JsonBaseDao;
import com.wangshaoyang.util.JsonUtils;
import com.wangshaoyang.util.PageBean;
import com.wangshaoyang.util.StringUtils;

public class MenuDao extends JsonBaseDao {
	
	
	/**
	 * 
	 * @param map  req.getParameterMap
	 * @param pageBean 分页
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<TreeNode> list(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Map<String, Object>> listMenu = this.listMenuSef(map, pageBean);
		List<TreeNode> treeNodeList=new ArrayList<>();
		menuList2TreeNodeList(listMenu, treeNodeList);
		return treeNodeList;
	}
	/**
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> listMenuSef(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from tb_menu where true";
		String id=JsonUtils.getParamVal(map, "menuHid");
		if(StringUtils.isNotBlank(id)) {
			sql+=" and menuid in ("+id+") ";
		}
		else {
			sql+=" and menuid=-1";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<Map<String, Object>> listMenu(Map<String, String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from tb_menu where true ";
		String id=JsonUtils.getParamVal(map, "id");
		if(StringUtils.isNotBlank(id)) {
			sql+=" and parentid = "+id;
		}
		else {
			sql+=" and parentid = -1";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	
	/**
	 * menu表的数据不符合easyui树形展示的数据格式
	 * 需要转换成easyui所能识别的数据格式
	 * @param map
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public void menu2TreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
		treeNode.setId(map.get("menuid").toString());
		treeNode.setTitle(map.get("menuname").toString());
		treeNode.setAttributes(map);
		
		Map<String, String[]> jspMap=new HashMap<>();
		//获取到了当前节点的Id
		jspMap.put("id", new String [] {treeNode.getId()});
//		调方法把id当成parentid来查询子节点
		List<Map<String, Object>> listMenu = this.listMenu(jspMap, null);
		List<TreeNode> treeNodeList=new ArrayList<>();
		menuList2TreeNodeList(listMenu, treeNodeList);
		treeNode.setChildren(treeNodeList);
	}
	/**
	 * 转化数据格式
	 * @param mapList
	 * @param treeNodeList
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public void menuList2TreeNodeList(List<Map<String, Object>> mapList,List<TreeNode> treeNodeList) throws InstantiationException, IllegalAccessException, SQLException {
		TreeNode treeNode=null;
		for (Map<String, Object> map : mapList) {
			treeNode=new TreeNode();
			menu2TreeNode(map, treeNode);
			treeNodeList.add(treeNode);
		}
	}
}
web层去调用dao层给前台返回数据
UserAction
package com.wangshaoyang.web;

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

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

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

public class UserAction extends ActionSupport{
	private UserDao userDao=new UserDao();
	public String login(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
		List<Map<String, Object>> list=this.userDao.list(req.getParameterMap(), null);
		if(list!=null&&list.size()>0) {
			List<Map<String, Object>> listMenu = this.userDao.listMenu(req.getParameterMap(), null);
			StringBuffer sb=new StringBuffer();
			for (Map<String, Object> map : listMenu) {
				sb.append(","+map.get("menuId"));
			}
			req.setAttribute("menuHid", sb.substring(1));
		}
		else {
			return "login";
		}
		return "index";
	}
}
BookAction
package com.wangshaoyang.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.wangshaoyang.dao.BookDao;
import com.wangshaoyang.util.JsonUtils;
import com.wangshaoyang.util.PageBean;
import com.wangshaoyang.util.ResponseUtil;
import com.zking.framework.ActionSupport;

public class BookAction extends ActionSupport {
	private BookDao bookDao=new BookDao();
	public String list(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
		PageBean pageBean=new PageBean();
		pageBean.setRequest(req);
		List<Map<String, Object>> list = this.bookDao.list(req.getParameterMap(),pageBean);
		Map map=new HashMap();
		map.put("data", list);
		map.put("code", 0);
		map.put("count", pageBean.getTotal());
		ObjectMapper om=new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(map));
		return null;
	}
	public int addBook(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
		int add = bookDao.add(req.getParameterMap());
		ObjectMapper om=new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(add));
		return add;
	}
	public int del(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
		int del = bookDao.del(req.getParameterMap());
		ObjectMapper om=new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(del));
		return del;
	}
	public int edit(HttpServletRequest req,HttpServletResponse resp) throws JsonProcessingException, Exception {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("html/text;charset=utf-8");
		int edit = bookDao.edit(req.getParameterMap());
		ObjectMapper om=new ObjectMapper();
		ResponseUtil.write(resp, om.writeValueAsString(edit));
		return edit;
	}
}
MenuAction
package com.wangshaoyang.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.wangshaoyang.dao.MenuDao;
import com.wangshaoyang.entity.TreeNode;
import com.wangshaoyang.util.ResponseUtil;
import com.zking.framework.ActionSupport;

public class MenuAction extends ActionSupport {
	private MenuDao menuDao=new MenuDao();
	public String treeMenu(HttpServletRequest req,HttpServletResponse resp) throws Exception {						
		List<TreeNode> list = this.menuDao.list(req.getParameterMap(), null);
		ObjectMapper om=new ObjectMapper();
//		将List集合转为json串
		String jsonStr = om.writeValueAsString(list);
		ResponseUtil.write(resp, jsonStr);
		return null;
	}
}
导入相关依赖
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zrh</groupId>
  <artifactId>Struts</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Struts Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
   
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    

	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <version>4.0.1</version>
	    <scope>provided</scope>
	</dependency>
	
     <dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.5.13</version>
		</dependency>
   	
   	<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.44</version>
		</dependency>
   	
     <dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		
    
			<dependency> 
			   <groupId>javax.servlet.jsp</groupId> 
			   <artifactId>jsp-api</artifactId> 
			   <version>2.1</version> 
			   <scope>provided</scope>
			</dependency>
			
			<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
		
		
		<!--我们需要自定义标签库一定要引用这个jar包  -->
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jsp-api</artifactId>
			<version>8.0.47</version>
		</dependency>
			
		
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-databind</artifactId>
		    <version>2.4.4</version>
		</dependency>
		
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>4.5.16</version>
		</dependency>
		
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.47</version>
		</dependency>
			
  </dependencies>
  <build>
    <finalName>Struts</finalName>
    <plugins>
    	<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
    </plugins>
  </build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Layui</display-name>
  <filter>
  	<filter-name>encodingFiter</filter-name>
  	<filter-class>com.wangshaoyang.util.EncodingFiter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFiter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <servlet>
  	<servlet-name>actionServlet</servlet-name>
  	<servlet-class>com.zking.framework.ActionServlet</servlet-class>
  	<init-param>
  		<param-name>xmlPath</param-name>
  		<param-value>/mvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>actionServlet</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

今天就更新到这里
喜欢的可以关注我
不定时更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听晚风续过晚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值