說明
- Vue數據綁定
- axios請求服務器端數據
- layui提供表格樣式
- laypage提供分頁
整合效果
代碼
創建項目,導入
服務器端代碼
CREATE TABLE `tb_dept` (
`deptno` tinyint(2) UNSIGNED NOT NULL COMMENT '部門編號',
`dname` varchar(14) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '部門名稱',
`loc` varchar(13) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '部門地址',
PRIMARY KEY (`deptno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
public class Dept {
private Integer deptno;
private String dname;
private String loc;
}
public class PageBean<T> {
private long pageSize = 10;
private long pageNum;
private long total;
private long pages;
private List<T> records;
}
public final class DBUtil {
private static final String url = "jdbc:mysql://localhost:3306/db_test?useSSL=false&serverTimezone=GMT%2B8";
private static final String name = "root";
private static final String pass = "root";
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, name, pass);
}
public static void free(ResultSet rs, Statement stmt, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
public class DeptService {
public List<Dept> getAll() throws SQLException {
List<Dept> depts = new ArrayList<>();
Connection conn = DBUtil.getConnection();
String sql = "select * from tb_dept";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
depts.add(new Dept(rs.getInt("deptno"), rs.getString("dname"), rs.getString("loc")));
}
return depts;
}
public List<Dept> getWithPage(Integer pageNum, Integer pageSize) throws SQLException {
List<Dept> depts = new ArrayList<>();
Connection conn = DBUtil.getConnection();
String sql = "select * from tb_dept limit ?,?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, (pageNum - 1) * pageSize);
ps.setInt(2, pageSize);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
depts.add(new Dept(rs.getInt("deptno"), rs.getString("dname"), rs.getString("loc")));
}
DBUtil.free(rs,ps,conn);
return depts;
}
public int count() throws SQLException {
Connection conn = DBUtil.getConnection();
String sql = "select count(*) from tb_dept";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
rs.next();
int res = rs.getInt(1);
DBUtil.free(rs,ps,conn);
return res;
}
}
@WebServlet(urlPatterns = "/dept")
public class DeptServlet extends HttpServlet {
private DeptService deptService = new DeptService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String op = req.getParameter("op");
PrintWriter out = resp.getWriter();
switch (op) {
case "getAll":
try {
getAll(out);
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "getWithPage":
try {
getWithPage(req, out);
} catch (SQLException e) {
e.printStackTrace();
}
break;
default:
break;
}
out.flush();
}
private void getWithPage(HttpServletRequest req, PrintWriter out) throws SQLException {
int pageNum = Integer.parseInt(req.getParameter("pageNum"));
int pageSize = Integer.parseInt(req.getParameter("pageSize"));
List<Dept> depts = deptService.getWithPage(pageNum, pageSize);
PageBean<Dept> pageBean = new PageBean<>();
pageBean.setPageNum(pageNum);
pageBean.setPageSize(pageSize);
pageBean.setRecords(depts);
int count = deptService.count();
pageBean.setTotal(count);
pageBean.setPageSize(count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
out.write(JSON.toJSONString(pageBean));
}
private void getAll(PrintWriter out) throws SQLException {
List<Dept> depts = deptService.getAll();
PageBean<Dept> pageBean = new PageBean<>();
pageBean.setRecords(depts);
out.write(JSON.toJSONString(pageBean));
}
}
前端頁面代碼(★★★★★)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<table class="layui-table" id="app">
<thead>
<tr>
<th>部門編號</th>
<th>部門名稱</th>
<th>部門地址</th>
</tr>
</thead>
<tbody>
<tr v-for="dept in deptList">
<td>{{dept.deptno}}</td>
<td>{{dept.dname}}</td>
<td>{{dept.loc}}</td>
</tr>
</tbody>
</table>
<div id="laypage"></div>
<script src="layui/layui.js"></script>
<script src="layui/jquery.js"></script>
<script src="js/vue.js"></script>
<script src="js/axios.min.js"></script>
<script type="text/javascript">
let vm = new Vue({
el: "#app",
data: {
deptList: [],
pageNum: 1,
pageSize: 10,
total: 0,
pages: 1
},
methods: {
getData: function () {
axios.get('dept', {
params: {
op: 'getWithPage',
pageNum: this.pageNum,
pageSize: this.pageSize
}
}).then(res => {
this.deptList = res.data.records;
this.total = res.data.total;
this.pages = res.data.pages;
console.info(res);
if (this.pageNum == 1) {
this.showPage();
}
}).catch(error => {
console.log(error);
});
},
showPage: function () {
layui.use('laypage', function () {
let laypage = layui.laypage;
laypage.render({
elem: 'laypage',
count: vm.total,
limit: vm.pageSize,
limits: [10, 20, 30],
curr: 1,
groups: 3,
prev: '上一頁',
next: '下一頁',
first: 1,
last: vm.pages,
layout: ['prev', 'page', 'next', 'limit', 'refresh', 'skip'],
jump: function (obj, first) {
console.log(obj.curr);
console.log(obj.limit);
vm.pageNum = obj.curr;
vm.pageSize = obj.limit;
if (!first) {
vm.getData();
}
}
});
});
}
},
created: function () {
this.getData();
}
});
</script>
</body>
</html>