mybatis-pagehelper插件配置

1.pom.xml 引用pagerhelper插件

最新版本见github
         
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>4.1.6version>
        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过来的


<configuration>
    <settings>
        <setting name="cacheEnabled" value="true" />
        
    settings>
    <plugins>
        
        <plugin interceptor="com.github.pagehelper.PageHelper">
            
            
            
            
            <property name="offsetAsPageNum" value="true" />
            
            
            <property name="rowBoundsWithCount" value="true" />
            
            
            <property name="pageSizeZero" value="true" />
            
            
            
            <property name="reasonable" value="false" />
            
            
            
            
            
            
            <property name="supportMethodsArguments" value="false" />
            
            <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;

@Repository
public class ReportmalfuncMstServerImpl implements ReportmalfuncMstServer {

    @Autowired
    private ReportmalfuncMstMapper mapper;

    public PageInfo findAll(String term_mac, Integer page, Integer pageSize){
        PageHelper.startPage(page, pageSize);
        List list = mapper.selectByMac(term_mac);
        return new PageInfo(list);
    }
}

5.thymeleaf 查看视图


        <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_idth>
                                    <th>report_dtth>
                                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})}">«a>li>
                                <li >
                                    <a th:if="${not isFirstPage}" th:href="@{${'/index'}(pageNum=${pageNum-1},pageSize=${pageSize})}">Previousa>
                                    <a th:if="${isFirstPage}" href="javascript:void(0);">Previousa>
                                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})}">Nexta>
                                    <a th:if="${isLastPage}" href="javascript:void(0);">Nexta>
                                li>
                                <li><a th:href="@{${'/index'}(pageNum=${lastPage},pageSize=${pageSize})}">»a>li>
                            ul>
                        nav>
                    div>
                div>
            div>
        div>
        

你可能感兴趣的:(插件,github,个人知识管理)