效果图如下:
dao层:
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.model.Book;
import com.zking.util.DBHelper;
public class BookDao implements IBookDao{
@Override
public List<Book> getBooks(String name, int pageIndex, int pageSize) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Book> list = new ArrayList<>();
try {
String sql = "select id,bookname,price,booktype,rownum as rid from t_book";
if(name != null && !"".equals(name)) {
sql += " where bookname like ?";
}
sql = "select * from (" + sql + ")b where b.rid between ? and ?";
con = DBHelper.getCon();
ps = con.prepareStatement(sql);
int start =(pageIndex-1)*pageSize+1;
int end = pageIndex*pageSize;
if(name != null && !"".equals(name)) {
ps.setString(1, name+"%");
ps.setInt(2, start);
ps.setInt(3, end);
} else {
ps.setInt(1, start);
ps.setInt(2, end);
}
rs = ps.executeQuery();
while(rs.next()) {
Book m = new Book();
m.setId(rs.getInt("id"));
m.setBookname(rs.getString("bookname"));
m.setPrice(rs.getString("price"));
m.setBooktype(rs.getString("booktype"));
list.add(m);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
@Override
public int getTotalPage() {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
int n = 0;
try {
con= DBHelper.getCon();
String sql = "select count(*) from t_book";
ps = con.prepareStatement(sql);
rs= ps.executeQuery();
if(rs.next()) {
n = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
public static void main(String[] args) {
BookDao dao = new BookDao();
List<Book> books = dao.getBooks("秀", 1, 5);
books.forEach(t -> System.out.println(t));
}
}
service层:
package com.zking.service;
import java.util.List;
import com.zking.dao.BookDao;
import com.zking.dao.IBookDao;
import com.zking.model.Book;
public class BookService implements IBookService{
private IBookDao dao = new BookDao();
@Override
public List<Book> getBooks(String name, int pageIndex, int pageSize) {
// TODO Auto-generated method stub
return dao.getBooks(name, pageIndex, pageSize);
}
@Override
public int getTotalPage() {
// TODO Auto-generated method stub
return dao.getTotalPage();
}
}
servle层:
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.alibaba.fastjson.JSON;
import com.zking.model.Book;
import com.zking.service.BookService;
import com.zking.service.IBookService;
@WebServlet("/bookServlet")
public class BookListServlet extends HttpServlet{
private IBookService service = new BookService();
@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.setContentType("application/json; charset=utf-8");
String name = req.getParameter("bookName");
String pageIndex = req.getParameter("page");
int pid = pageIndex == null||"".equals(pageIndex)?1:Integer.parseInt(pageIndex);
int pageSize = 10;
List<Book> list = service.getBooks(name, pid, pageSize);
int totaIPage = service.getTotalPage();
Map<String,Object> data = new HashMap<>();
data.put("total", totaIPage);
data.put("rows", list);
String json = JSON.toJSONString(data);
PrintWriter out = resp.getWriter();
out.write(json);
out.flush();
out.close();
}
}
前端界面:
<%@ 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>
<%@ include file="common/head.jsp"%>
<title>Insert title here</title>
</head>
<script type="text/javascript">
$(function(){
$('#menuTree').tree({
url:ctx + '/moduleServlet',
onDblClick:function(node) {
let children = $('#menuTree').tree('getChildren',node.target);
if(children <= 0) {
if($('#funcTab').tabs('exists',node.text))
return;
$('#funcTab').tabs('add',{
title: node.text,
content:'<iframe frameborder=0 src="'
+ node.url
+ '" scrolling="no" style="width:100%;height:100%;"></iframe>',
closable:true
});
}
}
});
});
</script>
<body class="easyui-layout">
<div data-options="region:'north',title:'North Title',split:true" style="height:100px;"></div>
<div data-options="region:'south',title:'South Title',split:true" style="height:100px;"></div>
<div data-options="region:'west',title:'West',split:true" style="width:200px;">
<ul id="menuTree" class="easyui-tree"></ul>
</div>
<div data-options="region:'center'" style="padding:5px;background:#eee;">
<!-- - -->
<div id="funcTab" class="easyui-tabs" style="width:100%;height:100%;">
<div title="首页" style="padding:20px;display:none;">
tab1
</div>
</div>
<!-- -->
</div>
</body>
</html>