欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > Spring Boot (maven)分页4.0.1版本 专业版- 改

Spring Boot (maven)分页4.0.1版本 专业版- 改

2025/2/22 8:08:11 来源:https://blog.csdn.net/c_yanxin_ru/article/details/145708668  浏览:    关键词:Spring Boot (maven)分页4.0.1版本 专业版- 改
前言:

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

正片

核心 · 环境搭建:spring boot 3(3.3.7版本) + spring web + mybatis + Mysql Driver + Lombk

专业版:在动态分页查询(通用版)的基础上,进行迭代

通用版本模板

第一步:创建用于数据传输和接受的实体类

import lombok.Data;
import java.util.Date;@Data
public class RequestPageEntity {/*** id,username,roleId,lastLogin 数据库表* into,Max 分页查询映射参数*/private int id;private String username;private int roleId;private Date lastLogin;private Integer into;private int Max;
}

这个实体类包含两部分,或者说实体类的作用有两部分

第一部分:接收数据库返回内容

第二部分:传输SQL语句映射参数

第二步:新建java映射

import org.apache.ibatis.annotations.Mapper;
import org.example.mybatis.entity.RequestPageEntity;
import java.util.List;@Mapper
public interface RequestPageMapper {List<RequestPageEntity> requestPage(Integer into,int Max);//id改为实体类中的Max
}

@Mapper是为了告诉Springboot,这是一个映射,当然你也可以配置一下

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

"mapper层" :存放mapper文件的路径

第三步:新建xml映射

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

为了避免不必要的路径问题,请务必让java映射和xml映射路径完全一致

1+2等于第二步的配置参数

1+2+3等于xml中的namespace的参数

1+2 = resource下的1+2,可以避免不必要的报错

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

第四步:在Mapper.xml写增删改查业务

    <select id="java映射接口方法名" resultType="返回值类型"></select>

返回值类型,就像我们调用实体类一样,一个路径

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.Mapper.RequestPageMapper"><select id="requestPage" resultType="org.example.mybatis.entity.RequestPageEntity"></select>
</mapper>

注意,不要import加上去

如何判断十分成功了呢?

利用IDEA的ctrl + 左键 会自动跳转

select * from user limit #{对应java接口的参数名,不一样会报错},#{对应java接口的参数名,不一样会报错}

完整版

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.Mapper.RequestPageMapper"><select id="requestPage" resultType="org.example.mybatis.entity.RequestPageEntity">select * from user limit #{into},#{id}</select>
</mapper>

mybatis会自动读取接口实体类中的全部get/set方法,去除掉get做为传输数据,去掉set作为返回类型状态,也就是一开始说的实体类的两个作用

我们测试一下

import jakarta.annotation.Resource;
import org.example.mybatis.Mapper.RequestPageMapper;
import org.example.mybatis.entity.RequestPageEntity;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
class MybatisApplicationTests {@ResourceRequestPageMapper mapper;@Testvoid contextLoads() {List<RequestPageEntity> requestPageEntities = mapper.requestPage(1, 3);System.out.println(requestPageEntities);}}

测试成功

作者这里偷懒,就不继续做了,你们要做可以参考

org.mybatis.spring.MyBatisSystemException: 
### 查询数据库时出错。原因:java.lang.IndexOutOfBoundsException: 索引 4 超出了长度为 4 的界限
### 错误可能存在于 org/example/mybatis/Mapper/RequestPageMapper.xml
### 错误可能涉及 org.example.mybatis.Mapper.RequestPageMapper.requestPage
### 错误发生在处理结果时
### SQL: select * from user limit ?,?
### 原因:java.lang.IndexOutOfBoundsException: 索引 4 超出了长度为 4 的界限

报错原因,在实体类中添加了有参数构造函数

import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Date;@Data
// @AllArgsConstructor  报错原因
public class RequestPageEntity {/*** id,username,roleId,lastLogin 数据库表* into,Max 分页查询映射参数*/private int id;private String username;private int roleId;private Date lastLogin;private Integer into;private int Max;}

自动计算分页点算法:起点 = (页数 - 1)* 查询最大值

@Data
// @AllArgsConstructor  报错原因
public class RequestPageEntity {/*** id,username,roleId,lastLogin 数据库表* into,Max 分页查询映射参数*/private int id;private String username;private int roleId;private Date lastLogin;private Integer into;private int Max;public Integer getPageBegin(){return (this.into - 1) * Max;}
}

我们通过this自动获取参数,然后计算

修改我们的Mapper层

import org.apache.ibatis.annotations.Mapper;
import org.example.mybatis.entity.RequestPageEntity;
import java.util.List;@Mapper
public interface RequestPageMapper {List<RequestPageEntity> requestPage(RequestPageEntity entity);

我们根据mybatis修改xml映射

我们直接在控制类测试

@RestController
public class user {@ResourceRequestPageMapper mapper;@PostMapping("/list")public String getList(RequestPageEntity entity){List<RequestPageEntity> requestPageEntities = mapper.requestPage(entity);return "打印成功" + requestPageEntities;}
}

1

### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'PageBegin' in 'class org.example.mybatis.entity.RequestPageEntity'
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'PageBegin' in 'class org.example.mybatis.entity.RequestPageEntity'] with root causeorg.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'PageBegin' in 'class org.example.mybatis.entity.RequestPageEntity'

这个报错是因为你mybatis映射自动映射导致的

mybatis会自动将getxxx的方法转换成xxx,然后作为参数传入

注意了

第二张图片报错了

getPageBegin

PageBegin报错了

pageBegin没报错

说明mybatis自动映射会将get后面的第一个单词转换成小写

P会变成p

Big会编程big

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词