• 自定义MVC增查


    目录

    一、配置自定义MVC环境

    1、将项目相关代码打包成jar包

    2、导入工程

    二、通用增删改的编写

    三、编写Dao方法层

     四、编写Action接收请求并处理

    五、编写service(相当于biz层)

    六、XML文件配置 

    七、前端代码


    一、配置自定义MVC环境

    1、将项目相关代码打包成jar包

    右键项目Export ==> Export...

    找到Java点击JAR flie 

     

    点击Next 注意勾选红色框框勾选的 随后建在对应的路径里,最后finish

     

    我们接着就把生成的jar包导入新的工程里--->并且把相关的依赖包也都导入--->再把我们分页标签的相关文件以及助手类导入;

    2、导入工程

    再建一个源文件夹:Source Folder

    将项目需要的config.xml文件 复制进来


    二、通用增删改的编写

    Student.java(学生实体类)

    1. package com.zking.model;
    2. public class Student {
    3. private Integer sid;
    4. private String sname;
    5. private Integer age;
    6. private String addr;
    7. public Integer getSid() {
    8. return sid;
    9. }
    10. public void setSid(Integer sid) {
    11. this.sid = sid;
    12. }
    13. public String getSname() {
    14. return sname;
    15. }
    16. public void setSname(String sname) {
    17. this.sname = sname;
    18. }
    19. public Integer getAge() {
    20. return age;
    21. }
    22. public void setAge(Integer age) {
    23. this.age = age;
    24. }
    25. public String getAddr() {
    26. return addr;
    27. }
    28. public void setAddr(String addr) {
    29. this.addr = addr;
    30. }
    31. public Student(Integer sid, String sname, Integer age, String addr) {
    32. super();
    33. this.sid = sid;
    34. this.sname = sname;
    35. this.age = age;
    36. this.addr = addr;
    37. }
    38. public Student() {
    39. // TODO Auto-generated constructor stub
    40. }
    41. @Override
    42. public String toString() {
    43. return "Student [sid=" + sid + ", sname=" + sname + ", age=" + age + ", addr=" + addr + "]";
    44. }
    45. }

    三、编写Dao方法层

    StudentDao(dao层)

    1. ppackage com.yian.dao;
    2. import java.sql.Connection;
    3. import java.sql.PreparedStatement;
    4. import java.sql.ResultSet;
    5. import java.sql.SQLException;
    6. import java.util.ArrayList;
    7. import java.util.List;
    8. import java.util.Objects;
    9. import org.junit.Test;
    10. import org.w3c.dom.ls.LSOutput;
    11. import com.zking.model.Student;
    12. import com.zking.util.BaseDao;
    13. import com.zking.util.BaseDao.ICovent;
    14. import com.zking.util.DBUtil;
    15. import com.zking.util.PageBean;
    16. public class StudentDao implements IStudentDao{
    17. @Override
    18. public List getStudents(Student stu ,PageBean pageBean) {
    19. String sql="select sid,sname,age,addr from student ";
    20. List param =new ArrayList();
    21. if(stu!=null&&stu.getSname()!=null&&!"".equals(stu.getSname())) {
    22. sql+=" where sname like ?";
    23. param.add("%"+stu.getSname()+"%");
    24. }
    25. return BaseDao.query(sql, param, pageBean,new ICovent() {
    26. @Override
    27. public List convent(ResultSet rs) throws SQLException {
    28. Listlist=new ArrayList();
    29. while(rs.next()) {
    30. Student s=new Student();
    31. s.setSid(rs.getInt("sid"));
    32. s.setSname(rs.getString("sname"));
    33. s.setAge(rs.getInt("age"));
    34. s.setAddr(rs.getString("addr"));
    35. list.add(s);
    36. }
    37. return list;
    38. }
    39. });
    40. }
    41. public void addStudents(Student student) {
    42. String sql="insert into student(sname,age,addr) values(?,?,?)";
    43. Connection con=null;
    44. PreparedStatement ps=null;
    45. try {
    46. con=DBUtil.getConection();
    47. ps=con.prepareStatement(sql);
    48. ps.setObject(1, student.getSname());
    49. ps.setObject(2,student.getAge());
    50. ps.setObject(3, student.getAddr());
    51. ps.executeUpdate();
    52. } catch (Exception e) {
    53. e.printStackTrace();
    54. }finally {
    55. DBUtil.closeDB(null, ps, con);
    56. }
    57. }
    58. public void updStudents(Student student) {
    59. String sql="update student set sname=?,age=?,addr=? where sid=?";
    60. Connection con=null;
    61. PreparedStatement ps=null;
    62. ResultSet rs=null;
    63. try {
    64. con=DBUtil.getConection();
    65. ps=con.prepareStatement(sql);
    66. ps.setObject(1, student.getSname());
    67. ps.setObject(2,student.getAge());
    68. ps.setObject(3, student.getAddr());
    69. ps.setObject(4, student.getSid());
    70. ps.executeUpdate();
    71. } catch (Exception e) {
    72. e.printStackTrace();
    73. }finally {
    74. DBUtil.closeDB(rs, ps, con);
    75. }
    76. }
    77. public void delStudents(int id) {
    78. String sql="delete from student where sid=?";
    79. Connection con=null;
    80. PreparedStatement ps=null;
    81. ResultSet rs=null;
    82. try {
    83. con=DBUtil.getConection();
    84. ps=con.prepareStatement(sql);
    85. ps.setInt(1, id);
    86. ps.executeUpdate();
    87. } catch (Exception e) {
    88. e.printStackTrace();
    89. }finally {
    90. DBUtil.closeDB(rs, ps, con);
    91. }
    92. }
    93. public Student queryById(int id) {
    94. Student s=new Student();
    95. String sql="select * from student where sid=?";
    96. Connection con=null;
    97. PreparedStatement ps=null;
    98. ResultSet rs=null;
    99. try {
    100. con=DBUtil.getConection();
    101. ps=con.prepareStatement(sql);
    102. ps.setInt(1, id);
    103. rs=ps.executeQuery();
    104. if(rs.next()) {
    105. s.setSid(rs.getInt(1));
    106. s.setSname(rs.getString(2));
    107. s.setAge(rs.getInt(3));
    108. s.setAddr(rs.getString(4));
    109. }
    110. } catch (Exception e) {
    111. e.printStackTrace();
    112. }finally {
    113. DBUtil.closeDB(rs, ps, con);
    114. }
    115. return s;
    116. }
    117. @Test
    118. public void testStudnet() {
    119. Student student = new Student();
    120. getStudents(student, new PageBean());
    121. }
    122. public static void main (String[] args)throws Exception{
    123. StudentDao dao = new StudentDao();
    124. PageBean pageBean = new PageBean();
    125. List students = dao.getStudents(null, pageBean);
    126. students.forEach(t-> System.out.println(t));
    127. }
    128. }
    129. IStudentDao

      1. package com.yian.dao;
      2. import java.util.List;
      3. import com.zking.model.Student;
      4. import com.zking.util.PageBean;
      5. public interface IStudentDao {
      6. ListgetStudents(Student stu,PageBean pageBean);
      7. void addStudents(Student student);
      8. void updStudents(Student student);
      9. Student queryById(int id);
      10. void delStudents(int id);
      11. }

       四、编写Action接收请求并处理

      StudentAction

      1. package com.yian.action;
      2. import java.util.List;
      3. import javax.servlet.http.HttpServletRequest;
      4. import javax.servlet.http.HttpServletResponse;
      5. import com.yian.service.IStudentService;
      6. import com.yian.service.StudentService;
      7. import com.zking.model.Student;
      8. import com.zking.mymvc.framework.AbstractDispatchAction;
      9. import com.zking.mymvc.framework.ModelDriver;
      10. import com.zking.util.PageBean;
      11. public class StudentAction extends AbstractDispatchAction implements ModelDriver {
      12. private Student s=new Student();
      13. @Override
      14. public Object getMode() {
      15. return s;
      16. }
      17. private IStudentService service=new StudentService();
      18. public String getStudents(HttpServletRequest req,HttpServletResponse resp) {
      19. PageBean pageBean=new PageBean();
      20. pageBean.setRequest(req);
      21. List students = service.getStudents(s, pageBean);
      22. req.setAttribute("students", students);
      23. return "students";
      24. }
      25. public String addStudents(HttpServletRequest req,HttpServletResponse resp) {
      26. service.addStudents(s);
      27. return "success";
      28. }
      29. public String updStudents(HttpServletRequest req,HttpServletResponse resp) {
      30. s.setSid(Integer.parseInt(req.getParameter("sid")));
      31. service.updStudents(s);
      32. return "success";
      33. }
      34. public String getStudentById(HttpServletRequest req,HttpServletResponse resp) {
      35. Student queryById = service.queryById(Integer.parseInt(req.getParameter("sid")));
      36. req.setAttribute("stu", queryById);
      37. return "update";
      38. }
      39. public String delStudentById(HttpServletRequest req,HttpServletResponse resp) {
      40. service.delStudents(Integer.parseInt(req.getParameter("sid")));
      41. return "success";
      42. }
      43. }

      五、编写service(相当于biz层)

      IStudentService

      1. package com.yian.service;
      2. import java.util.List;
      3. import com.zking.model.Student;
      4. import com.zking.util.PageBean;
      5. public interface IStudentService {
      6. ListgetStudents(Student stu,PageBean pageBean);
      7. void addStudents(Student student);
      8. void updStudents(Student student);
      9. Student queryById(int id);
      10. void delStudents(int id);
      11. }

       StudentService

      1. package com.yian.service;
      2. import java.util.List;
      3. import com.yian.dao.IStudentDao;
      4. import com.yian.dao.StudentDao;
      5. import com.zking.model.Student;
      6. import com.zking.util.PageBean;
      7. public class StudentService implements IStudentService{
      8. private IStudentDao dao=new StudentDao();
      9. @Override
      10. public List getStudents(Student stu,PageBean pageBean) {
      11. // TODO Auto-generated method stub
      12. return dao.getStudents(stu, pageBean);
      13. }
      14. @Override
      15. public void addStudents(Student student) {
      16. // TODO Auto-generated method stub
      17. dao.addStudents(student);
      18. }
      19. @Override
      20. public void updStudents(Student student) {
      21. dao.updStudents(student);
      22. }
      23. @Override
      24. public Student queryById(int id) {
      25. // TODO Auto-generated method stub
      26. return dao.queryById(id) ;
      27. }
      28. @Override
      29. public void delStudents(int id) {
      30. // TODO Auto-generated method stub
      31. dao.delStudents(id);
      32. }
      33. }

      六、XML文件配置 

      config.xml

      1. config[
      2. config (action*)>
      3. action (forward*)>
      4. forward EMPTY>
      5. action
      6. path CDATA #REQUIRED
      7. type CDATA #REQUIRED
      8. >
      9. forward
      10. name CDATA #REQUIRED
      11. path CDATA #REQUIRED
      12. redirect (true|false) "false"
      13. >
      14. ]>
      15. <config>
      16. <action path="/studentAction" type="com.yian.action.StudentAction">
      17. <forward name="students" path="/students.jsp" redirect="false"/>
      18. <forward name="success" path="/studentAction.action?methodName=getStudents"/>
      19. <forward name="update" path="updStudent.jsp"/>
      20. action>
      21. <action path="/bookAction" type="com.zking.mymvc.action.BookAction">
      22. <forward name="books" path="/books.jsp" redirect="true"/>
      23. action>
      24. config>

      七、前端代码

      主页界面

      1. <%@ page language="java" contentType="text/html; charset=UTF-8"
      2. pageEncoding="UTF-8"%>
      3. <%@taglib prefix="z" uri="/zking" %>
      4. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
      5. html>
      6. <html>
      7. <head>
      8. <meta charset="UTF-8">
      9. <title>Insert title heretitle>
      10. head>
      11. <body>
      12. <center>
      13. <h1>学生信息h1>
      14. <form action="<%=request.getContextPath()%>/student" method="post">
      15. <input type="text" name="sname"value="<%=request.getParameter("sname") == null ? "" : request.getParameter("sname")%>">
      16. <input type="submit" value="查询">
      17. <a href="<%=request.getContextPath()%>/addStudent.jsp">增加a>
      18. form>
      19. <table border>
      20. <tr>
      21. <td>学生学号td>
      22. <td>学生姓名td>
      23. <td>学生年龄td>
      24. <td>学生地址td>
      25. <td>学生操作td>
      26. tr>
      27. <c:forEach items="${students}" var="s">
      28. <tr>
      29. <td>${s.sid}td>
      30. <td>${s.sname}td>
      31. <td>${s.age}td>
      32. <td>${s.addr}td>
      33. <td>
      34. <a href="<%=request.getContextPath()%>/studentAction.action?methodName=getStudentById&sid=${s.sid}">修改a>
      35. <a href="<%=request.getContextPath()%>/studentAction.action?methodName=delStudentById&sid=${s.sid} "onclick="return confirm('确定删除吗?')">删除a>
      36. td>
      37. tr>
      38. c:forEach>
      39. table>
      40. <z:paging pageBean="${pageBean}"/>
      41. center>
      42. body>
      43. html>

      页面效果:

       

      增加界面

      1. <%@ page language="java" contentType="text/html; charset=UTF-8"
      2. pageEncoding="UTF-8"%>
      3. html>
      4. <html>
      5. <head>
      6. <meta charset="UTF-8">
      7. <title>Insert title heretitle>
      8. head>
      9. <body>
      10. <center>
      11. <h1>增加学生信息h1>
      12. <form action="<%=request.getContextPath()%>/studentAction.action?methodName=addStudents" method="post">
      13. 姓名:
      14. <input type="text" id="sname" name="sname"/><p>
      15. 年龄:
      16. <input type="text" name="age"/><p>
      17. 地址:
      18. <input type="text" name="addr"/><p>
      19. <p>
      20. <input type="submit" value="提交"/>
      21. form>
      22. center>
      23. body>
      24. html>

      页面效果:

        

      修改界面:

      1. <%@ page language="java" contentType="text/html; charset=UTF-8"
      2. pageEncoding="UTF-8"%>
      3. html>
      4. <html>
      5. <head>
      6. <meta charset="UTF-8">
      7. <title>Insert title heretitle>
      8. head>
      9. <body>
      10. <center>
      11. <h1>修改学生信息h1>
      12. <form action="<%=request.getContextPath()%>/studentAction.action?methodName=updStudents" method="post">
      13. <input type="hidden" id=sid name="sid" value="${stu.sid }"/>
      14. 姓名:
      15. <input type="text" id="sname" name="sname" value="${stu.sname }"/><p>
      16. 年龄:
      17. <input type="text" name="age" value="${stu.age }"/><p>
      18. 地址:
      19. <input type="text" name="addr" value="${stu.addr }"/><p>
      20. <p>
      21. <input type="submit" value="提交"/>
      22. form>
      23. center>
      24. body>
      25. html>

      页面效果:

      删除效果:

       

       

    130. 相关阅读:
      Day7力扣打卡
      MySql 数据库【分组查询】
      字符串的API
      JVM认识之垃圾收集算法
      Yalmip使用教程(6)-将约束条件写成矩阵形式
      spring-boot 依赖注入流程
      IB经济与商业可以一起选吗?
      Milvus向量数据库检索
      CubeMX RTOS Demo
      编程技巧│高级 markdown 编辑器的惊艳语法
    131. 原文地址:https://blog.csdn.net/weixin_62270300/article/details/125857475