<resultMap id="BaseResultTreeMap" type="com.shic.edu.model.vo.platform.EduPlatformLearnTypeLearnVo"> <id column="id" property="id"/> <result column="type_name" property="typeName"/> <result column="parent_id" property="parentId"/> <result column="sort" property="sort"/> <result column="help_center_default" property="helpCenterDefault"/> <collection property="platformHelpLearnVos" ofType="com.shic.edu.model.vo.platform.EduPlatformHelpLearnVo" column="type_id"> <id column="learnId" property="learnId"/> <result column="learn_name" property="learnName"/> <result column="type_id" property="typeId"/> <result column="publish_time" property="publishTime"/> <result column="type" property="type"/> <result column="learn_content" property="learnContent"/> </collection> </resultMap> 结果为空
这段代码是一个 MyBatis 的 resultMap,用于将查询结果映射成一个 EduPlatformLearnTypeLearnVo 对象,其中包含一个平台学习类型的信息以及该类型下的学习帮助信息。根据您提供的信息,可能是因为查询结果为空导致没有生成对象。您可以检查一下查询语句或者数据库中是否存在对应的数据。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xie.mapper.StudentInfoMapper"> <resultMap id="studentinfo" type="com.xie.pojo.StudentInfo"> <id column="student_id" property="studentid"/> <result column="student_name" property="studentname"/> <result column="student_tel" property="studenttel"/> <result column="student_pwd" property="studentpwd"/> <result column="student_card" property="studentcard"/> <result column="student_email" property="studentemail"/> <result column="student_ctime" property="studentctime"/> </resultMap> <resultMap id="entryVo" type="com.xie.vo.EntryVo"> <id column="audit.audit_id" property="auditid"/> <result column="exam.exam_name" property="examname"/> <result column="exam.exam_subject" property="examsubject"/> <result column="audit.audit_state" property="auditstate"/> </resultMap> <resultMap id="permitVo" type="com.xie.vo.PermitVo"> <id column="permit.permit_id" property="permitid"/> <result column="permit.permit_number" property="permitnumber"/> <result column="exam.exam_name" property="examname"/> <result column="exam.exam_subject" property="examsubject"/> <result column="exam.exam_stime" property="examstime"/> <result column="exam.exam_etime" property="exametime"/> </resultMap>
这是一个 MyBatis 的 XML 映射文件,用于将 SQL 语句与 Java 接口方法进行映射。该文件定义了多个 resultMap,用于将查询结果映射为 Java 对象。其中 studentinfo
resultMap 将查询结果映射为 com.xie.pojo.StudentInfo
类型的对象,包括学生 ID、姓名、手机号、密码、身份证号、邮箱、创建时间等属性。entryVo
resultMap 将查询结果映射为 com.xie.vo.EntryVo
类型的对象,包括审核 ID、考试名称、考试科目、审核状态等属性。permitVo
resultMap 将查询结果映射为 com.xie.vo.PermitVo
类型的对象,包括许可证 ID、许可证号码、考试名称、考试科目、考试开始时间、考试结束时间等属性。这些 resultMap 的定义将在接下来的 SQL 语句中使用。
<resultMap id="treeListResult" type="TreeListVo"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="parent_id" property="parentId"/> <result column="sort" property="sort"/> <collection property="children" ofType="TreeListVo"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="parent_id" property="parentId"/> <result column="sort" property="sort"/> <collection property="children" ofType="TreeListVo"> ... </collection> </collection> </resultMap>sql怎么写
假设你的树形结构表名为 tree
,包含字段 id
、name
、parent_id
、sort
,其中 id
是主键,parent_id
是父节点的 ID,根节点的 parent_id
为 null。根据上面的 resultMap
配置,一对多查询的 SQL 可以这样写:
SELECT *
FROM tree t1
LEFT JOIN tree t2 ON t1.id = t2.parent_id
WHERE t1.parent_id IS NULL
ORDER BY t1.sort, t2.sort
这个 SQL 查询出所有根节点及其子节点,按照根节点的 sort
字段升序排列,子节点按照其 sort
字段升序排列。其中,t1
表示根节点,t2
表示子节点。LEFT JOIN
保证即使某个根节点没有子节点,仍然能查询出来。
注意,如果树很大,这个查询可能会很慢,需要根据实际情况进行优化,例如增加索引等。
相关推荐
















