mybatis pagehelp分页 Springmvc+Mybatis+pagehelper5.1.2+bootstrap table分页和分页查询

来源:互联网 发布:书画频道软件下载 编辑:程序博客网 时间:2024/06/05 15:11

本文使用pagehelper5.1.2版本+bootstrap table,还需要jsqlparser0.9.4

1.修改Mybatis配置文件
<!-- MyBatis配置 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="dataSource" />    <property name="mapperLocations" value="classpath*:com/jck/**/mapper/*.xml" />    <property name="plugins">        <array>            <bean class="com.github.pagehelper.PageInterceptor">                <property name="properties">                    <value>                        helperDialect=mysql                        reasonable=true                        supportMethodsArguments=true                        params=count=countSql                        autoRuntimeDialect=true                    </value>                </property>            </bean>        </array>    </property></bean>
2.controller层
/** * 主页 * @param page 起始页 * @param pageSize 每页最大条数 * @param wl 查询条件1 * @return gys 查询条件2 */@RequestMapping("main")public ModelAndView mainForm(HttpSession session,ModelAndView mv,        @RequestParam(required=true,defaultValue="1") Integer page,        @RequestParam(required=false,defaultValue="30") Integer pageSize,        String wl, String gys) {     //会给下面第一条查询进行分页     PageHelper.startPage(page, pageSize);    List<Depot> list = mainServer.getDepotList(session, wl, gys);    mv.addObject("depotList", list);    PageInfo<Depot> p = new PageInfo<Depot>(list);    mv.addObject("page", p);    if(null == wl || wl.equals("")) {        wl = "0";    }    if(null == gys || gys.equals("")) {        gys = "0";    }    mv.addObject("wl", wl);    mv.addObject("gys", gys);    mv.setViewName("depot");    return mv; }
3.server层
public List<Depotlog> getDepotList(HttpSession session, String wl, String gys) {    if(null == wl|| wl.equals("0")) {        wl= "";    }    if(null == gys|| gys.equals("0")) {        gys= "";    }    User user = (User)session.getAttribute("user");// user.type 1管理员 2仓库管理员 3供应商    if(user.getType() == 1) {        return depotlogDao.selectDepotListBy(wl, gys);    }else if(user.getType() == 2) {        return depotlogDao.selectDepotListByKC(user.getKcdd(), wl, gys);    }else {        return depotlogDao.selectDepotListByGYS(user.getGys(), gys);    }}
4.mapper
<!-- 查询所有库存记录 --><select id="selectDepotListBy" resultType="com.jck.entity.Depot" >    SELECT       *    FROM      depot d     LEFT JOIN goods g     ON d.wlid=g.id     LEFT JOIN supplier s     ON d.gysid=s.id     where       1=1     <if test="wl != '' and wl != null">        AND g.wl = #{wl}     </if>     <if test="gys != '' and gys != null">        AND s.gys = #{gys}     </if>     order by d.id desc</select>
5.JSP
5.1引入bootstrap table
<script src="/static/js/jquery.min.js"></script><script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script><script src="/static/js/bootstrap.min.js"></script><script src="/static/js/bootstrap-paginator.min.js"></script><script src="/static/js/bootstrap-table.min.js"></script><script src="/static/js/bootstrap-table-zh-CN.min.js"></script><link href="/static/css/bootstrap.min.css" rel="stylesheet"><link href="/static/css/bootstrap-table.css" rel="stylesheet">
5.2条件查询部分
<form action="depot" method="get"class="form-inline">                <div class="form-group">                    <label for="exampleInputName2">物料</label>                     <input type="text" pattern="^[0-9]*$"                        class="form-control"                        <c:if test="${wl == 0 || empty wl }">                            placeholder="请输入物料编码"                         </c:if>                        <c:if test="${wl != 0 && !empty wl }">                            value="${wl }"                         </c:if>                        name="wl " id="wl ">                </div>                <c:if test="${user.type != 3}">                    <div class="form-group">                        <label for="exampleInputEmail2">供应商</label>                         <input type="text" pattern="^[0-9]*$"                            class="form-control"                        <c:if test="${gys == 0 || empty gys }">                            placeholder="请输入供应商编码"                         </c:if>                        <c:if test="${gys != 0 && !empty gys }">                            value="${gys }"                         </c:if>                            name="gys " id="gys ">                    </div>                </c:if>                <button type="submit" class="btn btn-default">搜索</button>                <button type="reset" class="btn btn-default" id="reset">清空</button>            </form>
5.3bootstrap分页部分
    <!-- 固定表头-->    <table class="table table-striped table-bordered table-hover" data-height="700"  data-toggle="table" data-page-size="30">    <thead>        <tr id="table_head">            <th>物料凭证</th>            <th>物料凭<br>证项目</th>            <th>物料</th>            <th>物料描述</th>            <th>库存地点</th>            <th>移动类型</th>            <th>输入日期</th>            <th>输入时间</th>            <th>批次</th>            <th>采购订单</th>            <th>项目</th>            <th>数量</th>            <th>基本计<br>量单位</th>            <th>本位币金额</th>            <th>供应商</th>            <th>供应商描述</th>        </tr>        </thead>        <tbody>            <c:forEach var="item" items="${depotList}" varStatus="status">                <tr>                    <td>${item.wlpz}</td>                    <td>${item.wlpzxm}</td>                    <td>${item.wl}</td>                    <td>${item.wlms}</td>                    <td>${item.kcdd}</td>                    <td>${item.ydlx}</td>                    <td>${item.srrq}</td>                    <td>${item.srsj}</td>                    <td>${item.pc}</td>                    <td>${item.cgdd}</td>                    <td>${item.xm}</td>                    <td>${item.sl}</td>                    <td>${item.jbjl}</td>                    <td>${item.bwbje}</td>                    <td>${item.gys}</td>                    <td>${item.gysms}</td>                </tr>             </c:forEach>         </tbody>    </table>    <div id="page" style="float: right;"></div>
5.4bootstrap js部分
    $(function() {    var options = {        bootstrapMajorVersion : 1, //版本        currentPage : ${page.pageNum}, //当前页数        numberOfPages : 5, //最多显示Page页        totalPages : ${page.pages}, //所有数据可以显示的页数        itemTexts: function (type, page, current) {              switch (type) {                case "first":                  return "<span class='glyphicon glyphicon-fast-backward' aria-hidden='true'></span>";                case "prev":                  return "<span class='glyphicon glyphicon-backward' aria-hidden='true'></span>";                case "next":                  return "<span class='glyphicon glyphicon-forward' aria-hidden='true'></span>";                case "last":                  return "<span class='glyphicon glyphicon-fast-forward' aria-hidden='true'></span>";                case "page":                  return page;              }           },        onPageClicked : function(e, originalEvent, type, page) {            location.href = "/depot?page=" + page + "&wl=" + ${wl} + "&gys=" + ${gys};        }    }    $("#page").bootstrapPaginator(options);})$("#reset").click(function(){    $("#wl").attr("value",null);    $("#gys").attr("value",null);}); 
6.效果

效果

原创粉丝点击