今天是EasyUi的第二次课,主要也是教大家连接数据库使用。
EasyUi代码实操
utli包下的DBHeper代码如下:
package com.yjx.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import oracle.jdbc.driver.OracleDriver;
public class DBHeper {
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
//定义字符串链接
private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
public static Connection getCon() {
try {
return DriverManager.getConnection(URL,"scott","zking123");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//关闭资源
public static void getClose(Connection con, PreparedStatement ps, ResultSet rs) {
try {
if (con != null && !con.isClosed()) {
con.close();
}
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
utli包下的TreeFactory代码如下:
package com.yjx.util;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* 帮助我们菜单分类好父节点和子节点
* @author zjjt
*
*/
@SuppressWarnings("all")
public class TreeFactory {
public static List<TreeNode> buildList(List<TreeNode> nodeList) {
List<TreeNode> list = buildList(nodeList, "0");
TreeNode root;
if (!list.isEmpty()) {
return list;
} else {
root = new TreeNode();
root.setId("000");
root.setParentId("-1");
root.setHasParent(false);
root.setHasChildren(true);
root.setChecked(true);
root.setChildren(list);
root.setText("主菜单");
root.setState("closed");
}
return Arrays.asList(root);
}
public static List<TreeNode> buildList(List<TreeNode> nodeList, String topMenuId) {
return Optional.ofNullable(nodeList).map(list -> {
list.forEach(item -> {
list.stream().filter(i -> {
return i.getId().equals(item.getParentId());
}).forEach(i -> {
i.setHasChildren(Boolean.TRUE);
i.getChildren().add(item);
});
});
return list.stream().filter(i -> {
return i.getParentId().equals(topMenuId);
}).collect(Collectors.toList());
}).get();
}
}
utli包下的TreeNode代码如下:
package com.yjx.util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
@Accessors(chain = true)
public class TreeNode {
private String id;
private String text;
private String state;
private Boolean checked = false;
private Map<String, Object> attributes=new HashMap<>();
private List<TreeNode> children = new ArrayList<>();
private String parentId;
private Boolean hasParent = false;
private Boolean hasChildren = false;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Boolean getChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
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 String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public Boolean getHasParent() {
return hasParent;
}
public void setHasParent(Boolean hasParent) {
this.hasParent = hasParent;
}
public Boolean getHasChildren() {
return hasChildren;
}
public void setHasChildren(Boolean hasChildren) {
this.hasChildren = hasChildren;
}
public TreeNode() {
// TODO Auto-generated constructor stub
}
public TreeNode(String id, String text, String state, Boolean checked, Map<String, Object> attributes,
List<TreeNode> children, String parentId, Boolean hasParent, Boolean hasChildren) {
super();
this.id = id;
this.text = text;
this.state = state;
this.checked = checked;
this.attributes = attributes;
this.children = children;
this.parentId = parentId;
this.hasParent = hasParent;
this.hasChildren = hasChildren;
}
@Override
public String toString() {
return "TreeNode [id=" + id + ", text=" + text + ", state=" + state + ", checked=" + checked + ", attributes="
+ attributes + ", children=" + children + ", parentId=" + parentId + ", hasParent=" + hasParent
+ ", hasChildren=" + hasChildren + "]";
}
}
pojo包下的Permission代码如下:
package com.yjx.pojo;
/**
* permission实体类
* @author zjjt
*
*/
public class Permission {
private Integer id;
private Integer pid;
private String text;
private String url;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Permission() {
// TODO Auto-generated constructor stub
}
public Permission(Integer id, Integer pid, String text, String url) {
super();
this.id = id;
this.pid = pid;
this.text = text;
this.url = url;
}
@Override
public String toString() {
return "Permission [id=" + id + ", pid=" + pid + ", text=" + text + ", url=" + url + "]";
}
}
数据访问层接口IPermissionDao代码如下:
package com.yjx.dao;
import java.util.List;
import com.yjx.pojo.Permission;
import com.yjx.util.TreeNode;
public interface IPermissionDao {
//查询所有的数据
List<Permission> all();
List<TreeNode> listNodes();
}
数据访问层PermissionDaoImpl代码如下:
package com.yjx.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.yjx.dao.IPermissionDao;
import com.yjx.pojo.Permission;
import com.yjx.util.DBHeper;
import com.yjx.util.TreeFactory;
import com.yjx.util.TreeNode;
public class PermissionDaoImpl implements IPermissionDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
/**
* 查询表格所有数据
*/
@Override
public List<Permission> all() {
List<Permission> list=new ArrayList<Permission>();
try {
con=DBHeper.getCon();
ps=con.prepareStatement("select * from bs_permission");
rs=ps.executeQuery();
while(rs.next()) {
Permission p=new Permission();
p.setId(rs.getInt(1));
p.setPid(rs.getInt(2));
p.setText(rs.getString(3));
p.setUrl(rs.getString(4));
list.add(p);
System.out.print(list);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHeper.getClose(con, ps, rs);
}
return list;
}
/**
* 拿到父节点
*/
@Override
public List<TreeNode> listNodes() {
List<TreeNode> listNode=new ArrayList<TreeNode>();
//拿到Permission的数据
List<Permission> list=all();
System.out.print(list);
//遍历Permission的数据
for (Permission p : list) {
//将该数据赋值给到TreeNode
TreeNode node=new TreeNode();
node.setId(p.getId()+"");
node.setParentId(p.getPid()+"");
node.setText(p.getText());
node.getAttributes().put("pid",p.getPid());
node.getAttributes().put("url",p.getUrl());
listNode.add(node);
}
//调用该方法,将参数传进去得到一个集合
return TreeFactory.buildList(listNode,"0");
}
}
业务逻辑层接口IPermissionBiz代码如下:
package com.yjx.biz;
import java.util.List;
import com.yjx.pojo.Permission;
import com.yjx.util.TreeNode;
public interface IPermissionBiz {
//查询所有的数据
List<Permission> all();
List<TreeNode> listNodes();
}
业务逻辑层IPermissionBiz代码如下:
package com.yjx.biz.impl;
import java.util.List;
import com.yjx.biz.IPermissionBiz;
import com.yjx.dao.IPermissionDao;
import com.yjx.dao.impl.PermissionDaoImpl;
import com.yjx.pojo.Permission;
import com.yjx.util.TreeNode;
public class PermissionBizImpl implements IPermissionBiz{
IPermissionDao dao=new PermissionDaoImpl();
@Override
public List<Permission> all() {
return dao.all();
}
@Override
public List<TreeNode> listNodes() {
return dao.listNodes();
}
}
Servlet包下的PermissionServlet代码如下:
package com.yjx.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yjx.biz.IPermissionBiz;
import com.yjx.biz.impl.PermissionBizImpl;
@WebServlet("/permission.do")
public class PermissionServlet extends HttpServlet{
private IPermissionBiz biz=new PermissionBizImpl();
private ObjectMapper mapper = new ObjectMapper();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//解决乱码问题
resp.setCharacterEncoding("utf-8");
//将数据写给前台
mapper.writeValue(resp.getWriter(),biz.listNodes());
}
}
前端代码index.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head lang="zh">
<meta charset="UTF-8">
<script src="${pageContext.request.contextPath}/jquery-easyui-1.5.5.2/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/jquery-easyui-1.5.5.2/jquery.easyui.min.js"></script>
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/jquery-easyui-1.5.5.2/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/jquery-easyui-1.5.5.2/themes/icon.css">
<title>首页</title>
</head>
<body class="easyui-layout">
<div data-options="region:'north',title:'网站导航栏',split:false" style="height:100px;"></div>
<div data-options="region:'south',title:'友情链接',split:false" style="height:100px;"></div>
<div data-options="region:'west',title:'菜单'" style="width:300px;">
<ul id="tt"></ul>
</div> `
<div data-options="region:'center',border:'false'" style="padding:5px;background:#eee;">
<div id="mainTab" class="easyui-tabs" style="width:100%;height:100%;"></div>
</body>
<script>
let mainTab=$("#mainTab");
//tree构建方法
$("#tt").tree({
url:"permission.do",//远程数据的地址
method:"POST",//访问方式
lines:true,//显示虚线
onClick: function(node){
//node.text拿到的是每一个节点的名字
//node.attributes.pid拿到attributes下的pid的值
//判断是否是父节点,是父节点就不打开
if(!node.attributes['pid']){
return
}
//判断面板是否已经被打开
if(mainTab.tabs('exists',node.text)){
//就选中我点开的
maintab.tabs('select',node.text);
return;
}
//点击时新开一个面板
mainTab.tabs('add',{
pid:node.pid,
title:node.text
})
}
})
</script>
</html>
今天的学习就到这里啦!!!