li81514
2015-05-27 08:38
采纳率: 100%
浏览 2.2k
已采纳

MVC实现连接数据库 实现按条件查询

mvc结构 连接数据库 类色图书馆管理系统 但不要那么复杂的 我只要里面的分类查询显示 这一块 简单的就行 给例子 谢谢 大神了哈!!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 神奇的程序员 2015-05-27 10:08
    已采纳

    list页面 相当于V
    <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


    首页


    添加学生

    学号: 姓名: name="name"> 班级名称:


    ${bj.b_name }
    /c:forEach


















    /c:if


    /c:if





    /c:forEach
    学号 姓名 性别 生日日期 爱好 班级名称 操作
    ${stu.xh} ${stu.name} ${stu.birthday} ${stu.favorite} ${stu.b_name} 修改
    删除





    test="${pageNow> 1}">
    上页
    /c:if
    下页
    /c:if 当前第 /

    $(document).ready(function() { $("#pre").click(function() { $("#form").attr('action', 'index.jsp?pageNow=${pageNow-1}'); $("#form").submit(); }); $("#next").click(function() { $("#form").attr('action', 'index.jsp?pageNow=${pageNow+1}'); $("#form").submit(); }); $("#btn").click(function() { $("#form").attr('action', 'index.jsp'); $("#form").submit(); }); });

    servlet 相当于C
    public class listStudent extends HttpServlet {

    private static final long serialVersionUID = -6441193787541923748L;
    
    private StudentService studentService = new StudentServiceImpl();
    private BJService bJService = new BJServiceImpl();
    
    /**
     * @Description: 处理doGet方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    
        // 交给doPost处理
        doPost(req, resp);
    }
    
    /**
     * @Description: 处理doPost方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    
        final int pageSize = 3;
        int pageNow = 1;
        int pageCount = 1;
        // 查询条件封装成map集合
        Map<String, String> paramsMap = new HashMap<String, String>();
    
        if (StringUtils.isNotBlank(req.getParameter("xh"))) {
            paramsMap.put("xh", req.getParameter("xh"));
        }
        if (StringUtils.isNotBlank(req.getParameter("name"))) {
            paramsMap.put("name", req.getParameter("name"));
        }
        if (StringUtils.isNotBlank(req.getParameter("b_no"))) {
            paramsMap.put("b_no", req.getParameter("b_no"));
        }
        // 设置当前页
        if (StringUtils.isNotBlank(req.getParameter("pageNow"))) {
            pageNow = Integer.parseInt(req.getParameter("pageNow"));
        }
        List<Student> listStudent = null;
        List<BJ> listBJ=null;
        try {
            // 根据pageSize,pageNow和parmasMap查询所有符合条件的学生
            listStudent = studentService.listStudent(pageSize, pageNow,
                    paramsMap);
            listBJ=bJService.listAllBJ();
            // 获取页数
            pageCount = studentService.getCount(pageSize, paramsMap);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 学生数据集合
        req.setAttribute("list", listStudent);
        //班级数据集合
        req.setAttribute("listBJ", listBJ);
        // 总页数
        req.setAttribute("pageCount", pageCount);
        // 当前页
        req.setAttribute("pageNow", pageNow);
    
        req.getRequestDispatcher("/listStudent.jsp").forward(req, resp);
    }
    
    javaBean 相当于M
    public class Student {
    
    private int xh;// 学号
    private String name;// 姓名
    private int sex;// 性别,0表示男,1表示女
    private String birthday;// 生日
    private String favorite;// 爱好
    private String b_name;// 班级名称
    private int b_no;//班级号
    
    public int getXh() {
        return xh;
    }
    
    public void setXh(int xh) {
        this.xh = xh;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getSex() {
        return sex;
    }
    
    public void setSex(int sex) {
        this.sex = sex;
    }
    
    public String getBirthday() {
        return birthday;
    }
    
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    
    public String getFavorite() {
        return favorite;
    }
    
    public void setFavorite(String favorite) {
        this.favorite = favorite;
    }
    
    public String getB_name() {
        return b_name;
    }
    
    public void setB_name(String b_name) {
        this.b_name = b_name;
    }
    
    public int getB_no() {
        return b_no;
    }
    
    public void setB_no(int b_no) {
        this.b_no = b_no;
    }
    

    }

    点赞 打赏 评论

相关推荐