在分页的基础上完成增删改查
实体类
package com.zhoujun.entity;
public class Blog {
private String id;
private String job;
private String company;
private String address;
private String salary;
private String url;
private String limit;
private String time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getLimit() {
return limit;
}
public void setLimit(String limit) {
this.limit = limit;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Blog(String id, String job, String company, String address, String salary, String url, String limit,
String time) {
super();
this.id = id;
this.job = job;
this.company = company;
this.address = address;
this.salary = salary;
this.url = url;
this.limit = limit;
this.time = time;
}
public Blog() {}
@Override
public String toString() {
return "Blog [id=" + id + ", job=" + job + ", company=" + company + ", address=" + address + ", salary="
+ salary + ", url=" + url + ", limit=" + limit + ", time=" + time + "]";
}
}
dao方法
package com.zhoujun.dao;
import java.sql.SQLException;
import java.util.List;
import com.zhoujun.entity.Blog;
import com.zhoujun.util.BaseDao;
import com.zhoujun.util.PageBean;
import com.zhoujun.util.StringUtils;
public class BlogDao extends BaseDao<Blog> {
public List<Blog> list(Blog blog,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
//查询的sql的语句
String sql="select * from t_solr_job where true";
//可能需要带条件查询,那么那么是需要拼接查询sql
String company = blog.getCompany();
//如果前台jsp传了参数到后台
if(StringUtils.isNotBlank(company)) {
sql +=" and company like '%"+company+"%'";
}
//在修改的时候,查询被修改的那条记录信息,也就是说是数据
String id =blog.getId();
if(StringUtils.isNotBlank(id)) {
sql +="and id ="+id;
}
return super.executeQuery(sql, Blog.class, pageBean);
}
public int add(Blog blog) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
//书写新增信息的sql语句
String sql="insert into t_solr_job values(?,?,?,?,?,?,?,?)";
//调用父类basedao的方法完成新增功能
return super.executeUpdate(sql, blog, new String[] {"id","job","company","address","salary","url","limit","time"});
}
public int edit(Blog blog) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
//修改的sql语句
String sql="update t_solr_job set job=?,company=?,address=?,salary=?,url=?,limit=?,time=? where id=? ";
//调用父类basedao的方法完成修改功能
return super.executeUpdate(sql, blog, new String[] {"job","company","address","salary","url","limit","time"});
}
public int del(Blog blog) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
//删除的sql语句
String sql="delete from t_solr_job where id=? ";
//调用父类basedao的方法完成删除功能
return super.executeUpdate(sql, blog, new String[] {"id"});
}
}
BlogAction类
package com.zhoujun.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhoujun.dao.BlogDao;
import com.zhoujun.entity.Blog;
import com.zhoujun.util.PageBean;
import com.zhoujun.framework.ActionSupport;
import com.zhoujun.framework.ModelDriven;
public class BlogAction extends ActionSupport implements ModelDriven<Blog> {
private BlogDao blogDao =new BlogDao();
private Blog blog=new Blog();
//查询
public String list(HttpServletRequest req,HttpServletResponse resp) {
System.out.println("sadasdas");
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
try {
//默认是查询出第一页的数据
List<Blog> list = this.blogDao.list(blog, pageBean);
//将查询的结果展示到对应blogList.jsp
req.setAttribute("blogs", list);
//分页标签中使用
req.setAttribute("pageBean", pageBean);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
//blogList.jsp
return "list";
}
//跳转新增
public String toAdd(HttpServletRequest req,HttpServletResponse resp) {
//bookAdd.jsp
return "toAdd";
}
//新增
public String add(HttpServletRequest req,HttpServletResponse resp) {
//blog.action?methodName
try {
this.blogDao.add(blog);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
//跳转修改
public String toEdit(HttpServletRequest req,HttpServletResponse resp) {
//blogEdit.jsp
//查询当前要编辑信息
try {
Blog b = this.blogDao.list(blog, null).get(0);
req.setAttribute("b", b);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toEdit";
}
//修改
public String edit(HttpServletRequest req,HttpServletResponse resp) {
//blog.action?methodName
try {
this.blogDao.edit(blog);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
//删除
public String del(HttpServletRequest req,HttpServletResponse resp) {
//blog.action?methodName
try {
this.blogDao.del(blog);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
@Override
public Blog getModel() {
// TODO Auto-generated method stub
return blog;
}
}
jsp主界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="z" uri="/zhoujun" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta 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 }/blog.action?methodName=list" method="post">
<div class="form-group mb-2">
<input type="text" class="form-control-plaintext" name="company" placeholder="请输入公司名称">
</div>
<button type="button" class="btn btn-primary mb-2">查询</button>
<a href="${pageContext.request.contextPath}/blogAdd.jsp" class="btn btn-primary">新增</a>
</form>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">编号</th>
<th scope="col">行业</th>
<th scope="col">名称</th>
<th scope="col">地址</th>
<th scope="col">工资</th>
<th scope="col">url</th>
<th scope="col">学龄</th>
<!-- <th scope="col">时间</th> -->
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="b" items="${blogs }">
<tr>
<td>${b.id }</td>
<td>${b.job }</td>
<td>${b.company }</td>
<td>${b.address }</td>
<td>${b.salary }</td>
<td>${b.url }</td>
<td>${b.limitd }</td>
<td>
<a href="${pageContext.request.contextPath }/blog.action?methodName=del&id=${b.id }" class="btn btn-sm btn-danger mb-2 ml-4">删除</a>
<a href="${pageContext.request.contextPath }/blog.action?methodName=toEdit&id=${b.id }" class="btn btn-sm btn-success mb-2 ml-4">修改</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>
jsp增加界面
<%@ 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>
<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>
</head>
<body>
新增页面
<form action="${pageContext.request.contextPath }/blok.action?methodName=add" method="post">
<div class="form-group row">
<label for="id" class="col-sm-2 col-form-label">编号</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="id" name="bid" value="">
</div>
</div>
<div class="form-group row">
<label for="job" class="col-sm-2 col-form-label">行业</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="job" name="job">
</div>
</div>
<div class="form-group row">
<label for="company" class="col-sm-2 col-form-label">名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="company" name="company">
</div>
</div>
<div class="form-group row">
<label for="address" class="col-sm-2 col-form-label">地址</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="address" name="address">
</div>
</div>
<div class="form-group row">
<label for="salary" class="col-sm-2 col-form-label">工资</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="salary" name="salary">
</div>
</div>
<div class="form-group row">
<label for="url" class="col-sm-2 col-form-label">url</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="url" name="url">
</div>
</div>
<div class="form-group row">
<label for="limit" class="col-sm-2 col-form-label">学龄</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="limitd" name="limitd">
</div>
</div>
<!-- <div class="form-group row">
<label for="time" class="col-sm-2 col-form-label">时间</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="time" name="time">
</div>
</div> -->
<div class="form-group row">
<div class="col-sm-12">
<input type="submit" class="form-control bg-success" value="提交">
</div>
</div>
</form>
</body>
</html>
jsp修改界面
<%@ 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>
<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>
</head>
<body>
修改页面
<form action="${pageContext.request.contextPath }/blok.action?methodName=edit"
method="post">
<div class="form-group row">
<label for="id" class="col-sm-2 col-form-label">编号</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="id" name="bid" value="${b.id }">
</div>
</div>
<div class="form-group row">
<label for="job" class="col-sm-2 col-form-label">行业</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="job" name="job" value="${b.job }">
</div>
</div>
<div class="form-group row">
<label for="company" class="col-sm-2 col-form-label">名称</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="company" name="company" value="${b.company }">
</div>
</div>
<div class="form-group row">
<label for="address" class="col-sm-2 col-form-label">地址</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="address" name="address" value="${b.address }">
</div>
</div>
<div class="form-group row">
<label for="salary" class="col-sm-2 col-form-label">工资</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="salary" name="salary" value="${b.salary }">
</div>
</div>
<div class="form-group row">
<label for="url" class="col-sm-2 col-form-label">url</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="url" name="url" value="${b.url }">
</div>
</div>
<div class="form-group row">
<label for="limitd" class="col-sm-2 col-form-label">学龄</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="limitd" name="limitd" value="${b.limitd }">
</div>
</div>
<%-- <div class="form-group row">
<label for="time" class="col-sm-2 col-form-label">时间</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="time" name="time" value="${b.time }">
</div>
</div> --%>
<div class="form-group row">
<div class="col-sm-12">
<input type="submit" class="form-control bg-success" value="提交">
</div>
</div>
</form>
</body>
</html>