思维导图及效果
1、 一般增删改查
每个方法都要写一个对应的servlet来处理业务逻辑,创建的类太多了
,如下图,每个方法都要Servlet,就比较麻烦。
2、二般增删改查(嘿嘿)
然后就为了方便,又出现了第二种增删改查的方法,只要一个类,传一个methodName用来区分需要调用的方法,像下面这样用if,else区分,但这样就会造成分支太多,代码臃肿;处理前端jsp传递到后端值得封装代码量过大。
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String methodName = req.getParameter("methodName");
if("list".equals(methodName)) {
list(req, resp);
}else if("add".equals(methodName)) {
add(req, resp);
}else if("toEdit".equals(methodName)) {
toEdit(req, resp);
}else if("edit".equals(methodName)) {
edit(req, resp);
}else if("del".equals(methodName)) {
del(req, resp);
}
}
3、然后就出现了第三种——自定义mvc
通过反射动态方法,读写属性。通过xml配置去跳转页面,更加简洁。
具体思路流程如下图:
后面可以直接打jar包调用。
自定义mvc的增删改查的效果(实现了分页查询,模糊查询,增加,修改,删除功能)
案例讲解
从内向外讲解
1、jar包and工具包
2、mvc.xml配置文件
用来跳转页面,更加方便
<?xml version="1.0" encoding="utf-8"?>
<config>
<action path="/job" type="com.wangjuanxia.web.JobAction">
<forward name="list" path="/Joblist.jsp" redirect="false" />
<forward name="toEdit" path="/JobEdit.jsp" redirect="false" />
<forward name="toAdd" path="/JobAdd.jsp" redirect="" />
<forward name="toList" path="/job.action?methodName=list" redirect="" />
</action>
</config>
3、BaseDao里面写通用方法executeQuery,executeUpdate
分页的通用方法(executeQuery)我之前的博客里写过了就不再重复了,可以去看我之前的博客~
executeUpdate:传一个sql语句,一个泛型,属性值(用一个数组存储传过来)利用反射存储属性;
package com.wangjuanxia.util;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BaseDao<T> {
public List<T> executeQuery(String sql,Class clz,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException{
System.out.println("BaseDao");
List<T> list=new ArrayList<T>();
Connection con=DBAccess.getConnection();
PreparedStatement ps=null;
ResultSet rs=null;
if(pageBean!=null&&pageBean.isPagination()) {
String countSql=getCountSql(sql);
ps=con.prepareStatement(countSql);
rs=ps.executeQuery();
if(rs.next()) {
pageBean.setTotal(rs.getObject(1).toString());
}
String pageSql=getPageSql(sql,pageBean);
ps=con.prepareStatement(pageSql);
rs=ps.executeQuery();
}else {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
}
while(rs.next()) {
T t=(T) clz.newInstance();
for (Field f: clz.getDeclaredFields()) {
f.setAccessible(true);
f.set(t, rs.getObject(f.getName()));
}
list.add(t);
}
DBAccess.close(con, ps, rs);
return list;
}
private String getPageSql(String sql, PageBean pageBean) {
// TODO Auto-generated method stub
return sql+" limit "+pageBean.getStartIndex()+","+pageBean.getRows()+"";
}
private String getCountSql(String sql) {
// TODO Auto-generated method stub
return "select count(1) from ("+sql+") t";
}
public int executeUpdate(String sql,T t,String[] attrs) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Connection con = DBAccess.getConnection();
PreparedStatement ps=con.prepareStatement(sql);
int loop=1;
Field f=null;
for (String attr : attrs) {
f = t.getClass().getDeclaredField(attr);
f.setAccessible(true);
ps.setObject(loop++, f.get(t));
}
int code = ps.executeUpdate();
DBAccess.close(con, ps, null);
return code;
}
}
4、JobDao
jobdao继承BaseDao调用它的通用方法之后,增删改查的方法就只需要寥寥数行了,比起之前要简洁许多。写好sql语句然后调用通用方法就好了;
public class JobDao extends BaseDao<Job>{
public List<Job> list(Job job,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
System.out.println("jobdao");
String sql="select * from t_solr_job where true";
String company=job.getCompany();
String jid=job.getId();
if(StringUtils.isNotBlank(company)) {
sql+=" and company like '%"+company+"%'";
}
if(StringUtils.isNotBlank(jid)) {
sql+=" and id="+jid+"";
}
return super.executeQuery(sql, Job.class, pageBean);
}
public int add(Job job) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="insert into t_solr_job values(?,?,?,?,?,?,?,?,?,?,?)";
return super.executeUpdate(sql, job, new String[] {"id","job","company","address","salary","url","limit","time","desc","jobHandle","addressHandle"});
}
public int edit(Job job) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="update t_solr_job set `job`=?,`company`=?,`address`=?,`salary`=?,`url`=?,`limit`=?,`time`=?,`desc`=?,`jobHandle`=?,`addressHandle`=? where `id`=?";
return super.executeUpdate(sql, job, new String[] {"job","company","address","salary","url","limit","time","desc","jobHandle","addressHandle","id"});
}
public int del(Job job) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="delete from t_solr_job where id=?";
return super.executeUpdate(sql, job, new String[] {"id"});
}
}
5、Action接口
定义一个execute方法
package com.wangjuanxia.framework;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Action {
String execute(HttpServletRequest req,HttpServletResponse resp) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException;
}
6、ActionSuppor类
实现Action接口,重写execute方法,获取到前台传过来的要调用的方法名,反射动态调用方法。
package com.wangjuanxia.framework;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ActionSupport implements Action{
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
System.out.println("ActionSupport");
String methodName = req.getParameter("methodName");
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
return (String) m.invoke(this, req,resp);
}
}
7、ModelDriven
模型驱动类
相当于:private Book book = new Book();
package com.wangjuanxia.framework;
public interface ModelDriver<T> {
T getModel();
}
8、JobAction
处理网络请求
继承ActionSupport
实现ModelDriven
处理各个方法的业务逻辑,调用方法
然后返回字符串对应mvc.xml里跳转界面。
package com.wangjuanxia.web;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wangjuanxia.dao.JobDao;
import com.wangjuanxia.entity.Job;
import com.wangjuanxia.framework.ActionSupport;
import com.wangjuanxia.framework.ModelDriver;
import com.wangjuanxia.util.PageBean;
public class JobAction extends ActionSupport implements ModelDriver<Job>{
private JobDao jd=new JobDao();
private Job job=new Job();
private String edit(HttpServletRequest req,HttpServletResponse resp) {
System.out.println("处理修改");
try {
int e = this.jd.edit(job);
} 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";
}
private String del(HttpServletRequest req,HttpServletResponse resp) {
System.out.println("删除");
try {
this.jd.del(job);
} 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";
}
private String toEdit(HttpServletRequest req,HttpServletResponse resp) {
System.out.println("跳转修改界面");
Job j;
try {
j = this.jd.list(job, null).get(0);
req.setAttribute("j", j);
} 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";
}
private String add(HttpServletRequest req,HttpServletResponse resp) {
System.out.println("增加界面");
try {
this.jd.add(job);
} 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";
}
private String list(HttpServletRequest req,HttpServletResponse resp) {
System.out.println("jobaction");
Job job=new Job();
job.setCompany(req.getParameter("company"));
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
List<Job> list;
try {
list = this.jd.list(job, pageBean);
req.setAttribute("list", list);
} 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();
}
req.setAttribute("pageBean", pageBean);
return "list";
}
private String toAdd(HttpServletRequest req,HttpServletResponse resp) {
System.out.println("跳转增加界面");
return "toAdd";
}
@Override
public Job getModel() {
return job;
}
}
9、DispatherSerlvet
中央控制器
继承HttpServlet
实例化处理网络请求URL的类
动态封装参数
将前端jsp参数传递到后端的所有值封装到业务模型类中
动态调用方法
package com.wangjuanxia.framework;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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 org.apache.commons.beanutils.BeanUtils;
@WebServlet(name="dispatherServlet",urlPatterns="*.action")
public class DispatherServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private ConfigModel configModel;
@Override
public void init() throws ServletException {
try {
configModel= ConfigModelFactory.build();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("DispatherServlet");
String uri = req.getRequestURI();
String path=uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
ActionModel actionModel = configModel.pop(path);
if(actionModel==null) {
throw new RuntimeException("action 标签没配置");
}
try {
Action action=(Action) Class.forName(actionModel.getType()).newInstance();
if(action instanceof ModelDriver) {
ModelDriver md=(ModelDriver) action;
BeanUtils.populate(md.getModel(), req.getParameterMap());
}
String code = action.execute(req, resp);
ForwardModel forwardModel = actionModel.pop(code);
String jspPath=forwardModel.getPath();
if(forwardModel.isRedirect()) {
resp.sendRedirect(req.getServletContext()+jspPath);
}else {
req.getRequestDispatcher(jspPath).forward(req, resp);
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException 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();
}
}
}
10、JSP页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="/wangjuanxia" prefix="w" %>
<!DOCTYPE html>
<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>
<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>
<title>书籍列表</title>
</head>
<body>
<br/><br/>
<form class="form-inline" action="${pageContext.request.contextPath }/job.action?methodName=list" method="post">
<div class="form-group mb-2">
<input type="text" class="form-control-plaintext" name="company" placeholder="请输入书籍名称" />
</div>
<button type="submit" class="btn btn-primary mb-2">查询</button>
<a href="${pageContext.request.contextPath }/JobAdd.jsp" class="btn btn-success mb-2 ml-4">增加</a>
</form>
<br/>
<br/>
<table class="table table-striped" style="width: 100px;">
<thead>
<tr align="center" >
<th scope="col">编号</th>
<th scope="col">岗位</th>
<th scope="col">公司</th>
<th scope="col">地址</th>
<th scope="col">薪水</th>
<th scope="col">网址</th>
<th scope="col">学历</th>
<th scope="col">时间</th>
<th scope="col">备注</th>
<th scope="col">jobhandle</th>
<th scope="col">addresshandle</th>
</tr>
</thead>
<tbody>
<c:forEach var="j" items="${list }">
<tr align="center">
<th scope="row" >${j.id }</th>
<td >${j.job }</td>
<td >${j.company }</td>
<td >${j.address }</td>
<td >${j.salary }</td>
<td >${j.url }</td>
<td >${j.limit }</td>
<td >${j.time }</td>
<td >${j.desc }</td>
<td >${j.jobhandle }</td>
<td >${j.addresshandle }</td>
<td><a href="${pageContext.request.contextPath }/job.action?methodName=del&id=${j.id}" class="btn btn-danger mb-2">删除</a>
<a href="${pageContext.request.contextPath }/job.action?methodName=toEdit&id=${j.id}" class="btn btn-warning mb-2 ml-4">修改</a> </td>
</tr>
</c:forEach>
</tbody>
</table>
<w:page pageBean="${pageBean }"></w:page>
</body>
</html>
总结
希望这次分享能帮到你们,帮不到也没关系,反正肯定不是我的问题,白白~٩꒰▽ ꒱۶⁼³₌₃