This is the 30th day of my participation in the Wenwen Challenge

1. Introduction

This blog mainly uses SpringBoot to integrate Mybatis through annotations while using PageHelper to page the results, all the code involved has been uploaded to Github Mybatis – PageHelper. Here’s the integration process, and let’s get started:

2. Integrate the process

The final project structure is shown in the figure below:

2.1 Creating a Spring Boot Project

To create a New Spring Boot project and add Web components, follow the three ways I created a Spring Boot project in another blog post.

2.2 Adding POM Dependencies

Since we need to integrate MyBatis, we need to add mysql driver, SpringBoot MyBatis integration package, SpringBoot Mapper integration package and PageHelper in the configuration file of the project.

<! -- SpringBoot Mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>
<! -- Mysql driver -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<! --mapper-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.5</version>
</dependency>
<! -- pagehelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.13</version>
</dependency>
Copy the code

2.3 Preparing a Database

  1. Database creation and input insertion

Create table user (id, name, age); create table user (id, age);

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
  `name` varchar(50) DEFAULT NULL COMMENT 'name',
  `age` int(11) DEFAULT NULL COMMENT 'age'.PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

INSERT INTO  user values (1"Village Rain Yao ",25);
INSERT INTO  user values (2, "zhang",26);
INSERT INTO  user values (3, "bill",27);
Copy the code
  1. Data Source Configuration

Configure the data source in the project configuration file application.properties;

# database configuration
spring.datasource.username=root
spring.datasource.password=* * * *
spring.datasource.url=jdbc:mysql://localhost:3306/springboot? useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Copy the code

2.4 the pojo layer

Create entity classes from the database. To simplify the code, Lombok is more or less used for the rest of the process, so it needs to be introduced in pom.xml beforehand.

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
Copy the code
package com.cunyu.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/ * * *@author : cunyu
 * @version : 1.0
 * @className : User
 * @date: 2020/7/26 fell *@description: User Entity class */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
}

Copy the code

2.5 the dao layer

After the creation of the entity class, write the corresponding interface of the entity class;

package com.cunyu.dao;

import com.cunyu.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/ * * *@author : cunyu
 * @version : 1.0
 * @className : UserDao
 * @date : 2020/7/27 0:39
 * @description: User interface */

@Mapper
public interface UserDao {

    / * * *@param
     * @returnUser List@descriptionFind all users *@date2020/7/27 "*@author cunyu1943
     * @version1.0 * /
    @Select("SELECT id,name,age FROM user")
    List<User> queryAll(a);
}
Copy the code

2.6 the util layer

package com.cunyu.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/ * * *@author : cunyu
 * @version : 1.0
 * @className : PageHelperUtil
 * @date: 2020/7/27 he *@description: PageHelper paging result set */

@Data
public class PageHelperUtil<T> {
    /* Total data */
    private Long total;
    /* Number of pages */
    private Integer pageTotal;
    /* Current page */
    private Integer page;
    /* Number of entries per page */
    private Integer pageSize;
    /* Result set */
    private List<T> list;


}
Copy the code

2.7 the service layer

  1. The service interface
package com.cunyu.service;

import com.cunyu.pojo.User;
import com.cunyu.util.PageHelperUtil;

/ * * *@author : cunyu
 * @version : 1.0
 * @className : UserService
 * @date : 2020/7/27 0:38
 * @description: userService interface */

public interface UserService {
    PageHelperUtil<User> getUserByPage(Integer page, Integer pageSize);
}
Copy the code
  1. Service interface implementation class
package com.cunyu.service.impl;

import com.cunyu.dao.UserDao;
import com.cunyu.pojo.User;
import com.cunyu.service.UserService;
import com.cunyu.util.PageHelperUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/ * * *@author : cunyu
 * @version : 1.0
 * @className : UserServiceImpl
 * @date : 2020/7/27 0:38
 * @description: userService interface implementation class */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    public PageHelperUtil<User> getUserByPage(Integer page, Integer pageSize) {
        // Set the start page and the number of pages to display
        PageHelper.startPage(page, pageSize);
        / / query
        List<User> list = userDao.queryAll();
        // Send the query result to pageInfo for processing
        PageInfo<User> pageInfo = new PageInfo<>(list);
        // Create a result set object
        PageHelperUtil<User> result = new PageHelperUtil<>();
        // Encapsulates the result into a result set object, the current page
        result.setPage(page);
        / / every pages
        result.setPageSize(pageInfo.getPageSize());
        / / the total number of pages
        result.setPageTotal(pageInfo.getPages());
        / / the total number of article
        result.setPageTotal((int) pageInfo.getTotal());
        / / the result set
        result.setList(pageInfo.getList());

        returnresult; }}Copy the code

2.8 the controller layer

package com.cunyu.controller;

import com.cunyu.pojo.User;
import com.cunyu.service.UserService;
import com.cunyu.util.PageHelperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/ * * *@author : cunyu
 * @version : 1.0
 * @className : UserController
 * @date : 2020/7/27 0:38
 * @description : UserController
 */

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/users/{page}/{pageSize}")
    public PageHelperUtil<User> getUserByPage(@PathVariable Integer page, @PathVariable Integer pageSize) {
        System.out.println("Current page:" + page + "\t Number per page:" + pageSize);
        PageHelperUtil<User> users = userService.getUserByPage(page, pageSize);
        returnusers; }}Copy the code

2.9 Entry program configuration

Mapper automatic scanning is configured in the entry program;

package com.cunyu;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan(basePackages = "com.cunyu.dao")
@SpringBootApplication
public class MybatisXmlApplication {

    public static void main(String[] args) { SpringApplication.run(MybatisXmlApplication.class, args); }}Copy the code

2.10 Web testing

After completing all of the above steps, in the browser to http://localhost:8080/user/ {page} / {pageSize}, you can in a web page displays all the information corresponding to the id of the User object;

conclusion

Spring Boot + Mybatis PageHelper Spring Boot + Mybatis PageHelper If you have a better way, feel free to leave a comment!