- 一、搭建自定义mvc框架环境
- 二、基础的增删改
- 三、通用的增删改
- 四、查询删除及重复表单提交问题
- 五、新增修改的前端实现
首先导入自定义mvcjar包,然后导入jar包需要的依赖,
然后再导入通用分页的类
配置web.xml
Yzp_mvc_crud mvc com.yzp.framework.DispatcherServlet configLocation /yzp.xml mvc *.action
配置文件yzp.xml
下面展示 基础增删改代码。
package com.yzp.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.yzp.entity.Stu; import com.yzp.util.BaseDao; import com.yzp.util.DBAccess; import com.yzp.util.PageBean; import com.yzp.util.StringUtils; public class StuDao extends BaseDao三、通用的增删改{ //查询 public List list(Stu stu,PageBean pagebean) throws Exception{ String sql = "select * from tb_stu where 1=1"; String sname=stu.getSname(); if(StringUtils.isNotBlank(sname)) { sql+=" and sname like '%"+sname+"%' "; } int sid = stu.getSid(); //前台jsp传递到后台,只要传了就有值,没传就是默认值,默认值就是0 if(sid!=0) { sql+=" and sid ="+sid; } return super.executeQuery(sql, pagebean, rs ->{ List list = new ArrayList<>(); try { while(rs.next()) { list.add(new Stu(rs.getInt("sid"), rs.getString("sname"), rs.getInt("jid"), rs.getInt("bid"), rs.getString("hid"))); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }); } //增 public int add(Stu stu) throws Exception { Connection con = DBAccess.getConnection(); String sql = "insert into tb_stu values(?,?,?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setObject(1, stu.getSid()); ps.setObject(2, stu.getSname()); ps.setObject(3, stu.getJid()); ps.setObject(4, stu.getBid()); ps.setObject(5, stu.getHid()); return ps.executeUpdate(); } // public int add(Stu stu) throws Exception { // Connection con = DBAccess.getConnection(); // String sql = "insert into tb_stu values(?,?,?,?,?)"; // return super.executeUpdate(sql, stu, new String[] {"sid","sname","jid","bid","hid"}); // } //删 public int del(Stu stu) throws Exception { Connection con = DBAccess.getConnection(); String sql = "delete from tb_stu where sid=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setObject(1, stu.getSid()); return ps.executeUpdate(); } // public int del(Stu stu) throws Exception { // String sql = "delete from tb_stu where sid=?"; // return super.executeUpdate(sql, stu,new String[] {"sid"}); // } //改 public int edit(Stu stu) throws Exception { Connection con = DBAccess.getConnection(); String sql = "update tb_stu set sname=?,jid=?,bid=?,hid=? where sid=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setObject(1, stu.getSname()); ps.setObject(2, stu.getJid()); ps.setObject(3, stu.getBid()); ps.setObject(4, stu.getHid()); ps.setObject(5, stu.getSid()); return ps.executeUpdate(); } // public int edit(Stu stu) throws Exception { // String sql = "update tb_stu set sname=?,jid=?,bid=?,hid=? where sid=?"; // return super.executeUpdate(sql, stu, new String[] {"sname","jid","bid","hid","sid"}); // } }
在basedao里面写一个==public int executeUpdate(String sql,T t,String[] attrs) ==的方法,然后写dao方法的时候返回即可
下面展示 basedao代码片。
public int executeUpdate(String sql,T t,String[] attrs) throws Exception {
Connection con = DBAccess.getConnection();
PreparedStatement ps = con.prepareStatement(sql);
//将t的某一个属性对应值加到pst对象中
for (int i = 0; i < attrs.length; i++) {
Field f = t.getClass().getDeclaredField(attrs[i]);
f.setAccessible(true);
ps.setObject(i+1, f.get(t));
}
// ps.setObject(2, stu.getSname());
// ps.setObject(3, stu.getJid());
// ps.setObject(4, stu.getBid());
// ps.setObject(5, stu.getHid());
return ps.executeUpdate();
}
下面展示studao代码。
package com.yzp.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.yzp.entity.Stu; import com.yzp.util.BaseDao; import com.yzp.util.DBAccess; import com.yzp.util.PageBean; import com.yzp.util.StringUtils; public class StuDao extends BaseDao四、查询删除及重复表单提交问题{ //查询 public List list(Stu stu,PageBean pagebean) throws Exception{ String sql = "select * from tb_stu where 1=1"; String sname=stu.getSname(); if(StringUtils.isNotBlank(sname)) { sql+=" and sname like '%"+sname+"%' "; } int sid = stu.getSid(); //前台jsp传递到后台,只要传了就有值,没传就是默认值,默认值就是0 if(sid!=0) { sql+=" and sid ="+sid; } return super.executeQuery(sql, pagebean, rs ->{ List list = new ArrayList<>(); try { while(rs.next()) { list.add(new Stu(rs.getInt("sid"), rs.getString("sname"), rs.getInt("jid"), rs.getInt("bid"), rs.getString("hid"))); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }); } //增 // public int add(Stu stu) throws Exception { // Connection con = DBAccess.getConnection(); // String sql = "insert into tb_stu values(?,?,?,?,?)"; // PreparedStatement ps = con.prepareStatement(sql); // ps.setObject(1, stu.getSid()); // ps.setObject(2, stu.getSname()); // ps.setObject(3, stu.getJid()); // ps.setObject(4, stu.getBid()); // ps.setObject(5, stu.getHid()); // return ps.executeUpdate(); // } public int add(Stu stu) throws Exception { Connection con = DBAccess.getConnection(); String sql = "insert into tb_stu values(?,?,?,?,?)"; return super.executeUpdate(sql, stu, new String[] {"sid","sname","jid","bid","hid"}); } //删 // public int del(Stu stu) throws Exception { // Connection con = DBAccess.getConnection(); // String sql = "delete from tb_stu where sid=?"; // PreparedStatement ps = con.prepareStatement(sql); // ps.setObject(1, stu.getSid()); // return ps.executeUpdate(); // } public int del(Stu stu) throws Exception { String sql = "delete from tb_stu where sid=?"; return super.executeUpdate(sql, stu,new String[] {"sid"}); } //改 // public int edit(Stu stu) throws Exception { // Connection con = DBAccess.getConnection(); // String sql = "update tb_stu set sname=?,jid=?,bid=?,hid=? where sid=?"; // PreparedStatement ps = con.prepareStatement(sql); // ps.setObject(1, stu.getSname()); // ps.setObject(2, stu.getJid()); // ps.setObject(3, stu.getBid()); // ps.setObject(4, stu.getHid()); // ps.setObject(5, stu.getSid()); // return ps.executeUpdate(); // } public int edit(Stu stu) throws Exception { String sql = "update tb_stu set sname=?,jid=?,bid=?,hid=? where sid=?"; return super.executeUpdate(sql, stu, new String[] {"sname","jid","bid","hid","sid"}); } }
下面展示 代码。
package com.yzp.web; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.yzp.dao.StuDao; import com.yzp.entity.Stu; import com.yzp.framework.ActionSupport; import com.yzp.framework.ModelDriver; import com.yzp.util.PageBean; public class StuAction extends ActionSupport implements ModelDriver五、新增修改的前端实现{ private Stu stu = new Stu(); private StuDao sd = new StuDao(); @Override public Stu getModel() { return stu; } //增加 public String add(HttpServletRequest req, HttpServletResponse resp) { try { sd.add(stu); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //代表跳到查询界面 return "tolist"; } //删 public String del(HttpServletRequest req, HttpServletResponse resp) { try { sd.del(stu); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //代表跳到查询界面 return "tolist"; } //修改 public String edit(HttpServletRequest req, HttpServletResponse resp) { try { sd.edit(stu); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //代表跳到查询界面 return "tolist"; } //查询 public String list(HttpServletRequest req, HttpServletResponse resp) { try { PageBean pagebean = new PageBean(); pagebean.setRequest(req); List list = sd.list(stu, pagebean); req.setAttribute("list", list); req.setAttribute("pagebean", pagebean); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //执行查询展示 return "list"; } //跳转到新增/修改界面 public String preEdit(HttpServletRequest req, HttpServletResponse resp) { try { int sid = stu.getSid(); if(sid!=0) { //传递sid到后台,有且只能查出一条数据,那也就意味着集合只有一条 List list = sd.list(stu, null); req.setAttribute("b", list.get(0)); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //代表跳到编辑界面 return "toEdit"; } }
下面展示一些 内联代码片。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here



