mybatis-pagehelper插件配置

来源:互联网 发布:mac os 10.11 iso镜像 编辑:程序博客网 时间:2024/05/22 05:23

1.pom.xml 引用pagerhelper插件

最新版本见github
        <!-- 分页插件 -->         <dependency>            <groupId>com.github.pagehelper</groupId>            <artifactId>pagehelper</artifactId>            <version>4.1.6</version>        </dependency>

2.Config类,注解@Configuration

ps:该项目的mapper文件为java 文件,如果是xml 改为xml
/**     * pagehelper配置     */    @Bean    public SqlSessionFactory sqlSessionFactory() throws Exception {        System.out.println("注入pagehelper配置!!!");        final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();        sqlSessionFactory.setDataSource(dataSource());        sqlSessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));        sqlSessionFactory.setFailFast(true);        sqlSessionFactory.setMapperLocations(getResource("mapper", "**/*.java"));        return sqlSessionFactory.getObject();    }    public Resource[] getResource(String basePackage, String pattern) throws IOException {            String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(new StandardEnvironment().resolveRequiredPlaceholders(basePackage)) + "/" + pattern;            Resource[] resources = new PathMatchingResourcePatternResolver().getResources(packageSearchPath);            return resources;    }

3.配置mybatis-config.xml

下面代码网上直接copy过来的
<?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>    <settings>        <setting name="cacheEnabled" value="true" />        <!-- <setting name="lazyLoadingEnabled" value="true" /> <setting name="aggressiveLazyLoading"             value="true" /> <setting name="useGeneratedKeys" value="true" /> <setting             name="defaultExecutorType" value="SIMPLE" /> <setting name="defaultStatementTimeout"             value="10" /> -->    </settings>    <plugins>        <!-- com.github.pagehelper为PageHelper类所在包名 -->        <plugin interceptor="com.github.pagehelper.PageHelper">            <!-- 4.0.0以后版本可以不设置该参数   <property name="dialect" value="mysql" /> -->            <!-- 该参数默认为false -->            <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->            <!-- 和startPage中的pageNum效果一样 -->            <property name="offsetAsPageNum" value="true" />            <!-- 该参数默认为false -->            <!-- 设置为true时,使用RowBounds分页会进行count查询 -->            <property name="rowBoundsWithCount" value="true" />            <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->            <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型) -->            <property name="pageSizeZero" value="true" />            <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->            <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->            <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->            <property name="reasonable" value="false" />            <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->            <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->            <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->            <!-- 不理解该含义的前提下,不要随便复制该配置 -->            <!-- <property name="params"                value="pageNum=pageHelperStart;pageSize=pageHelperRows;" /> -->            <!-- 支持通过Mapper接口参数来传递分页参数 -->            <property name="supportMethodsArguments" value="false" />            <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->            <property name="returnPageInfo" value="none" />        </plugin>    </plugins></configuration>

4.调试测试

在impl调用mapper文件中 调用静态方法 PageHelper.startPage(page, pageSize);对象pageinfo 有很多参数可参考 官方文档
package lb.springboot.server.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import lb.springboot.model.ReportmalfuncMst2;import lb.springboot.model.mapper.ReportmalfuncMstMapper;import lb.springboot.server.ReportmalfuncMstServer;@Repositorypublic class ReportmalfuncMstServerImpl implements ReportmalfuncMstServer {    @Autowired    private ReportmalfuncMstMapper mapper;    public PageInfo<ReportmalfuncMst2> findAll(String term_mac, Integer page, Integer pageSize){        PageHelper.startPage(page, pageSize);        List<ReportmalfuncMst2> list = mapper.selectByMac(term_mac);        return new PageInfo<ReportmalfuncMst2>(list);    }}

5.thymeleaf 查看视图

<!-- WRAPPER -->        <div id="wrapper">            <div class="vertical-align-wrap">                <div class="vertical-align-middle">                    <div class="content">                        <table class="table table-hover" >                            <thead>                                <tr>                                    <th>malfunc_id</th>                                    <th>report_dt</th>                                </tr>                            </thead>                            <tr th:each="entity : ${entitys}">                                <th th:text="${entity.malfuncId}"></th>                                <th th:text="${#dates.format(entity.reportDt, 'yyyy-MM-dd')}"></th>                            </tr>                        </table>                        <nav>                            <ul class="pager">                                <li><a th:href="@{${'/index'}(pageNum=${firstPage},pageSize=${pageSize})}">&laquo;</a></li>                                <li >                                    <a th:if="${not isFirstPage}" th:href="@{${'/index'}(pageNum=${pageNum-1},pageSize=${pageSize})}">Previous</a>                                    <a th:if="${isFirstPage}" href="javascript:void(0);">Previous</a>                                </li>                                <li th:each="pageNo : ${#numbers.sequence(1, totalPages)}">                                    <a th:if="${pageNum eq pageNo}" href="javascript:void(0);">                                         <span th:text="${pageNo}"></span>                                    </a>                                    <a th:if="${not (pageNum eq pageNo)}" th:href="@{${'/index'}(pageNum=${pageNo},size=${pageSize})}">                                        <span th:text="${pageNo}"></span>                                    </a>                                </li>                                <li>                                    <a th:if="${not isLastPage}" th:href="@{${'/index'}(pageNum=${pageNum+1},size=${pageSize})}">Next</a>                                    <a th:if="${isLastPage}" href="javascript:void(0);">Next</a>                                </li>                                <li><a th:href="@{${'/index'}(pageNum=${lastPage},pageSize=${pageSize})}">&raquo;</a></li>                            </ul>                        </nav>                    </div>                </div>            </div>        </div>        <!-- END WRAPPER -->