作者:admin,发布日期:2021-06-18
阅读:1241;评论:0
最近在网上看了很多mybatis的多表查询,都是基于resultMap来实现结果映射的,但是大多数文章都是完整编写了一遍resultMap,其实mybatis的resultMap是可以自动映射的,我们只需要写上ID所在的列的以及结果集中列名和实体类中属性名不一致的列就可以了,可以参考下方的文件。
在xml的resultMap中,我们开启了autoMapping选项,这样mybatis就可以帮我们自动映射字段,下面将解释实体类中注解的用处。
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="cn.craftyun.testproj.mapper.ExamMapper"> <resultMap id="examResult" type="cn.craftyun.testproj.entity.Exam" autoMapping="true"> <id property="id" column="id"/> <association property="paper" autoMapping="true"> <id property="id" column="paper_id"/> </association> </resultMap> <select id="selectExam" resultMap="examResult"> SELECT exam.*, paper.`name`, paper.caption FROM exam, paper WHERE exam.paper_id = paper.id ORDER BY exam.id </select> <select id="selectExamNoPaper" resultType="cn.craftyun.testproj.entity.Exam"> select * from exam order by exam.id </select> </mapper>
接口文件
public interface ExamMapper extends BaseMapper<Exam> { List<Exam> selectExam(); List<Exam> selectExamNoPaper(); }
实体类
package cn.craftyun.testproj.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; import lombok.EqualsAndHashCode; import java.io.Serializable; import java.time.LocalDateTime; /** * <p> * * </p> * * @author FanHuaCloud * @since 2021-06-18 */ @Data @EqualsAndHashCode(callSuper = false) public class Exam implements Serializable { /** * ID */ @TableId(value = "id", type = IdType.AUTO) private Integer id; /** * 试卷ID */ private Integer paperId; /** * 开始时间 */ private LocalDateTime startTime; /** * 考试时长 */ private Integer duration; /** * 所属试卷 */ @JsonInclude(JsonInclude.Include.NON_NULL) @TableField(exist = false) private Paper paper; }
package cn.craftyun.testproj.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import java.io.Serializable; import lombok.Data; import lombok.EqualsAndHashCode; /** * <p> * * </p> * * @author FanHuaCloud * @since 2021-06-18 */ @Data @EqualsAndHashCode(callSuper = false) public class Paper implements Serializable { /** * ID */ @TableId(value = "id", type = IdType.AUTO) private Integer id; /** * 名称 */ private String name; /** * 试卷说明 */ private String caption; }
控制器代码
略
关于注解
@JsonInclude(JsonInclude.Include.NON_NULL) @TableField(exist = false)
这里在属性上添加了两个注解
@JsonInclude - jackson的注解,用户指明序列化该属性的条件,如果注解的属性不为NULL时则序列化该字段
@TableField - mybatis-plus的注解,代表这个属性不是表里面的属性
测试结果
最终返回的JSON
[ { "id": 64, "paperId": 20, "startTime": "2020-01-01T00:00:00", "duration": 3600, "paper": { "id": 20, "name": "PHP多选", "caption": "PHP多选" } }, { "id": 65, "paperId": 21, "startTime": "2020-01-01T00:00:00", "duration": 3600, "paper": { "id": 21, "name": "PHP单选", "caption": "PHP单选" } }, { "id": 67, "paperId": 22, "startTime": "2020-01-01T00:00:00", "duration": 3600, "paper": { "id": 22, "name": "PHP判断", "caption": "PHP判断" } } ]
下面的结果是普通查询时的结果,由于没有进行联合查询,paper属性为null,所以最终不会序列化。
[ { "id": 64, "paperId": 20, "startTime": "2020-01-01T00:00:00", "duration": 3600 }, { "id": 65, "paperId": 21, "startTime": "2020-01-01T00:00:00", "duration": 3600 }, { "id": 67, "paperId": 22, "startTime": "2020-01-01T00:00:00", "duration": 3600 } ]