目录
右键项目Export ==> Export...
找到Java点击JAR flie
点击Next 注意勾选红色框框勾选的 随后建在对应的路径里,最后finish
我们接着就把生成的jar包导入新的工程里--->并且把相关的依赖包也都导入--->再把我们分页标签的相关文件以及助手类导入;
再建一个源文件夹:Source Folder
将项目需要的config.xml文件 复制进来
Student.java(学生实体类)
- package com.zking.model;
-
- public class Student {
- private Integer sid;
-
- private String sname;
-
- private Integer age;
-
- private String addr;
-
-
- public Integer getSid() {
- return sid;
- }
-
- public void setSid(Integer sid) {
- this.sid = sid;
- }
-
- public String getSname() {
- return sname;
- }
-
- public void setSname(String sname) {
- this.sname = sname;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public String getAddr() {
- return addr;
- }
-
- public void setAddr(String addr) {
- this.addr = addr;
- }
-
-
- public Student(Integer sid, String sname, Integer age, String addr) {
- super();
- this.sid = sid;
- this.sname = sname;
- this.age = age;
- this.addr = addr;
- }
-
- public Student() {
- // TODO Auto-generated constructor stub
- }
-
- @Override
- public String toString() {
- return "Student [sid=" + sid + ", sname=" + sname + ", age=" + age + ", addr=" + addr + "]";
- }
-
- }
StudentDao(dao层)
- ppackage com.yian.dao;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Objects;
-
- import org.junit.Test;
- import org.w3c.dom.ls.LSOutput;
-
- import com.zking.model.Student;
- import com.zking.util.BaseDao;
- import com.zking.util.BaseDao.ICovent;
- import com.zking.util.DBUtil;
- import com.zking.util.PageBean;
-
- public class StudentDao implements IStudentDao{
-
- @Override
- public List
getStudents(Student stu ,PageBean pageBean) { -
- String sql="select sid,sname,age,addr from student ";
- List
- if(stu!=null&&stu.getSname()!=null&&!"".equals(stu.getSname())) {
- sql+=" where sname like ?";
- param.add("%"+stu.getSname()+"%");
- }
- return BaseDao.query(sql, param, pageBean,new ICovent
() { -
- @Override
- public List
convent(ResultSet rs) throws SQLException { - List
list=new ArrayList(); - while(rs.next()) {
- Student s=new Student();
- s.setSid(rs.getInt("sid"));
- s.setSname(rs.getString("sname"));
- s.setAge(rs.getInt("age"));
- s.setAddr(rs.getString("addr"));
- list.add(s);
- }
- return list;
- }
- });
-
- }
-
- public void addStudents(Student student) {
- String sql="insert into student(sname,age,addr) values(?,?,?)";
- Connection con=null;
- PreparedStatement ps=null;
- try {
- con=DBUtil.getConection();
- ps=con.prepareStatement(sql);
- ps.setObject(1, student.getSname());
- ps.setObject(2,student.getAge());
- ps.setObject(3, student.getAddr());
- ps.executeUpdate();
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- DBUtil.closeDB(null, ps, con);
- }
- }
-
- public void updStudents(Student student) {
- String sql="update student set sname=?,age=?,addr=? where sid=?";
- Connection con=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- con=DBUtil.getConection();
- ps=con.prepareStatement(sql);
- ps.setObject(1, student.getSname());
- ps.setObject(2,student.getAge());
- ps.setObject(3, student.getAddr());
- ps.setObject(4, student.getSid());
- ps.executeUpdate();
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- DBUtil.closeDB(rs, ps, con);
- }
- }
-
-
- public void delStudents(int id) {
- String sql="delete from student where sid=?";
- Connection con=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- con=DBUtil.getConection();
- ps=con.prepareStatement(sql);
- ps.setInt(1, id);
- ps.executeUpdate();
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- DBUtil.closeDB(rs, ps, con);
- }
- }
-
-
- public Student queryById(int id) {
- Student s=new Student();
- String sql="select * from student where sid=?";
- Connection con=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- con=DBUtil.getConection();
- ps=con.prepareStatement(sql);
- ps.setInt(1, id);
- rs=ps.executeQuery();
- if(rs.next()) {
- s.setSid(rs.getInt(1));
- s.setSname(rs.getString(2));
- s.setAge(rs.getInt(3));
- s.setAddr(rs.getString(4));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- DBUtil.closeDB(rs, ps, con);
- }
- return s;
- }
-
-
-
- @Test
- public void testStudnet() {
- Student student = new Student();
- getStudents(student, new PageBean());
- }
-
- public static void main (String[] args)throws Exception{
- StudentDao dao = new StudentDao();
- PageBean pageBean = new PageBean();
- List
students = dao.getStudents(null, pageBean); -
- students.forEach(t-> System.out.println(t));
- }
-
-
- }
IStudentDao
- package com.yian.dao;
-
- import java.util.List;
-
- import com.zking.model.Student;
- import com.zking.util.PageBean;
-
- public interface IStudentDao {
-
- List
getStudents(Student stu,PageBean pageBean); - void addStudents(Student student);
- void updStudents(Student student);
- Student queryById(int id);
- void delStudents(int id);
- }
StudentAction
- package com.yian.action;
-
- import java.util.List;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.yian.service.IStudentService;
- import com.yian.service.StudentService;
- import com.zking.model.Student;
- import com.zking.mymvc.framework.AbstractDispatchAction;
- import com.zking.mymvc.framework.ModelDriver;
- import com.zking.util.PageBean;
-
- public class StudentAction extends AbstractDispatchAction implements ModelDriver {
-
- private Student s=new Student();
- @Override
- public Object getMode() {
-
- return s;
- }
-
- private IStudentService service=new StudentService();
-
- public String getStudents(HttpServletRequest req,HttpServletResponse resp) {
- PageBean pageBean=new PageBean();
- pageBean.setRequest(req);
- List
students = service.getStudents(s, pageBean); -
- req.setAttribute("students", students);
-
- return "students";
- }
-
- public String addStudents(HttpServletRequest req,HttpServletResponse resp) {
-
- service.addStudents(s);
- return "success";
- }
-
- public String updStudents(HttpServletRequest req,HttpServletResponse resp) {
- s.setSid(Integer.parseInt(req.getParameter("sid")));
- service.updStudents(s);
- return "success";
- }
-
- public String getStudentById(HttpServletRequest req,HttpServletResponse resp) {
- Student queryById = service.queryById(Integer.parseInt(req.getParameter("sid")));
- req.setAttribute("stu", queryById);
- return "update";
- }
-
- public String delStudentById(HttpServletRequest req,HttpServletResponse resp) {
- service.delStudents(Integer.parseInt(req.getParameter("sid")));
- return "success";
- }
-
- }
IStudentService
- package com.yian.service;
-
- import java.util.List;
-
- import com.zking.model.Student;
- import com.zking.util.PageBean;
-
- public interface IStudentService {
-
- List
getStudents(Student stu,PageBean pageBean); - void addStudents(Student student);
- void updStudents(Student student);
- Student queryById(int id);
- void delStudents(int id);
- }
StudentService
- package com.yian.service;
-
- import java.util.List;
-
- import com.yian.dao.IStudentDao;
- import com.yian.dao.StudentDao;
- import com.zking.model.Student;
- import com.zking.util.PageBean;
-
- public class StudentService implements IStudentService{
-
- private IStudentDao dao=new StudentDao();
- @Override
- public List
getStudents(Student stu,PageBean pageBean) { - // TODO Auto-generated method stub
- return dao.getStudents(stu, pageBean);
- }
- @Override
- public void addStudents(Student student) {
- // TODO Auto-generated method stub
- dao.addStudents(student);
- }
-
- @Override
- public void updStudents(Student student) {
- dao.updStudents(student);
- }
- @Override
- public Student queryById(int id) {
- // TODO Auto-generated method stub
- return dao.queryById(id) ;
- }
- @Override
- public void delStudents(int id) {
- // TODO Auto-generated method stub
- dao.delStudents(id);
- }
-
- }
config.xml
- config[
- config (action*)>
- action (forward*)>
- forward EMPTY>
- action
- path CDATA #REQUIRED
- type CDATA #REQUIRED
- >
- forward
- name CDATA #REQUIRED
- path CDATA #REQUIRED
- redirect (true|false) "false"
- >
- ]>
- <config>
- <action path="/studentAction" type="com.yian.action.StudentAction">
- <forward name="students" path="/students.jsp" redirect="false"/>
- <forward name="success" path="/studentAction.action?methodName=getStudents"/>
- <forward name="update" path="updStudent.jsp"/>
- action>
- <action path="/bookAction" type="com.zking.mymvc.action.BookAction">
- <forward name="books" path="/books.jsp" redirect="true"/>
- action>
- config>
主页界面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@taglib prefix="z" uri="/zking" %>
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title heretitle>
- head>
- <body>
- <center>
- <h1>学生信息h1>
- <form action="<%=request.getContextPath()%>/student" method="post">
- <input type="text" name="sname"value="<%=request.getParameter("sname") == null ? "" : request.getParameter("sname")%>">
- <input type="submit" value="查询">
-
- <a href="<%=request.getContextPath()%>/addStudent.jsp">增加a>
-
- form>
- <table border>
- <tr>
- <td>学生学号td>
- <td>学生姓名td>
- <td>学生年龄td>
- <td>学生地址td>
- <td>学生操作td>
- tr>
- <c:forEach items="${students}" var="s">
- <tr>
- <td>${s.sid}td>
- <td>${s.sname}td>
- <td>${s.age}td>
- <td>${s.addr}td>
- <td>
- <a href="<%=request.getContextPath()%>/studentAction.action?methodName=getStudentById&sid=${s.sid}">修改a>
- <a href="<%=request.getContextPath()%>/studentAction.action?methodName=delStudentById&sid=${s.sid} "onclick="return confirm('确定删除吗?')">删除a>
- td>
- tr>
- c:forEach>
- table>
-
- <z:paging pageBean="${pageBean}"/>
- center>
-
- body>
- html>
页面效果:
增加界面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title heretitle>
- head>
- <body>
- <center>
- <h1>增加学生信息h1>
-
- <form action="<%=request.getContextPath()%>/studentAction.action?methodName=addStudents" method="post">
- 姓名:
- <input type="text" id="sname" name="sname"/><p>
- 年龄:
- <input type="text" name="age"/><p>
- 地址:
- <input type="text" name="addr"/><p>
- <p>
- <input type="submit" value="提交"/>
- form>
- center>
- body>
- html>
页面效果:
修改界面:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title heretitle>
- head>
- <body>
- <center>
- <h1>修改学生信息h1>
-
- <form action="<%=request.getContextPath()%>/studentAction.action?methodName=updStudents" method="post">
- <input type="hidden" id=sid name="sid" value="${stu.sid }"/>
- 姓名:
- <input type="text" id="sname" name="sname" value="${stu.sname }"/><p>
- 年龄:
- <input type="text" name="age" value="${stu.age }"/><p>
- 地址:
- <input type="text" name="addr" value="${stu.addr }"/><p>
- <p>
- <input type="submit" value="提交"/>
- form>
- center>
- body>
- html>
页面效果:
删除效果: