Dao层代码:
?
package com.yq.dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.yq.entity.Student;
import com.zking.unti.PageBean;
import com.zking.unti.StringUtils;
public class StudenDao extends BaseDao<Student>{
public int add(Student stu) {
String sql="insert into t_student(sname_pinyin,age,remark) values('"+stu.getSname()+"','"+stu.getSnamePinyin()+"',"+stu.getAge()+",'"+stu.getRemark()+"')";
return this.executeUpdate(sql);
}
public int update(Student stu) {
String sql="update t_student set sname='"+stu.getSname()+"',sname_pinyin='"+stu.getSnamePinyin()+"',age="+stu.getAge()+",remark='"+stu.getRemark()+"' where sid="+stu.getSid();
return this.executeUpdate(sql);
}
public int del(Student stu) {
String sql="delete from t_student where sid="+stu.getSid();
return this.executeUpdate(sql);
}
/**
* 查询所有
* @param student
* @param pageIndex
* @param pageSize
* @return
*/
public List<Student> list(Student student,PageBean pageBean) throws Exception{
String sql="select * from t_student where 1=1";
if(StringUtils.isNotBlank(student.getSname())) {
sql+=" and sname like '%"+student.getSname()+"%'";
}
return this.executeQuery(sql, pageBean, new CallBack<Student>() {
@Override
public List<Student> forEach(ResultSet rs) throws Exception {
List<Student> students=new ArrayList<>();
Student s=null;
while(rs.next()) {
s=new Student();
s.setSid(rs.getInt("sid"));
s.setSname(rs.getString("sname"));
s.setAge(rs.getInt("age"));
s.setRemark(rs.getString("remark"));
students.add(s);
}
return students;
}
});
}
}
BaseDao:
package com.yq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import com.zking.unti.DBAccess;
import com.zking.unti.PageBean;
public class BaseDao<T> {
public interface CallBack<T>{
public List<T> forEach(ResultSet rs) throws Exception;
}
/**
* 1.通用分页两个基本要素
* 1.1 需要知道满足条件的条目数
* 1.2查询满足条件指定页码的结果
* @throws Exception
*/
public List<T> executeQuery(String sql,PageBean pageBean,CallBack<T> callBack) throws Exception{
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con=DBAccess.getConnection();
// 如果用户需要分页的话将用户传递过来的sql语句进行转换
if(null!=pageBean&&pageBean.isPagination()) {
String countSql = this.countSql(sql);
ps=con.prepareStatement(countSql);
rs=ps.executeQuery();
if(rs.next()) {
pageBean.setTotal(rs.getInt(1));
}
//开始查询结果
String pageSql = this.pageSql(sql, pageBean);
ps=con.prepareStatement(pageSql);
}else {
//用户不需要分页
ps=con.prepareStatement(sql);
}
rs=ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return callBack.forEach(rs);
}
/**
* 共用的方法用于执行sql语句实现增删改
* 通用编辑
* @param sql
* @return
*/
public int executeUpdate(String sql) {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
int n=0;
try {
con=DBAccess.getConnection();
ps=con.prepareStatement(sql);
n=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBAccess.close(con, ps, rs);
}
return n;
}
/**
* 将用户sql语句修改为查询满足条件的总条目数sql语句
* @param sql
* @return
*/
private String countSql(String sql) {
String countSql="select count(*) from ("+sql+") a";
return countSql;
}
private String pageSql(String sql,PageBean pageBean) {
String pageSql="select * from ("+sql+")a limit "+pageBean.getStartIndex()+","+pageBean.getRows();
return pageSql;
}
}
?子控制器代码:
?
package com.yq.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yq.dao.StudenDao;
import com.yq.entity.Student;
import com.yq.framework.ActionSupport;
import com.yq.framework.ModelDriver;
import com.zking.unti.PageBean;
public class StudentAction extends ActionSupport implements ModelDriver<Student>{
private Student s=new Student();
private StudenDao sd=new StudenDao();
@Override
public Student getModel() {
return s;
}
public String add(HttpServletRequest req, HttpServletResponse resp) {
sd.add(s);
return "toList";
}
public String delete(HttpServletRequest req, HttpServletResponse resp) {
sd.del(s);
return "toList";
}
public String edit(HttpServletRequest req, HttpServletResponse resp) {
sd.update(s);
return "toList";
}
public String list(HttpServletRequest req, HttpServletResponse resp) {
PageBean pageBean =new PageBean();
pageBean.setRequest(req);
try {
List<Student> list = sd.list(s, pageBean);
req.setAttribute("books", list);
req.setAttribute("pageBean", pageBean);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
public String toEdit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
// 如果跳转的是新增界面无需查询,如果跳转的是修改界面,需要查询当前bid对应的数据,回显到界面
if(s.getSid()!=0){
List<Student> ls = sd.list(s, null);
req.setAttribute("b", ls.get(0));
}
return "toEdit";
}
}
视图层代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link
href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"
rel="stylesheet">
<script
src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script>
<title>书籍列表</title>
<style type="text/css">
.page-item input {
padding: 0;
width: 40px;
height: 100%;
text-align: center;
margin: 0 6px;
}
.page-item input, .page-item b {
line-height: 38px;
float: left;
font-weight: 400;
}
.page-item.go-input {
margin: 0 10px;
}
</style>
</head>
<body>
<form class="form-inline"
action="${pageContext.request.contextPath }/book.action?methodName=list" method="post">
<div class="form-group mb-2">
<input type="text" class="form-control-plaintext" name="sname"
placeholder="请输入学生名称">
<!-- <input name="rows" value="20" type="hidden"> -->
<!-- 不想分页 -->
<input name="pagination" value="false" type="hidden">
</div>
<button type="submit" class="btn btn-primary mb-2">查询</button>
<a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/book.action?methodName=toEdit">新增</a>
</form>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">学生ID</th>
<th scope="col">学生名</th>
<th scope="col">年龄</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="b" items="${books }">
<tr>
<td>${b.sid }</td>
<td>${b.sname }</td>
<td>${b.age }</td>
<td>
<a href="${pageContext.request.contextPath }/book.action?methodName=toEdit&sid=${b.sid}">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=delete&sid=${b.sid}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- 这一行代码就相当于前面分页需求前端的几十行了 -->
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>
?运行结果:
查询单个:
修改:?
|