wjy0703
wjy0703
采纳率100%
2018-03-22 09:02 阅读 2.3k

gradle+spring boot+mybatis+PageHelper 分页无效

20

被折磨了一天,网上也找了很多解决办法,都不行,求助大神
如果PageHelper 降版本到4.0的话,PageInfo里的属性就全是空的,
直接用PageHelper.startPage(1, 2); 也没用。
,不废话,直接上代码。
测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMappertest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void getAllByPage(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("email", "a");
//      map.put("id", "2");
        PageHelper.startPage(1, 2,true);// 默认从第一页开始,每页五条
        List<Map<String,Object>> users = userMapper.getAllByPage(map);

        PageInfo<Map<String,Object>> pageBlog = new PageInfo<Map<String,Object>>(users);// 将查出来的blogArticleList绑定到pageInfo中

        System.out.println(">======page====>"+pageBlog.toString());

        int a = 1;
        for(Map<String,Object> m:users) {
            System.out.println(">============users===a====>"+a);
            System.out.println(">============users===username====>"+m.get("username"));
            a++;
        }
    }
}

输出结果:

 >======page====>PageInfo{pageNum=1, pageSize=7, size=7, startRow=0, endRow=6, total=7, pages=1, list=[{password=$76NoUnuTJ8iAtKIUi, name=qweq,id=1, avatar=//localhost:8081/view/5a9f9509c99a7138a8ea306c, email=i@om, username=admin}, {password=$2a$IUi, name=Wu, id=2, avatar=//localhost:8081/view/5a9f962ec99a7138a8ea306d, email=wa@wa.com, username=wau}, {password=$2a$1O7s3Jo4XW, name=是释放, id=3, avatar=//localhost:8081/view/5a9f9684c99a7138a8ea306e, email=12321@sfa.cn, username=wjy0703}, {password=123456, name=fs士大夫撒, id=4, avatar=//localhost:8081/view/5aa777c3c99a715b60793591, email=safdaf@ssfdsa.com, username=wjy}, {password=$2EXdOSDS, name=发大好时光, id=6, avatar=//localhost:8081/view/5aa77a8cc99a715b60793592, email=123212@sfa.cn, username=wjiyu}, {password=123456, name=搭顺风很舒服, id=7, email=safdafaaa@ssfdsa.com, username=qwe123}, {password=$2QB6cdbsAySst80C9FS, name=safddasfa, id=9, email=fas2f@sfa.com, username=abc123}], prePage=0, nextPage=0, isFirstPage=true, isLastPage=true, hasPreviousPage=false, hasNextPage=false, navigatePages=8, navigateFirstPage=1, navigateLastPage=1, navigatepageNums=[1]}
>============users===a====>1
>============users===username====>admin
>============users===a====>2
>============users===username====>waylau
>============users===a====>3
>============users===username====>wjy0703
>============users===a====>4
>============users===username====>wjy
>============users===a====>5
>============users===username====>wayu
>============users===a====>6
>============users===username====>qwe123
>============users===a====>7
>============users===username====>abc123

gradle配置 springBootVersion = '1.5.2.RELEASE':

 // 添加 MySQL连接驱动 的依赖
    compile('mysql:mysql-connector-java:6.0.5')

    // 添加 Apache Commons Lang 依赖
    compile('org.apache.commons:commons-lang3:3.5')

    //mybatis依赖
    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0')

    //pagehelper依赖
    compile('com.github.pagehelper:pagehelper:5.1.2')

mybatis-config.xml 配置:
下面使用"com.github.pagehelper.PageHelper"也不行,都不报错,也不能分页。

<?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>
    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer" />
        <typeAlias alias="Long" type="java.lang.Long" />
        <typeAlias alias="HashMap" type="java.util.HashMap" />
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
    </typeAliases>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">

        </plugin>
    </plugins>
</configuration> 

application.properties 配置:

 #Thymeleaf 
spring.thymeleaf.encoding=UTF-8
#热部署静态文件
spring.thymeleaf.cache=false
#使用HTML5标准
spring.thymeleaf.mode=HTML5

#DataSource
spring.datasource.url=jdbc:mysql://localhost/boot?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.username=boot
spring.datasource.password=boot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# mybatis配置 mybatis.config-location=classpath:mybatis-config.xml // 配置文件位置
mybatis.typeAliasesPackage=com.educate.domain 
mybatis.mapper-locations=classpath:com/educate/mapper/**/*.xml 
mybatis.config-locations=classpath:mybatis/mybatis-config.xml

#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true


#JPA
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

UserMapper.xml 配置:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.educate.mapper.user.UserMapper" >

    <sql id="Base_Column_List" >
        id,name, username, password, email,avatar
    </sql>

    <sql id="Base_Column_Where" >
        where 1=1
        <if test="id != null"> and id=#{id}</if>
        <if test="name != null"> and name like '%${name}%'</if>
        <if test="email != null"> and email like '%${email}%'</if>
    </sql>

    <select id="getAll" resultType="java.util.HashMap"  parameterType="java.util.HashMap">
       SELECT 
       <include refid="Base_Column_List" />
       FROM user
       <include refid="Base_Column_Where" />
    </select>

    <select id="getAllByPage" resultType="java.util.HashMap">
       SELECT 
       <include refid="Base_Column_List" />
       FROM user
       <include refid="Base_Column_Where" />
    </select>
</mapper>

UserMapper 代码:


@Mapper
public interface UserMapper {

    List<Map<String,Object>> getAll(Map<String,Object> map);

    List<Map<String,Object>> getAllByPage(Map<String,Object> page);

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    mikeywangtao mikeywangtao 2018-03-28 03:39

    应该是没有读取你的mybatis-config.xml配置文件
    把spring配置文件里 的 mybatis.config-locations 改成 mybatis.config-location 试试

    点赞 1 评论 复制链接分享
  • hx1666503534 hx1666503534 2018-03-22 10:06

    PageHelper 分页需要Sql解析工具jsqlparser.jar。

    点赞 评论 复制链接分享

相关推荐