Take you step by step to implement basic Mybatis paging query

For the paging method using the pageHelper paging plug-in, please see this article:Using PageHelper to implement paging query (details)_Xiao Xi’s Blog-CSDN Blog

 1. Introduce dependencies

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-boot.version>2.6.13</spring-boot.version>
        <druid.version>1.2.16</druid.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 相当于复制spring-boot-dependencies中的dependency到这里进行管理 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>
    </dependencies>

2. Configure yml

server:
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

3. Write mapper

Entity class:

@Data
@TableName("tz_user")
public class User {
    private Long id;
    private String name;
    private int age;
}

Paging query, the front end needs to obtain two results. One is pageTotal, which is the total number of results, and the other is rows, which is the paging list queried.. Therefore, two sql statement queries are required here.

 mapper class:

Querying paging data requirespage numberandcurrent page query number, so the query parameters are as follows:

The method heregetIndex() is because mybatis obtains parameters through the get method of the entity class Get, and the meaning of index is the actual number of digits of the current page number (that is, I want to check page 2, and check 2 on each page, then the index is 2). Used in the sql statement limit Within limits.

 maper.xml:

    <!--分页查询-->
    <select id="pageList" resultType="com.xin.entity.pojo.User">
        select id,name,age from tz_user
            <where>
                <if test="name != null and name !='' ">
                    and name like concat('%',#{name},'%')
                </if>
                <if test="age != '' and age != null">
                    and age = #{age}
                </if>
            </where>
        limit #{index} , #{pageSize}
    </select>
    <!-- 统计-->
    <select id="countTotal" resultType="java.lang.Integer">
        select count(1) from tz_user
        <where>
            <if test="name != null and name !='' ">
                and name like concat('%',#{name},'%')
            </if>
            <if test="age != '' and age != null">
                and age = #{age}
            </if>
        </where>
    </select>

After writing, you can test it.

Guess you like

Origin blog.csdn.net/weixin_58403235/article/details/132841577