Mybatis +Servlet+JSP+PageHelp插件分页

需求: 实现简单的插件分页;没有条件时,就查询所有的数据;有条件时,就根据条件来查询数据哦!

一、Mybatis底层配置文件mybatis-cfg.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--1>引用外部的配置数据连接-->
    <properties resource="database.properties"/>



    <typeAliases>
        <!--2.如果是很多实体类:注意写包名;那就直接写实体类名-->
        <package name="com.hlx.entity"/>
    </typeAliases>
    
    <!--分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

    
    <!--2>配置连接-->
    <environments default="development">
        <environment id="development">
            <!--事物管理-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据源连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <!--多个文件时,可以用包 -->
        <package name="com.hlx.mapper" />
    </mappers>

</configuration>

二、实体类与Mapper接口与配置文件逆向工程自动生成

三、业务类

 public List<Userinfo> getPageInfoById(int id){
        SqlSession session = null;

        try {

            //会话对象
            session = MybatisUtil.getSession();

            //获得接口对象
            UserinfoMapper mapper = session.getMapper(UserinfoMapper.class);

            //
            //实例化对象
            UserinfoExample example = new UserinfoExample();

            //创建条件对象
            UserinfoExample.Criteria criteria = example.createCriteria();

            if(id!=0) {
                //条件查询
                criteria.andGenderEqualTo(id);
            }
            /

            //调用方法
            return mapper.selectByExample(example);

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            //关闭会话
            MybatisUtil.closeSession(session);
        }

        return null;
    }

四、Servlet控制器

 private void findById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //处理编码
        response.setContentType("text/html;charset=utf-8");

        //输出对象
        PrintWriter out = response.getWriter();

        //中文处理
        request.setCharacterEncoding("utf-8");


        //获取性别ID
       String sex= request.getParameter("sex");
       int gender=0;

       if(sex!=null) {
            gender = Integer.parseInt(sex);

            request.setAttribute("sex",gender);
            System.out.println("gender=>"+gender);
       }

        //业务对象
        UserinfoService service =new UserinfoService();


        //请求数据
        String str =request.getParameter("pageIndex");

        //判断
        int pageIndex=(str==null)?(1):(Integer.parseInt(str));

        //分页对象
        Page<Userinfo> page= PageHelper.startPage(pageIndex,4);

        //调用业务方法
        List<Userinfo> list =service.getPageInfoById(gender);

        //获取分页
        PageInfo<Userinfo> pageInfo =page.toPageInfo();

        //保存数据
        request.setAttribute("list",list);
        request.setAttribute("pageinfo",pageInfo);

        //跳转
        request.getRequestDispatcher("index.jsp").forward(request,response);


        out.println();


        //关闭
        out.flush();
        out.close();
    }

五、JSP页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>

<select name="sex" onchange="fun(this.value)">
    <option value="0">请选择</option>
    <option value="1" <c:if test="${param.sex==1}">selected</c:if> >男</option>
    <option value="2" <c:if test="${param.sex==2}">selected</c:if>>女</option>
</select>

<table border="1" width="80%" align="center" cellspacing="0" cellpadding="0">
    <tr>
        <td>id</td>
        <td>usercode</td>
        <td>username</td>
        <td>userpassword</td>
        <td>gender</td>
        <td>birthday</td>
    </tr>

    <c:forEach items="${list}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.usercode}</td>
            <td>${user.username}</td>
            <td>${user.userpassword}</td>
            <td>${user.gender}</td>
            <td><fmt:formatDate value="${user.birthday}"/> </td>
        </tr>
    </c:forEach>



</table>

<p align="center">


    共${pageinfo.total}条/总${pageinfo.pages}页
    <a href="all?pageIndex=1&sex=${sex}">首页</a>
    <a href="all?pageIndex=${pageinfo.prePage}&sex=${sex}">上一页</a>
    <a href="all?pageIndex=${pageinfo.nextPage}&sex=${sex}">下一页</a>
    <a href="all?pageIndex=${pageinfo.pages}&sex=${sex}">尾页</a>


    <c:forEach items="${pageinfo.navigatepageNums}" var="i">
        <a href="all?pageIndex=${i}&sex=${sex}">${i}</a>
    </c:forEach>


</p>

</body>
</html>

<script type="text/javascript">
    function fun(gender){
       document.location.href="all?sex="+gender;
    }

</script>

效果:

 

根据性别条件分页

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值