前置:
在association标签中 javaType属性指向的是实体类的属性。
在collection标签中 javaType属性指向的是集合的类型 ofType指向的是集合的泛型类型 。
mybatis-plus selectPage与一对多查询时存在问题。
1.1实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("sys_class")
public class ClassInfo {
@TableId(type = IdType.AUTO)
private Integer id;
private String grade;
private String className;
private String classNickname;
private String ratedNum;
@TableField(fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date createTime; //创建时间
@TableField(fill = FieldFill.UPDATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date updateTime; //更新时间
@TableField(exist = false) // 属性字段在表中不存在
private List<Student> studentList; //该班级的学生
@TableField(exist = false) // 属性字段在表中不存在
private List<Teacher> teacherList; //管理该班级的教师
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("sys_student")
public class Student {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private String username;
private String password;
private SexEnum sex;
private Date birth;
private Date schoolDate;
private Integer classId;
private String portrait; //头像
@TableField(fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime; //创建时间
@TableField(fill = FieldFill.UPDATE)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime; //更新时间
@TableField(exist = false)// 属性字段在表中不存在
private ClassInfo classInfo;//当前学生的班级
@TableField(exist = false) // 属性字段在表中不存在
private List<Family> familyList; //当前学生的家长
}
1.2ClassInfoMapper.java
public interface ClassInfoMapper extends BaseMapper<ClassInfo> {
// 一对多查询
Page<ClassInfo> findPage(Page<ClassInfo> page, @Param("className") String className);
//所有班级id
@Select("select id from sys_class")
List<Integer> getClassIds();
}
1.3原mapper.xml如下:
<resultMap id="classInfoMap" type="ClassInfo">
-- as别名与column一致,column与mysql表字段一致,property与java实体一致
<id property="id" column="id"/>
<result property="grade" column="grade"/>
<result property="className" column="class_name"/>
<result property="classNickname" column="class_nickname"/>
<result property="ratedNum" column="rated_num"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
<collection property="studentList" javaType="ArrayList" ofType="Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
<result property="sex" column="s_sex"/>
</collection>
</resultMap>
<select id="findPage" resultMap="classInfoMap">
select c.*, s.id as s_id,s.name s_name,s.sex s_sex
from sys_class c left join sys_student s
on c.id =s.class_id
where c.class_name like concat('%',#{className},'%')
</select>
分两次sql查询:
<resultMap id="classInfoMap" type="ClassInfo">
<id property="id" column="id"/>
<result property="grade" column="grade"/>
<result property="className" column="class_name"/>
<result property="classNickname" column="class_nickname"/>
<result property="ratedNum" column="rated_num"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
<collection property="studentList" ofType="Student"
select="queryStudentList" column="id">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
</collection>
</resultMap>
<!-- 主查询sql -->
<select id="findPage" resultMap="classInfoMap">
-- as别名与column一致,column与mysql表字段一致,property与java实体一致
select c.*
from sys_class c
where c.class_name like concat('%',#{className},'%')
</select>
<!-- 子查询sql -->
<select id="queryStudentList" resultType="Student">
select s.id,s.name,s.sex
from sys_student s
where s.class_id=#{id}
</select>
附:多对多查询
表结构:sys_student,sys_family,中间表sys_student_family
<!-- 学生-家长列表展示-->
<resultMap id="Student_familyMap" type="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="portrait" column="portrait"/>
<collection property="familyList" ofType="Family"
select="queryFamilyList" column="id">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="portrait" column="portrait"/>
</collection>
</resultMap>
<!-- 主查询sql -->
<select id="showStudentsAndFamily" resultMap="Student_familyMap">
select s.id,s.name,s.sex,s.portrait
from sys_student s
where s.name like concat('%',#{name},'%')
</select>
<!-- 子查询sql -->
<select id="queryFamilyList" resultType="Family">
select f.id,f.name,f.sex,f.portrait
from sys_family f join sys_student_family sf
where f.id = sf.family_id
and #{id} = sf.student_id
</select>