mybatis+easyUI实现分页

来源:互联网 发布:医学数据分析是搞什么 编辑:程序博客网 时间:2024/06/06 00:25

最近在学习淘淘商城,我使用的IDEA工具,学习过程中遇到了很多的坑,在使用mybatis分页插件的时候发现在商品了表展示页面没有进行分页,而是一页显示所有商品信息。

首先需要在pom文件中添加依赖,导入paheHelper插件

  <!-- Mybatis -->    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis</artifactId>      <version>${mybatis.version}</version>    </dependency>    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis-spring</artifactId>      <version>${mybatis.spring.version}</version>    </dependency>    <dependency>      <groupId>com.github.miemiedev</groupId>      <artifactId>mybatis-paginator</artifactId>      <version>${mybatis.paginator.version}</version>    </dependency>    <dependency>      <groupId>com.github.pagehelper</groupId>      <artifactId>pagehelper</artifactId>      <version>${pagehelper.version}</version>    </dependency>

新建EasyUI返回的json数据格式的实体

public class EasyUIDataGridResult {    //总数    private long total;    // 每页数量    private List<?> rows;    public long getTotal() {        return total;    }    public void setTotal(long total) {        this.total = total;    }    public List<?> getRows() {        return rows;    }    public void setRows(List<?> rows) {        this.rows = rows;    }}

在application-datasource.xml文件中配置

  <!-- 配置SqlsessionFactory -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 加载mybatis的配置文件 -->        <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"/>        <!-- 配置数据源 -->        <property name="dataSource" ref="dataSource"/>        <property name="plugins">            <array>                <bean class="com.github.pagehelper.PageHelper">                    <property name="properties">                        <value>                            dialect=mysql                        </value>                    </property>                </bean>            </array>        </property>    </bean>

不配置这个则会出现开头所说的情况(一页显示全部信息)
service层代码:

   @Override    public EasyUIDataGridResult getItemList(int page, int rows) {        //分页处理        PageHelper.startPage(page,rows);        //执行查询        TbItemExample example = new TbItemExample();        List<TbItem> list = itemMapper.selectByExample(example);        //取分页信息        PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list);        //返回处理结果        EasyUIDataGridResult result = new EasyUIDataGridResult();        result.setRows(list);        result.setTotal(pageInfo.getTotal());        return result;    }

dao层可以使用逆向工程自动生成。
前台easyUI代码段:

<table class="easyui-datagrid" id="itemList" title="商品列表"        data-options="singleSelect:false,collapsible:true,pagination:true,url:'/item/list',method:'get',pageSize:30,toolbar:toolbar"> </table>
原创粉丝点击