mybatis-plus联表(多表)查询,并分页显示,实体类中的实体类为空的解决方案(看mapper.xml文件)
前提准备
使用mybatis-plus生成代码,或者自己编写mapper,service,controller层。
使用Mybatis-plus分页配置文件
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
实体类
public class TBlog implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.ID_WORKER)
private Long id;
private Boolean appreciation;
private Boolean commentabled;
private String content;
private Date createTime;
private String firstPicture;
private String flag;
private Boolean published;
private Boolean recommend;
private Boolean shareStatement;
private String title;
private String updateTime;
private Integer views;
private Long typeId;
private Long userId;
// 另一个实体类,
private TType type;
}
编写TBlogMapper接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sun.blog.entity.TBlog;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TBlogMapper extends BaseMapper<TBlog> {
//这里需要一个IPage参数,必须放在第一位
IPage<TBlog> selectPages(IPage<TBlog> page, String title, String type, String isbBoolean);
}
这里可以不用继承BaseMapper(这是mybatis-plus自动生成的代码)
编写TBlogMapper.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="com.sun.blog.mapper.TBlogMapper">
<!--结果集映射,这里只映射了我需要的值,没有映射的值是为空的 -->
<resultMap id="UserAndType" type="com.sun.blog.entity.TBlog">
<id property="id" column="id"/>
<result property="title" column="title"/>
<result property="recommend" column="recommend"/>
<result property="updateTime" column="update_time" />
<!--blog类下的实体类映射,property是实体类的字段,column是数据库的字段--->
<collection property="type" ofType="com.sun.blog.entity.TType">
<id property="id" column="id"/>
<result property="name" column="name"/>
</collection>
</resultMap>
<!--resultMap="UserAndType"使用这个结果集接受数据---->
<select id="selectPages" resultMap="UserAndType" parameterType="string">
select*from t_blog tb ,t_type type where type.id=tb.type_id
<if test="title!=null">
and tb.title=#{title}
</if>
<if test="type!=null">
and type.name=#{type}
</if>
<if test="isbBoolean!=null">
and tb.recommend=#{isbBoolean}
</if>
order by type.id
</select>
</mapper>
如果用resultType,会导致blog实体类的type(实体类)字段为null,一对多用association,一对一用collection
记得配置文件,配置mapper.xml的路径
#mybatis-plus
mybatis-plus:
#mapper.xml文件路径,classpath==resource
mapper-locations: classpath:mapping/*.xml
#实体类路径
type-aliases-package: com.sun.blog.entity
我这里使用的是application.yml文件格式
测试
测试1
@Resource
private TBlogMapper blogMapper;
@Test
void text1(){
//new Page<>(2,1),第一个参数为第几页,第二个参数为一页显示几行数据
IPage<TBlog> page = blogMapper.selectPages(new Page<>(2,1),null , null, null);
for (TBlog blog : page.getRecords()) {
System.out.println(blog.toString());
//打印实体类中实体类
System.out.println(blog.getType().toString());
}
}
测试2
@Resource
private TBlogMapper blogMapper;
@Test
void text1(){
//改变第几页为1
IPage<TBlog> page = blogMapper.selectPages(new Page<>(1,1),null , null, null);
for (TBlog blog : page.getRecords()) {
System.out.println(blog.toString());
//打印实体类中实体类
System.out.println(blog.getType().toString());
}
}
测试结果
测试结果1
测试结果2
思考
为什么实际执行的sql与我们自己编写的sql不一样了?
答:我们编写的MyBatisPlusConfig配置文件像拦截器,或者是拦截器。每次向数据库发送请求,都会被它拦截,根据我们的第一个参数(new Page<>(2,1))的值,修改我们编写的sql语句,然后再发送到数据库执行,这就是实际执行的sql与我们编写的不一样。
结尾
由于本人水平有限,文中可能存在一些错误,还望各位看客指正。