天天看点

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

插件介绍

通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及 Example 相关的单表操作。通用 Mapper 是为了解决 MyBatis 使用中 90% 的基本操作,PageHelper则提供通用的分页查询功能,使用它们可以很方便的进行开发,可以节省开发人员大量的时间。

项目结构

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

导入依赖

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

1

3

4 tk.mybatis

5 mapper-spring-boot-starter

6 2.1.5

7

8

10

11 com.github.pagehelper

12 pagehelper-spring-boot-starter

13 1.2.10

14

15

16

17 mysql

18 mysql-connector-java

19

20

21

22 org.springframework.boot

23 spring-boot-starter-web

24

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

属性配置

在 application.properties 文件中分别添加上数据库、Mybatis、通用Mapper、PageHelper的属性配置

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false

spring.datasource.username=root

spring.datasource.password=123456

# 如果想看到mybatis日志需要做如下配置

logging.level.com.carry=DEBUG

######### Mybatis 自身配置 ##########

mybatis.mapper-locations=classpath:com/carry/mapper*.xml

10

11 true

12

13

14

15

16 org.springframework.boot

17 spring-boot-maven-plugin

18

19

20

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

具体编码

1、表结构

创建一张 user表

2、实体类

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

1 package com.carry.entity;

2

3 import java.io.Serializable;

4

5 import javax.persistence.GeneratedValue;

6 import javax.persistence.GenerationType;

7 import javax.persistence.Id;

8 import javax.persistence.Table;

9

10 @Table(name = "user")

11 public class User implements Serializable {

12

13 private static final long serialVersionUID = -8057591359892731452L;

14

15 @Id

16 @GeneratedValue(strategy = GenerationType.IDENTITY)

17 private Long id;

18 private String username;

19 private String password;

20 public Long getId() {

21 return id;

22 }

23 public void setId(Long id) {

24 this.id = id;

25 }

26 public String getUsername() {

27 return username;

28 }

29 public void setUsername(String username) {

30 this.username = username;

31 }

32 public String getPassword() {

33 return password;

34 }

35 public void setPassword(String password) {

36 this.password = password;

37 }

38

39

40 public User() {}

41 public User(String username, String password) {

42 super();

43 this.username = username;

44 this.password = password;

45 }

46 }

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

3、持久层

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

package com.carry.mapper;

import com.carry.entity.User;

import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper {

int countByUsername(String username);

}

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

4、映射文件UserMapper.xml

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

1 <?xml version="1.0" encoding="UTF-8" ?>

2

3

4

5

6 SELECT count(1) FROM user WHERE username = #{username}

7

8

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

5、启动类

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication

@MapperScan(basePackages = "com.carry.mapper")

public class SpringbootMapperPagehelperApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootMapperPagehelperApplication.class, args);

}

}

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

测试

完成数据访问层接口后,编写一个junit测试类来检验代码的正确性。

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)

1 package com.carry;

2

3 import org.junit.Test;

4 import org.junit.runner.RunWith;

5 import org.slf4j.Logger;

6 import org.slf4j.LoggerFactory;

7 import org.springframework.beans.factory.annotation.Autowired;

8 import org.springframework.boot.test.context.SpringBootTest;

9 import org.springframework.test.context.junit4.SpringRunner;

10

11 import com.carry.entity.User;

12 import com.carry.mapper.UserMapper;

13 import com.github.pagehelper.PageHelper;

14 import com.github.pagehelper.PageInfo;

15

16 @RunWith(SpringRunner.class)

17 @SpringBootTest

18 public class SpringbootMapperPagehelperApplicationTests {

19

20 private final Logger logger = LoggerFactory.getLogger(this.getClass());

21

22 @Autowired

23 private UserMapper userMapper;

24

25 @Test

26 public void test() {

27 try {

28 final User user1 = new User("u1", "p1");

29 final User user2 = new User("u1", "p2");

30 final User user3 = new User("u3", "p3");

31 userMapper.insertSelective(user1);

32 logger.info("[user1回写主键] - [{}]", user1.getId());

33 userMapper.insertSelective(user2);

34 logger.info("[user2回写主键] - [{}]", user2.getId());

35 userMapper.insertSelective(user3);

36 logger.info("[user3回写主键] - [{}]", user3.getId());

37 final int count = userMapper.countByUsername("u1");

38 logger.info("[调用自己写的SQL] - [{}]", count);

39

40 // TODO 模拟分页

41 for (int i = 0; i < 20; i++) {

42 userMapper.insertSelective(new User("u" + i, "p" + i));

43 }

44 // TODO 分页 + 排序 this.userMapper.selectAll() 这一句就是我们需要写的查询,有了这两款插件无缝切换各种数据库

45 final PageInfo pageInfo = PageHelper.startPage(1, 10).setOrderBy("id desc")

46 .doSelectPageInfo(() -> this.userMapper.selectAll());

47 logger.info("[lambda写法] - [分页信息] - [{}]", pageInfo.toString());

48

49 PageHelper.startPage(1, 10).setOrderBy("id desc");

50 final PageInfo userPageInfo = new PageInfo<>(this.userMapper.selectAll());

51 logger.info("[普通写法] - [{}]", userPageInfo);

52 } catch (Exception e) {

53 logger.error(e.getMessage());

54 }

55 }

56

57 }

mysql分页插件springboot_Springboot集成mybatis通用Mapper与分页插件PageHelper(推荐)