SpringBoot+Mybatis+Pagehelper分页

1、pom.xml

<!-- mybatis分页插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

2、驼峰命名
在application.properties中添加以下配置,在执行查询后,可以将数据库的NN_NN格式字段,在java结果集对象中自动转换成驼峰命名参数。

mybatis.configuration.mapUnderscoreToCamelCase=true

3、可复用的@Results
a、声明时给id赋值为user

@Results(id="user",value={
        @Result(property="nnNn",column="NN_NN")
})

b、在其他方法中,重复使用id为user的结果映射

@ResultMap("user")

c、结果映射@Results
如果结果集不是JAVA对象而是Map,Map中的列名会和数据库中的NN_NN一样,是不会自动驼峰转换的。可以使用@Result来指明结果映射,同样也适用JAVA对象

@Results({
    @Result(property="nnNn",column="NN_NN")
})
@Select("select * from  user")
public List<Map> findAll();

4、打印SQL日志到控制台
在application.properties中添加以下配置

logging.level.你的包名.mybatis接口包=debug


第一行:==>左边是执行SQL的接口及其方法,右边是执行语句
第二行:传参数1,String类型
第三行:查到一行数据

5、分页

package com.lynch.mapper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lynch.entity.UserEntity;

@Service
@Transactional
public class PageServiceImpl {
    @Autowired
    private PageMapper pageMapper;

    public Page<UserEntity> pageUser(int pageNum, int pageSize) {
        // 分页插件: 查询第1页,每页10行
        Page<UserEntity> page = PageHelper.startPage(pageNum, pageSize);
        pageMapper.getAll();
        // 数据表的总行数
        page.getTotal();
        // 分页查询结果的总行数
        page.size();
        // 第一个User对象,参考list,序号0是第一个元素,依此类推
        page.get(0);

        return page;
    }
}

分页原理:PageHelper.startPage会拦截下一个sql,也就是pageMapper.getAll()的SQL。并且根据当前数据库的语法,把这个SQL改造成一个高性能的分页SQL,同时还会查询该表的总行数,具体可以看SQL日志。
PageHelper.startPage和pageMapper.getAll()最好紧跟在一起,中间不要有别的逻辑,否则可能出BUG。
Page<User> page:相当于一个list集合,findAll()方法查询完成后,会给page对象的相关参数赋值。

6、回传ID
假设数据库表的ID主键是自动增长的,现在添加一条数据,想要得到这条数据自动增长的ID,方法如下:
a、dao层
useGeneratedKeys=true:获取数据库生成的主键
keyProperty="id":把主键值存入User param对象的id属性

@Insert("insert into users(username,password,sex) values(#{username}, #{password}, #{sex})")
@Options(useGeneratedKeys=true, keyProperty="id")
int insert(UserEntity user);

b、service层

UserEntity userEntity = new UserEntity("laosis", "123456", SexEnum.WOMAN);
int result = pageMapper.insert(userEntity);
System.out.println("result=" + result);
System.out.println("回传ID值:" + userEntity.getId());

完整demo代码
1、PageMapper

package com.lynch.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import com.lynch.entity.UserEntity;

public interface PageMapper {
    @Select("select * from users order by id")
    List<UserEntity> getAll();

    @Insert("insert into users(username,password,sex) values(#{username}, #{password}, #{sex})")
    @Options(useGeneratedKeys=true, keyProperty="id")
    int insert(UserEntity user);
}

2、PageServiceImpl -- 分页service

package com.lynch.mapper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lynch.entity.UserEntity;

@Service
@Transactional
public class PageServiceImpl {
    @Autowired
    private PageMapper pageMapper;

    public Page<UserEntity> pageUser(int pageNum, int pageSize) {
        // 分页插件: 查询第1页,每页10行
        Page<UserEntity> page = PageHelper.startPage(pageNum, pageSize);
        pageMapper.getAll();
        // 数据表的总行数
        page.getTotal();
        // 分页查询结果的总行数
        page.size();
        // 第一个User对象,参考list,序号0是第一个元素,依此类推
        page.get(0);

        return page;
    }
}

3、PageMapperTest -- 单元测试

package com.lynch.mapper;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.github.pagehelper.Page;
import com.lynch.entity.UserEntity;
import com.lynch.enums.SexEnum;

@RunWith(SpringRunner.class)
@SpringBootTest
public class PageMapperTest {
    @Autowired
    private PageMapper pageMapper;
    @Autowired
    private PageServiceImpl pageService;

    @Test
    public void getAll() throws Exception {
        List<UserEntity> users = pageMapper.getAll();
        for(UserEntity user : users) {
            System.out.println(user);
        }

    }

    @Test
    public void pageUser() throws Exception {
        int pageNum = 1;
        int pageSize = 10;
        Page<UserEntity> page = pageService.pageUser(pageNum, pageSize);
        System.out.println("page:" + page);
        for(UserEntity user : page.getResult()) {
            System.out.println(user);
        }

    }

    @Test
    public void insert() throws Exception {
        UserEntity userEntity = new UserEntity("laosis", "123456", SexEnum.WOMAN);
        int result = pageMapper.insert(userEntity);
        System.out.println("result=" + result);
        System.out.println("回传ID值:" + userEntity.getId());
    }

}

原文地址:https://www.cnblogs.com/linjiqin/p/9687491.html

时间: 2024-08-30 09:05:37

SpringBoot+Mybatis+Pagehelper分页的相关文章

SpringBoot+MyBatis+PageHelper分页无效

POM.XML中的配置如下:<!-- 分页插件 --><!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --><dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.1

springboot+mybatis+pagehelper

springboot+mybatis+pagehelper整合 springboot   版本2.1.2.RELEASE mybatis  版本3.5 pagehelper 版本5.18 支持在mapper接口上直接写sql语句,支持在xml文件里写sql语句,支持分页查询,支持自定义sql语句 注意在生成mybatisGenerator文件时,可能在xml文件中生成了多个selectByExampleWithRowbounds语句,保留一个即可. 项目结构 application.proper

springboot + mybatis配置分页插件

1:首先配置springboot +mybatis框架  参考:http://www.cnblogs.com/liyafei/p/7911549.html 2:创建配置类MybatisConfig,对分页插件进行配置.将mybatis-config.xml移动到classpath路径下. package com.liyafei.util.pagehelper; import java.util.Properties; import org.apache.ibatis.plugin.Interce

SpringBoot整合Pagehelper分页插件

在web开发中,数据的分页是必不可少的.Pagehelper分页插件很强大,虽说平时我们不需要用到它的很多功能,但是了解下还是有必要的. 官网:https://pagehelper.github.io/ 注:在 MyBatis下使用. 一.Pagehelper分页插件介绍 原文地址:https://www.cnblogs.com/myitnews/p/12349655.html

springboot+mybatis集成分页功能

1.搭建使用idea搭建srpingboot项目 在pom.xml文件中引入如下的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- swagger文档相关依赖--> <dependency> <gro

springboot集成pagehelper分页插件

之前写的项目都是在前端进行分页,最近涉及到后台分页查询,回看自己之前练习的项目里发现自己写了分页给忘了,作为初级程序员拿来记录一下 引入pagehelper的pom依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependen

SpringBoot集成MyBatis的分页插件PageHelper

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

SpringBoot集成MyBatis的分页插件PageHelper(回头草)

俗话说:好??不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用

SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了.全部自动实现. 话不多说,直接上代码: 第一步pom文件配置添加jar: <!-- mybatis的分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>