1. Introduce dependencies

<! -- mybatis -->

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>2.0.0</version>

</dependency>

<! Mapper -->

<dependency>

<groupId>tk.mybatis</groupId>

<artifactId>mapper-spring-boot-starter</artifactId>

<version>2.0.0</version>

</dependency>

<! -- mysql -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

<! -- druid connection pool -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid-spring-boot-starter</artifactId>

<version>1.1.14</version>

</dependency>

<! -- Paging plugin -->

<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper-spring-boot-starter</artifactId>

<version>1.2.10</version>

</dependency>

Copy the code

2. Modify the configuration file

server:

port: 8080



spring:

datasource:

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://localhost:3306/admin? useUnicode=true&characterEncoding=UTF-8&useSSL=false

username: root

password: root



mybatis:

Load the mapper file

mapper-locations: classpath:mapper/*.xml

Set the entity category name

#type-aliases-package: demo.springboot.mybatismapperpagehelper.domain

Load mybatis global configuration file

#config-location: classpath:mybatis/SqlMapConfig.xml



mapper:

# Generic mapper class, can be multiple, do not put together with the business DAO

mappers: demo.springboot.mybatismapperpagehelper.basedao.BaseMapper

Copy the code

3.BaseMapper

// Inherit the interface from tkmapper

// add, delete, change, query, etc

//MySqlMapper is a set of operations on mysql

public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> {

}

Copy the code

4. The entity class

@Data

public class SysUser {

// Query by primary key must have @id annotation, otherwise an error will be reported

@Id

private Long id;



private String username;



private String password;



private String nickname;



private Integer age;

}

Copy the code

5.DAO

// Inheriting BaseMapper from step 3, generics are entity classes

public interface SysUserDAO extends BaseMapper<SysUser> {

}

Copy the code

6. Scan the dao

@SpringBootApplication

// The scan annotations used here are not mybatis annotations, but general mapper annotations

@MapperScan("demo.springboot.mybatismapperpagehelper.dao")

public class MybatisMapperPagehelperApplication {



public static void main(String[] args) {

SpringApplication.run(MybatisMapperPagehelperApplication.class, args);

}



}

Copy the code

7. Test

@RunWith(SpringRunner.class)

@SpringBootTest

public class MybatisMapperPagehelperApplicationTests {



@Autowired

private SysUserDAO userDAO;



@Test

public void testSelectByPrimaryKey(a) {

// Query by primary key

SysUser sysUser = userDAO.selectByPrimaryKey(1L);

System.out.println(sysUser);

}



@Test

public void testSelectAll(a) {

// Query all data

List<SysUser> sysUsers = userDAO.selectAll();

System.out.println(sysUsers);

}



@Test

public void testPageHelper(a){

// The first argument is the number of pages, and the second argument is the number of pages. For the first SQL after this method,

// Single table paging, one-to-one paging is correct. One-to-many, many-to-many paging causes an error

//PageHelper.startPage(1, 10);

Sort by id in reverse order

PageHelper.startPage(1.2."id desc");

List<SysUser> sysUsers = userDAO.selectAll();



// Get paging data

PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);

// Get the total number of entries

System.out.println(pageInfo.getTotal());

// Get the number of paging pages

System.out.println(pageInfo.getPageNum());

// Get the number of paging bars

System.out.println(pageInfo.getPageSize());

// Get the paging data, which is the sysUsers queried above

System.out.println(pageInfo.getList());

}

}

Copy the code
  • Test according to the primary key query console logs: = = > Preparing: SELECT id, username, password and nickname, age FROM sys_user WHERE id =? ==> Parameters: 1(Long) <== Total: 1

  • Test all console log data query: = = > Preparing: SELECT id, username, password and nickname, age FROM sys_user = = > Parameters: < = = Total: 3

  • Preparing: SELECT ID, username, password, nickname, age FROM sys_user order by ID desc LIMIT? ==> Parameters: 2(Integer) <== Total: 2

Note: General mapper can only do some simple operations, business complex also need to be manually implemented. The paging plug-in does not need to format the number of pages ((pagenum-1)*pageSize) that you need to format when writing your own SQL

Project path


The author blog

Author’s official account