You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
exam/mapper/qu/QuMapper.xml

159 lines
8.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?xml version="1.0" encoding="UTF-8"?>
<!-- 定义文档类型,引用 MyBatis Mapper 3.0 的 DTD用于验证 XML 文档结构是否符合 MyBatis 规范 -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 定义 Mapper 命名空间,关联到对应的 Java Mapper 接口,方便 MyBatis 找到对应的操作方法 -->
<mapper namespace="com.yf.exam.modules.qu.mapper.QuMapper">
<!-- 通用查询映射结果 -->
<!-- 定义一个结果映射,将数据库查询结果映射到 Java 实体类 com.yf.exam.modules.qu.entity.Qu -->
<resultMap id="BaseResultMap" type="com.yf.exam.modules.qu.entity.Qu">
<!-- 映射数据库表的主键列 id 到 Java 实体类的 id 属性 -->
<id column="id" property="id" />
<!-- 映射数据库表的 qu_type 列到 Java 实体类的 quType 属性 -->
<result column="qu_type" property="quType" />
<!-- 映射数据库表的 level 列到 Java 实体类的 level 属性 -->
<result column="level" property="level" />
<!-- 映射数据库表的 image 列到 Java 实体类的 image 属性 -->
<result column="image" property="image" />
<!-- 映射数据库表的 content 列到 Java 实体类的 content 属性 -->
<result column="content" property="content" />
<!-- 映射数据库表的 create_time 列到 Java 实体类的 createTime 属性 -->
<result column="create_time" property="createTime" />
<!-- 映射数据库表的 update_time 列到 Java 实体类的 updateTime 属性 -->
<result column="update_time" property="updateTime" />
<!-- 映射数据库表的 remark 列到 Java 实体类的 remark 属性 -->
<result column="remark" property="remark" />
<!-- 映射数据库表的 analysis 列到 Java 实体类的 analysis 属性 -->
<result column="analysis" property="analysis" />
</resultMap>
<!-- 通用查询结果列 -->
<!-- 定义一段可复用的 SQL 片段,包含常用的查询列,可在其他 SQL 语句中通过 <include> 标签引用 -->
<sql id="Base_Column_List">
`id`,`qu_type`,`level`,`image`,`content`,`create_time`,`update_time`,`remark`,`analysis`
</sql>
<!-- 随机取数据 -->
<!-- 定义一个查询方法,根据条件随机获取题目数据 -->
<select id="listByRandom" resultMap="BaseResultMap">
<!-- 从 el_qu 表中选取所有列,别名为 a -->
SELECT a.*
FROM el_qu a
<!-- 左连接 el_qu_repo 表,别名为 b通过 id 和 qu_id 关联 -->
LEFT JOIN el_qu_repo b ON a.id=b.qu_id
<!-- 筛选条件:指定题库 ID 和题目类型 -->
WHERE b.repo_id=#{repoId} AND a.qu_type=#{quType}
<!-- 判断 excludes 参数是否不为空 -->
<if test="excludes!=null">
<!-- 排除指定 ID 的题目 -->
AND a.id NOT IN
<!-- 遍历 excludes 集合,将集合元素用逗号分隔,用括号包裹 -->
<foreach item="item" collection="excludes" separator="," open="(" close=")" index="">'${item}'</foreach>
</if>
<!-- 按随机顺序排序 -->
ORDER BY RAND()
<!-- 限制返回结果数量 -->
LIMIT ${size}
</select>
<!-- 定义导出数据的结果映射,将数据库查询结果映射到 Java DTO 类 com.yf.exam.modules.qu.dto.export.QuExportDTO -->
<resultMap id="ExportResultMap" type="com.yf.exam.modules.qu.dto.export.QuExportDTO">
<!-- 映射数据库表的 q_id 列到 Java DTO 类的 qId 属性 -->
<id column="q_id" property="qId" />
<!-- 映射数据库表的 qu_type 列到 Java DTO 类的 quType 属性 -->
<result column="qu_type" property="quType" />
<!-- 映射数据库表的 q_content 列到 Java DTO 类的 qContent 属性 -->
<result column="q_content" property="qContent" />
<!-- 映射数据库表的 q_analysis 列到 Java DTO 类的 qAnalysis 属性 -->
<result column="q_analysis" property="qAnalysis" />
<!-- 映射数据库表的 a_is_right 列到 Java DTO 类的 aIsRight 属性 -->
<result column="a_is_right" property="aIsRight" />
<!-- 映射数据库表的 a_content 列到 Java DTO 类的 aContent 属性 -->
<result column="a_content" property="aContent" />
<!-- 映射数据库表的 a_analysis 列到 Java DTO 类的 aAnalysis 属性 -->
<result column="a_analysis" property="aAnalysis" />
<!-- 定义一个集合映射,通过 q_id 作为参数调用 selectRepos 方法,将结果映射到 repoList 属性 -->
<collection property="repoList" column="q_id" select="selectRepos"/>
</resultMap>
<!-- 定义一个查询方法,根据题目 ID 查询所属题库 ID 列表 -->
<select id="selectRepos" resultType="String">
<!-- 从 el_qu_repo 表中选取 repo_id 列 -->
SELECT repo_id FROM el_qu_repo po WHERE po.qu_id=#{qId}
</select>
<!-- 定义查询条件的 SQL 片段,可在其他 SQL 语句中通过 <include> 标签引用 -->
<sql id="query">
<!-- <where> 标签会自动处理 SQL 语句中的 AND 关键字,避免多余的连接词 -->
<where>
<!-- 判断 query 参数是否不为空 -->
<if test="query!=null">
<!-- 判断 query 中的 quType 属性是否不为空 -->
<if test="query.quType!=null">
<!-- 添加筛选条件,根据题目类型过滤 -->
AND q.qu_type = #{query.quType}
</if>
<!-- 判断 query 中的 repoIds 属性是否不为空且集合大小大于 0 -->
<if test="query.repoIds!=null and query.repoIds.size()>0">
<!-- 添加筛选条件,根据题库 ID 列表过滤 -->
AND po.repo_id IN
<!-- 遍历 query.repoIds 集合,将集合元素用逗号分隔,用括号包裹 -->
<foreach collection="query.repoIds" open="(" close=")" separator="," item="repoId">#{repoId}</foreach>
</if>
<!-- 判断 query 中的 content 属性是否不为空且不为空字符串 -->
<if test="query.content!=null and query.content!=''">
<!-- 添加模糊查询条件,根据题目内容过滤 -->
AND q.content LIKE CONCAT('%',#{query.content},'%')
</if>
<!-- 判断 query 中的 excludes 属性是否不为空且集合大小大于 0 -->
<if test="query.excludes!=null and query.excludes.size()>0">
<!-- 添加排除条件,排除指定 ID 的题目 -->
AND q.id NOT IN
<!-- 遍历 query.excludes 集合,将集合元素用逗号分隔,用括号包裹 -->
<foreach collection="query.excludes" open="(" close=")" separator="," item="quId">
#{quId}
</foreach>
</if>
</if>
</where>
</sql>
<!-- 定义分页查询方法,使用 BaseResultMap 进行结果映射 -->
<select id="paging" resultMap="BaseResultMap">
<!-- 从 el_qu 表中选取所有列,别名为 q -->
SELECT q.*
FROM el_qu q
<!-- 左连接 el_qu_repo 表,别名为 po通过 id 和 qu_id 关联 -->
LEFT JOIN el_qu_repo po ON q.id=po.qu_id
<!-- 引用 query SQL 片段,添加查询条件 -->
<include refid="query" />
<!-- 按题目 ID 分组,按更新时间倒序排序 -->
GROUP BY q.id ORDER BY q.update_time DESC
</select>
<!-- 定义导出数据的查询方法,使用 ExportResultMap 进行结果映射 -->
<select id="listForExport" resultMap="ExportResultMap">
<!-- 选取需要导出的列,并指定别名 -->
SELECT
q.id as q_id,
q.qu_type,
q.content AS q_content,
q.analysis as q_analysis,
a.content as a_content,
a.is_right as a_is_right,
a.analysis as a_analysis
FROM el_qu q
<!-- 左连接 el_qu_answer 表,别名为 a通过 id 和 qu_id 关联 -->
LEFT JOIN el_qu_answer a ON q.id=a.qu_id
<!-- 左连接 el_qu_repo 表,别名为 po通过 id 和 qu_id 关联 -->
LEFT JOIN el_qu_repo po ON q.id=po.qu_id
<!-- 引用 query SQL 片段,添加查询条件 -->
<include refid="query" />
<!-- 按答案 ID 分组,按题目 ID 排序 -->
GROUP BY a.id ORDER BY q.id
<!-- 限制返回结果数量为 10000 条 -->
LIMIT 10000
</select>
</mapper>