SpringBoot配置Mybatis和PageHelper看这一篇就够了

540 阅读2分钟

引入Jar

一共需要引入3个Jar包,包括java连接mysql的jar包,springboot对mybatis和pageHelper的jar。


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>


        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>


其实也不需要引入三个,因为pagehelper里面已经引用了mybatis的jar包。

建库建表

建立数据库语句

CREATE DATABASE mybatis DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

建立数据库表语句

USE mybatis
CREATE TABLE country (
    id int  NOT NULL auto_increment,
    countryCode VARCHAR(255) NULL,
    countryName VARCHAR(255) NULL,
    PRIMARY KEY(id)
);

编写与数据库表对应的实体类

@Data
public class Country {
    private Integer id;
    private String countryCode;
    private String countryName;

}

编写mapper.xml

在resource下新建mapper文件夹,然后编写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.bf.spring4.mapper.CountryMapper">
    <select id="selectAll" resultType="com.bf.spring4.model.Country">

        select * from country
    </select>

</mapper>

编写对象的Mapper类

@Mapper
public interface CountryMapper {
    List<Country> selectAll();
}

这里使用@Mapper注解,其实也可以使用@Repository注解,关于这两个注解的不同,将在下篇博客介绍(juejin.cn/post/684490…)。

编写service层

到目前为止,还没有涉及到分页的知识。首先构建返回分页返回对象。

public class PagedResponse<T> implements Serializable {
    /**
     * 页数
     */
    private int pageNum;
    /**
     * 每页大小
     */
    private int pageSize;
    /**
     * 总页数
     */
    private long totalPages;
    /**
     * 总条数
     */
    private long totalCount;
    /**
     * 返回数据
     */
    private List<T> result;

    public PagedResponse() {
    }

    public PagedResponse(Integer pageNum, Integer pageSize, Long totalCount) {
        this.setPageSize(pageSize);
        this.setTotalCount(totalCount);
        this.setPageNum(pageNum);
    }

    public int getPageNum() {
        return this.pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
        if (pageNum < 1) {
            this.pageNum = 1;
        } else if ((long)pageNum > this.getTotalPages()) {
            this.pageNum = this.getTotalPages().intValue();
        }

    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotalPages() {
        return this.totalPages;
    }

    private void setTotalPages(Long totalPages) {
        this.totalPages = totalPages;
    }

    public Long getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(Long totalCount) {
        this.totalCount = totalCount;
        Long result = totalCount / (long)this.getPageSize();
        if (totalCount % (long)this.pageSize != 0L) {
            result = result + 1L;
        }

        this.setTotalPages(result);
    }

    public List<T> getResult() {
        return this.result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }
}

然后编写请求对象,这里面肯定要包括两个参数,pageNum和pageSize,这个一般由前端传来的值。

@Data
public class CountryReq implements Serializable {
    private Integer pageNum;
    private Integer pageSize;
}

使用pageHelper其实也就两三行代码


@Service
public class CountryServiceImpl implements CountryService  {
    @Autowired
    private CountryMapper countryMapper;

    @Override
    public PagedResponse<Country> selectAll(CountryReq req) {

        Page<Country> page = PageHelper.startPage(req.getPageNum(), req.getPageSize(), true);
        List<Country> countries = countryMapper.selectAll();
        PagedResponse<Country> response = new PagedResponse<>(req.getPageNum(), req.getPageSize(), page.getTotal());
        response.setResult(countries);
        return response;

    }
}

Controller编写

@RestController
public class CountryController {

    @Autowired
    private CountryService countryService;


    @GetMapping("/getCountry")
    public PagedResponse<Country> getCountry(@RequestBody(required = false) CountryReq req) {
        PagedResponse<Country> response = countryService.selectAll(req);
        return response;
    }


}

测试

设置请求参数为{"pageNum":"1","pageSize":"2"}

返回值为

{
    "pageNum": 1,
    "pageSize": 2,
    "totalPages": 6,
    "totalCount": 11,
    "result": [
        {
            "id": 1,
            "countryCode": "1",
            "countryName": "中国"
        },
        {
            "id": 2,
            "countryCode": "2",
            "countryName": "日本"
        }
    ]
}