Spring Boot (maven)分页2.0版本

发布于:2025-02-16 ⋅ 阅读:(117) ⋅ 点赞:(0)
前言:

通过实践而发现真理,又通过实践而证实真理和发展真理。从感性认识而能动地发展到理性认识,又从理性认识而能动地指导革命实践,改造主观世界和客观世界。实践、认识、再实践、再认识,这种形式,循环往复以至无穷,而实践和认识之每一循环的内容,都比较地进到了高一级的程度。

正片:

XML版本 3.0

第一步:增加实体类(连接数据库与服务端的桥梁)

第二步:增加Mapper接口

第三步:增加XML文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="具体mapper层路径,精确到文件名">
​
</mapper>

tip:Mapper和XML保证路径相同,文件名相同

第四步:增加Service类

第一步:创建一个接口类 interface 接口名

第二步:创建一个实现类 实现类名 implements 接口名

第五步:增加Controller类(返回经处理过的数据)

第六步:增加Configuration类

@Configuration
@MapperScan(basePackages = "mapper层")
public class ApplicationConfig {
​
}

第七步:测试

全新模板迭代分页

这是我们用于测试的数据库表

 第一步:创建实体类

@Data
public class TestEntity {
    /**
     * int id 系统id
     * String username 我也不知道叫什么
     * String author 我也不知道该叫什么
     */
    
    int id;
    String username;
    String author;
}

第二步:创建mapper

重点在于返回类型和返回名,分页在1.0版本我们知道他返回不止一条,所以需要使用到集合类

package org.example.mybatis.Mapper;

import java.util.List;

public interface TestMapper {

    List<TestMapper> SelectLimit();
}
SELECT  字段名 FROM    表名    LIMIT    起点(n-1,n => 0),查询数量

根据1.0的SqL语句,我们可以知道他有两个参数,所以我们的接口也需要两个参数

package org.example.mybatis.Mapper;

import java.util.List;

public interface TestMapper {

    List<TestMapper> SelectLimit(int into,int Max);
}

第三步:新建xml文件

注意:

如果你没有配置mybaits.xml路径,如图

mapper-locations:classpath:

他会自动读取resource里的内容,但xml需要指定

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.Mapper.TestMapper">

    
</mapper>

注意 namespace需要对应上一步创建的TestMapper路径

    <select id="SelectLimit" resultType="org.example.mybatis.entity.TestEntity">
        
    </select>

查询模板,id需要对应上一步mapper层理的方法,resultType是返回的实体类路径

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.Mapper.UserMapper">

    <select id="UserByName" resultType="org.example.mybatis.entity.UserEntity">
        SELECT * from steel.user_test LIMIT #{into},#{Max}
    </select>
</mapper>

注意:接口的方法名就是id的参数,#{}里的内容请务必对其接口参数,顺序,大小写,一个不同,都会报错

到了这里,我们可以先去测试一下

package org.example.mybatis;

import jakarta.annotation.Resource;
import org.example.mybatis.Mapper.TestMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class MybatisApplicationTests {

    @Resource
    TestMapper testMapper;


    @Test
    void contextLoads() {
        List<TestMapper> testMappers = testMapper.SelectLimit(0, 6);
        System.out.println(testMappers);
    }

}
[TestEntity(id=1, username=ak47, author=未知), 
TestEntity(id=2, username=马克, author=未知), 
TestEntity(id=3, username=老子, author=未知), 
TestEntity(id=4, username=老铁6666, author=未知), 
TestEntity(id=5, username=含金量, author=未知), 
TestEntity(id=6, username=奶妈, author=未知)]

这是我们打印的结果

到了这里我们2.0分页也是完结了

文件结构

基础分页的核心在于

SELECT 字段名 FROM   表名   LIMIT    起点(n-1,n => 0),查询数量

这条sql语句

2.0比1.0就多了一个清晰的模板,那是不是太捞了

没错,就是这么捞


网站公告

今日签到

点亮在社区的每一天
去签到