目录
一、前置工作:
1.整体项目目录结构
2.创建普通javamaven项目。
3.导入依赖,改造成springboot项目
4.配置启动类
5.创建service接口及其实现类
6.创建接口Mapper
7.配置数据源
8.创建数据库表
二、使用自定义的分页插件
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能。
依赖:
4.0.0 com.qcby SpringBootMybatisPlus1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent2.4.0 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testtest com.baomidou mybatis-plus-boot-starter3.5.1 org.projectlombok lomboktrue mysql mysql-connector-java5.1.46 runtime org.springframework.boot spring-boot-maven-plugin
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属性 @TableId private Long id; private String name; private Integer age; private String email; @TableLogic private Integer isDeleted; }
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{ }
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 ServiceImplimplements UserService { }
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{ }
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{ }
在application.yaml中配置信息。
spring: # 配置数据源信息 datasource: # 配置数据源类型 type: com.zaxxer.hikari.HikariDataSource # 配置连接数据库信息 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false username: root password: root # 配置MyBatis日志 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
首先要在容器中配置一个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 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(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 { @Autowired private UserMapper userMapper; @Autowired private ProductMapper productMapper; @Test public void testPage(){ //设置分页参数 Pagepage = new Page<>(1, 5); userMapper.selectPage(page, null); //获取分页数据 List 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{ /** * 根据年龄查询用户列表,分页显示 * @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 selectPageVo(@Param("page") Page 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 { @Autowired private UserMapper userMapper; @Autowired private ProductMapper productMapper; @Test public void testSelectPageVo(){ //设置分页参数 Pagepage = new Page<>(1, 5); userMapper.selectPageVo(page, 20); //获取分页数据 List 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()); } }