项目场景:
学习springboot整合mybatis-plus,使用分页插件
问题描述:
可以取到当前页码current=1,总页数 3,每页数据个数size=1,数据总量 3,就是取不到当前页的数据
@RequestMapping("/list1")
public Object list1(String name,Long current,Long size){
//自定义了一个链表查询并使用了分页插件
Page<Student> page = studentService.getAll(new Page<Student>(current,size),name);
Map<String,Object> result = new HashMap<>();
result.put("当前页码",page.getCurrent());
result.put("当前页的数据",page.getRecords());
result.put("总页数",page.getPages());
result.put("数据总量",page.getTotal());
result.put("每页数据个数",page.getSize());
return result;
}
原因分析:
一开始我忘记了写拦截器//Spring boot方式
@Configuration
public class MybatisPlusConfig {
// 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
但是之后发现写了也不管用
之后发现resultType返回类型错了
不应该是page对象类型,应该是student对象类型
<?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="com.zsq.mybatis2.dao.StudentMapper">
<select id="getAll" resultType="com.baomidou.mybatisplus.extension.plugins.pagination.Page">
SELECT stu.name,class.clsName from stu,class
<where>
stu.classId=class.id
and stu.isDel=0
<if test="name != null and name != ''">
and name like concat('%',#{name},'%')
</if>
</where>
</select>
</mapper>
解决方案:
<?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="com.zsq.mybatis2.dao.StudentMapper">
<select id="getAll" resultType="com.zsq.mybatis2.entity.Student">
SELECT stu.name,class.clsName from stu,class
<where>
stu.classId=class.id
and stu.isDel=0
<if test="name != null and name != ''">
and name like concat('%',#{name},'%')
</if>
</where>
</select>
</mapper>
总结
这是个很简单错误,虽然一部分是因为插件自动生成的原因,但是更多是自己不细心,学的不够扎实