目录
一、前置工作:
1.整体项目目录结构
2.创建普通javamaven项目。
3.导入依赖,改造成springboot项目
4.配置启动类
5.创建service接口及其实现类
6.创建接口Mapper
7.配置数据源
8.创建数据库表
二、使用MP(mybatisplus)的分页插件
二、使用自定义的分页插件
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能。
一、前置工作:
1.整体项目目录结构
2.创建普通javamaven项目。
3.导入依赖,改造成springboot项目
依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.qcby</groupId><artifactId>SpringBootMybatisPlus</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.0</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.46</version><scope>runtime</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
4.配置启动类
package com.qcby;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.qcby.SpringBoot.mapper")
public class SpringBootApplication1 {public static void main(String[] args) {SpringApplication.run(SpringBootApplication1.class, args);}
}
5.配置实体类
package com.qcby.SpringBoot.pojo;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName("t_product")
public class Product {private Long id;private String name;private Integer price;private Integer version;
}
package com.qcby.SpringBoot.pojo;import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data //set和get方法
@AllArgsConstructor //全参构造器
@NoArgsConstructor //无参构造器
@TableName("t_user")
public class User {//因为用到雪花算法,所以用Long属性@TableIdprivate Long id;private String name;private Integer age;private String email;@TableLogicprivate Integer isDeleted;
}
5.创建service接口及其实现类
package com.qcby.SpringBoot.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.qcby.SpringBoot.pojo.User;/*** UserService继承IService模板提供的基础功能*/
public interface UserService extends IService<User> {
}
package com.qcby.SpringBoot.service.Impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.SpringBoot.mapper.UserMapper;
import com.qcby.SpringBoot.pojo.User;
import com.qcby.SpringBoot.service.UserService;
import org.springframework.stereotype.Service;/*** ServiceImpl实现了IService,提供了IService中基础功能的实现* 若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,并在实现类中实现*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}
6.创建接口Mapper
package com.qcby.SpringBoot.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.SpringBoot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;@Mapper
public interface UserMapper extends BaseMapper<User> {}
package com.qcby.SpringBoot.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qcby.SpringBoot.pojo.Product;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface ProductMapper extends BaseMapper<Product> {
}
7.配置数据源
在application.yaml中配置信息。
spring:# 配置数据源信息datasource:# 配置数据源类型type: com.zaxxer.hikari.HikariDataSource# 配置连接数据库信息driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=falseusername: rootpassword: root
# 配置MyBatis日志
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
8.创建数据库表
二、使用MP(mybatisplus)的分页插件
首先要在容器中配置一个mybatisplus分页插件的bean。
可以自定义一个配置类,也可以在启动类中配置,因为启动类也是一个配置类。
package com.qcby.SpringBoot.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MybatisPlusConfig {/*** 分页插件* 构建一个拦截来处理分页* 每个数据库厂商对于分页的实现语法有差别,因此,在声明该拦截时,需要指定应用的数据库类型* @return*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));//由于各个数据库的语法会有差别,因此,要指明数据库类型return interceptor;}
}
编写测试类
package com.qcby;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.SpringBoot.mapper.ProductMapper;
import com.qcby.SpringBoot.mapper.UserMapper;
import com.qcby.SpringBoot.pojo.Product;
import com.qcby.SpringBoot.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
public class Test2 {@Autowiredprivate UserMapper userMapper;@Autowiredprivate ProductMapper productMapper;@Testpublic void testPage(){//设置分页参数Page<User> page = new Page<>(1, 5);userMapper.selectPage(page, null);//获取分页数据List<User> list = page.getRecords();list.forEach(System.out::println);System.out.println("当前页:"+page.getCurrent());System.out.println("每页显示的条数:"+page.getSize());System.out.println("总记录数:"+page.getTotal());System.out.println("总页数:"+page.getPages());System.out.println("是否有上一页:"+page.hasPrevious());System.out.println("是否有下一页:"+page.hasNext());}}
二、使用自定义的分页插件
在usermapper中加入方法
package com.qcby.SpringBoot.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.SpringBoot.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;@Mapper
public interface UserMapper extends BaseMapper<User> {/*** 根据年龄查询用户列表,分页显示* @param page 分页对象 ,xml中可以从里面进行取值 ,传递参数 Page 即自动分页 ,必须放在第一位* @param age 年龄* @return*//*** 不用加limit语句,因为配置了一个拦截的插件,只需要传入page对象,还是使用的MP的分页插件* @param page* @param age* @return*/@Select("SELECT id,name,age,email FROM t_user WHERE age > #{age}")IPage<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);
}
package com.qcby;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.SpringBoot.mapper.ProductMapper;
import com.qcby.SpringBoot.mapper.UserMapper;
import com.qcby.SpringBoot.pojo.Product;
import com.qcby.SpringBoot.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
public class Test2 {@Autowiredprivate UserMapper userMapper;@Autowiredprivate ProductMapper productMapper;@Testpublic void testSelectPageVo(){//设置分页参数Page<User> page = new Page<>(1, 5);userMapper.selectPageVo(page, 20);//获取分页数据List<User> list = page.getRecords();list.forEach(System.out::println);System.out.println("当前页:"+page.getCurrent());System.out.println("每页显示的条数:"+page.getSize());System.out.println("总记录数:"+page.getTotal());System.out.println("总页数:"+page.getPages());System.out.println("是否有上一页:"+page.hasPrevious());System.out.println("是否有下一页:"+page.hasNext());}
}