.xml文件中如何进行子查询

本文详细介绍在MyBatis中如何使用子查询进行数据检索,通过具体的.xml文件配置示例,展示如何设置collection属性实现对关联实体的嵌套查询。文章以TastCommonEntity为主查询对象,演示了如何通过taskcode字段进行子查询,获取MissionStaffEntity和MissionCaseEntity的相关信息。

.xml文件中如何进行子查询

参考文献:MyBatis 中使用 Collection 嵌套查询
子查询写法_其一
对应TastCommonEntity

<resultMap id="BaseResultMap" type="com.aima.service.taskmanager.entity.TastCommonEntity">
        <id column="stationId" jdbcType="VARCHAR" property="stationId"/>
        <id column="lineid" jdbcType="VARCHAR" property="lineid"/>
        <result column="taskcode" jdbcType="VARCHAR" property="taskcode"/>
        <result column="createtime" jdbcType="TIMESTAMP" property="createtime"/>
		...
		
        <collection column="taskcode" property="casePro" ofType="com.aima.service.taskmanager.entity.MissionCaseEntity"
                    select="selectConstable" javaType="java.util.ArrayList">

        </collection>
        <collection column="taskcode" property="staffPro"  ofType="com.aima.service.taskmanager.entity.MissionStaffEntity"
                    select="selectStaff" javaType="java.util.ArrayList">
                     
        </collection>
</resultMap>

主要是 select=" " 这块必须跟下面查询的id对应,要不然找不到

对应MissionStaffEntity

<resultMap id="BaseResultMapStaff" type="com.aima.service.taskmanager.entity.MissionStaffEntity">
        <id column="staffid" jdbcType="VARCHAR" property="id"/>
        <result column="staffid" jdbcType="VARCHAR" property="id"/>
        ...
</resultMap>

对应MissionCaseEntity

<resultMap id="BaseResultMapCase" type="com.aima.service.taskmanager.entity.MissionCaseEntity">
        <result column="caseid" jdbcType="VARCHAR" property="id"/>
        <result column="casecode" jdbcType="VARCHAR" property="code"/>
        <result column="casename" jdbcType="VARCHAR" property="name"/>
        <result column="casesex" jdbcType="VARCHAR" property="sex"/>
        ....
</resultMap>

第一个查询

<select id="selectStationPreview" parameterType="com.aima.service.taskmanager.entity.TastCommonEntity" resultType="com.aima.service.taskmanager.entity.TastCommonEntity"
            resultMap="BaseResultMap">
        select
            *
        FROM 
        	mission_sta t
        where t.taskcode = #{taskcode,jdbcType=VARCHAR}
    </select>

子查询一

    <select id="selectStaff" resultMap="BaseResultMapStaff" resultType="com.aima.service.taskmanager.entity.MissionStaffEntity"
            parameterType="com.aima.service.taskmanager.entity.MissionStaffEntity">
        select 
            *
        from 
        	missionStaff ms
      	where ms.taskcode = #{taskcode,jdbcType=VARCHAR}
    </select>

子查询二

    <select id="selectCase" resultMap="BaseResultMapCase" resultType="com.aima.service.taskmanager.entity.MissionCaseEntity"
            parameterType="com.aima.service.taskmanager.entity.MissionCaseEntity">
        select 
        	*  
        from 
        	missionCase mc
    	where mc.taskcode = #{taskcode,jdbcType=VARCHAR}
    </select>

查询过程是主要查询,子查询一,子查询二

  1. taskcode是前面传过来的,用于主要查询的where条件使用
  2. 子查询一中 column=“taskcode” 将值带过来 用于 子查询的where条件使用
  3. 子查询二中 column=“taskcode” 将值带过来 用于 子查询的where条件使用

自己理解的,本人使用正常,如有错误欢迎指正,如有问题欢迎留言!! by java小白

### 创建Mapper XML文件 在 IntelliJ IDEA 中为 MyBatis 项目创建 `mapper.xml` 文件的过程涉及多个方面。首先,确保项目的结构已经设置好,并且包含了必要的配置文件。 #### 设置项目结构 为了方便管理和维护代码,通常会在 Maven 或 Gradle 构建工具的支持下组织项目结构。对于基于 Maven 的项目来说,推荐采用多模块的方式构建应用程序[^4]。这意味着可以有一个父级 POM 来管理所有子模块之间的依赖关系,从而简化了版本控制和依赖项的统一管理。 #### 配置MyBatis环境 在开始编写 Mapper 接口及其对应的 XML 映射之前,需先完成基本的框架搭建工作。这包括但不限于: - 在 `src/main/resources/mybatis-config.xml` 中声明全局性的配置选项; - 使用 `<mappers>` 节点指定要加载的一系列映射器资源位置,可以通过包名一次性导入整个目录下的所有接口实现类[^1]; #### 新增Mapper XML文件的具体操作步骤 当上述准备工作完成后,则可着手准备具体的 SQL 映射语句文档——即所谓的 `mapper.xml` 文件。具体做法如下所示: 1. 右键点击希望存放这些文件的目标文件夹(通常是位于 resources 下面的一个专门用于保存 mapper xmls 的文件夹),选择 New -> Directory 并命名为 "mapper" 或者其他合适的名称。 2. 再次右击新建好的文件夹,选取 New -> File ,输入文件名为 `{YourMapperInterfaceName}Mapper.xml` (例如 UserMapper.xml)。这里的名字应该与相应的 DAO/Repository 层中的 Java Interface 名字相匹配以便于自动发现机制能够正常运作。 3. 打开刚建立起来的新文件并编辑其内容,按照 MyBatis 官方规范填写 namespace 和 sql statement tags 。下面是一个简单的例子: ```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.example.mapper.UserMapper"> <!-- 查询单个用户 --> <select id="getUserById" parameterType="int" resultType="com.example.model.User"> SELECT * FROM users WHERE id=#{id} </select> <!-- 更多功能... --> </mapper> ``` 通过以上方式可以在 IntelliJ IDEA 中顺利地添加新的 Mapper XML 文件MyBatis 工程当中去[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yizhi-w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值