Spring boot + Mybatis plus分页处理

本文介绍了在SpringBoot项目中使用Maven构建,包含依赖管理(如SpringBootStarterWeb,MyBatisPlus等),以及如何配置数据库连接(Druid)和MyBatisPlus的CRUD操作,重点展示了Controller、Service和Mapper的实现方法。
摘要由CSDN通过智能技术生成

项目Pom.xml

<?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
    </parent>

    <groupId>com.cd.ssm</groupId>
    <artifactId>sm</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.18</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

yaml

server:
  port: 8080
  servlet:
    context-path: /sm


spring:
  datasource:
    url: jdbc:mysql://localhost:3306/library
    username: root
    password: xxxxxxx
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource


mybatis-plus:
  mapperLocations: classpath:/mapper/*.xml
  typeAliasesPackage: com.cd.ssm.pojo
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
  global-config:
    db-config:
      db-type: mysql

Pojo

关注点,主要是用vo接收来自前端的数据,pagesize,pagenum

Controller

    @PostMapping("list")
    public ResultEntity getBookPages(@RequestBody BookParamVo bookParamVo){
        ResultEntity result = bookService.getBookPages(bookParamVo);
        return result;
    }

ServiceImpl

public ResultEntity getBookPages(BookParamVo bookParamVo) {
        IPage<Map> page = new Page<>();
        //每页多少数据
        page.setSize(bookParamVo.getPageSize());
        //当前是第几页
        page.setCurrent(bookParamVo.getPageNum());

        bookMapper.selectMyPage(page,bookParamVo);
        List<Map> records = page.getRecords();
        Map data = new HashMap();
        data.put("pageRecords",records);
        data.put("pageNum",page.getCurrent());
        data.put("pageSize",page.getSize());
        data.put("totalPage",page.getPages());
        data.put("totalSize",page.getTotal());

        Map pageInfo = new HashMap<>();
        pageInfo.put("pageInfo",data);

        return ResultEntity.ok(pageInfo);
    }

Mapper接口

   IPage<Map> selectMyPage(@Param("page") IPage page, @Param("bookParamVo") BookParamVo bookParamVo);

Mapper.xml

其实也可以用VO来接数据,中途不用就用HashMap了

<resultMap id="BookMap" type="java.util.HashMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="concernId" column="concern_id" jdbcType="INTEGER"/>
        <result property="price" column="price" jdbcType="FLOAT"/>
        <result property="entryId" column="entry_id" jdbcType="INTEGER"/>
        <result property="deleteId" column="delete_id" jdbcType="INTEGER"/>
        <result property="updateDatetime" column="update_datetime" jdbcType="DATE"/>
        <result property="concernName" column="concern_name" jdbcType="VARCHAR"/>
    </resultMap>
 
   <select id="selectMyPage" resultMap="BookMap">
        select a.*,b.name as concern_name from book a inner join book_concern b on a.concern_id = b.id
        <if test="bookParamVo.bookName != null and bookParamVo.bookName.length()>0">
            and a.name like '%${bookParamVo.bookName}%'
        </if>

    </select>

添加拦截器

没有这个就拿不到页面相关数据

@SpringBootApplication
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
@MapperScan("com.cd.ssm.mapper")
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 设置为使用 MYSQL 方言
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

ResultEntity 类

泛型不太会用就写不好这个类

package com.cd.ssm.pojo;

public class ResultEntity<T>{

    private  Integer code;
    private  String message;
    private  T data;
    public ResultEntity() {
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public static <T> ResultEntity<T> ok(T data){
        ResultEntity result = new ResultEntity();
        result.code = 200;
        result.message = "Success";
        result.data = data;
        return result;
    }

    public static<T> ResultEntity<T> ok(Integer code,String message,T data){
        ResultEntity result = new ResultEntity();
        result.code = code;
        result.message = message;
        result.data = data;
        return result;
    }

    public static<T> ResultEntity<T> fail(Integer code, String message){
        ResultEntity result = new ResultEntity();
        result.code = code;
        result.message = message;
        return result;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值