mybatis级联查询,子查询多条件,且参数包含父查询没有的字段

mybaits中的级联查询,一对一,一对多等,有两种情况

1.一个参数,一般来说,这个参数是子查询与父查询的共有字段

mapper.xml

 <resultMap id="ProjectResult" type="com.spms.entity.CompleteProject">
        <result column="projid" jdbcType="NUMERIC" javaType="int" property="id"/>
        <result column="deptid" property="deptId"/>
        <result column="id_" property="applyUserId"/>
        <result column="projapplytype" property="subjectId"/>
        <result column="projcode" property="code"/>
        <result column="projname" property="name"/>
        <result column="projcreatedate" property="createDate"/>
        <result column="projconstructdate" property="constructDate"/>
       

        <association property="owner" column="id_" javaType="User" select="com.spms.dao.UserDao.getUserById"/>
        <association property="preparer" column="projdraftpreparer" javaType="com.spms.entity.User" select="com.spms.dao.UserDao.getUserById"/>

        <association property="subject" column="projapplytype" select="com.spms.dao.SubjectDao.getSubjectById"/>
        <association property="constructDepartment" column="deptId"  select="com.spms.dao.DepartmentDao.getConstructDepartmentById"/>
        <association property="relevantDepartment" column="relevantdept"  select="com.spms.dao.DepartmentDao.getRelevantDepartmentById"/>
</resultMap>

父查询:

<select id="getById" parameterType="int" resultMap="ProjectResult">
        select
        <include refid="PROJECT_BASE"/>
        from project p
        <where>
            p.projid= #{projectId}
        </where>
    </select>

子查询:(举其中个例)

 <select id="getRelevantDepartmentById" parameterType="integer" resultMap="Department">
        select
        <include refid="Base_Column_List" />
        from department
        where deptid = #{deptId}
    </select>

property:指定子查询到父查询类中的对象属性;

column:子查询条件,为共有字段,自动映射

select:指定到子查询的位置以及id名

2.子查询多参数,非共有字段子查询私有

mapper.xml

<collection property="equipmentLists" column="{projId=projID,listStatus=listStatus}" select="com.spms.dao.EquipmentDao.getEquipmentListsByProjectId" />
        <collection property="schedules" column="{projId=projID,scheStatus=scheStatus}" select="com.spms.dao.ProjectScheduleDao.getProjSchedulesByProjId"/>

父查询中添加临时列,以便映射到子查询中:

case when ('${scheStatus}' != '') then '' else '${scheStatus}' end as scheStatus,添加临时列
 <select id="getByproId" resultMap="ProjectResult">
        select
        <include refid="PROJECT_BASE"/>,
        case when ('${scheStatus}' != '') then '' else '${scheStatus}' end as scheStatus,
        case when ('${scheStatus}' != '') then '' else '${scheStatus}' end as listStatus
        from project
        where 1=1
        <if test="projId != null">
            and projid= #{projId}
        </if>
    </select>

子查询:

<select id="getEquipmentListsByProjectId" parameterType="java.util.Map" resultMap="equipmentListResult">
        select
        <include refid="Base_Column_EquipmentList" />
        from PROJEQPTLIST
        WHERE projID = #{projectId,jdbcType=INTEGER} and listStatus=#{listStatus,jdbcType=VARCHAR}
        order by annualSchedule
    </select>

子查询DAO:

List<EquipmentList> getEquipmentListsByProjectId(@Param("projectId") int projectID,@Param("listStatus")String listStatus);

父查询DAO:

CompleteProject getByproId(@Param("projId") int projectId,@Param("scheStatus") String scheStatus);

column:{projId=projId,listStatus=listStatus}

parameterType:java.util.Map   很必要,,

  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Mybatis级联查询可以通过在Mapper文件中使用嵌套查询的方式实现,具体步骤如下: 1. 在POJO类中定义关联属性(如一对多、多对多等),并提供对应的setter和getter方法。 2. 在Mapper文件中定义对应的嵌套查询语句,使用关联属性的getter方法来获取关联对象的数据,例如: ```xml <select id="findUserById" parameterType="int" resultMap="userResultMap"> select * from user where id = #{id} </select> <resultMap id="userResultMap" type="User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="age" property="age"/> <association property="department" javaType="Department"> <id column="dept_id" property="id"/> <result column="dept_name" property="name"/> </association> </resultMap> <select id="findUserWithDeptById" parameterType="int" resultMap="userResultMap"> select u.*, d.dept_name from user u left join department d on u.dept_id = d.id where u.id = #{id} </select> ``` 在上述代码中,`findUserById`只查询User表中的数据,而`findUserWithDeptById`则查询User表和Department表中的数据,并将Department作为User对象的关联属性返回。 3. 在业务层中调用Mapper接口的方法,即可进行级联查询,例如: ```java User user = userMapper.findUserWithDeptById(1); System.out.println(user.getDepartment().getName()); ``` 在上述代码中,`userMapper.findUserWithDeptById`方法会返回一个User对象,其中的Department属性已经被赋值为关联的Department对象,通过getDepartment()方法即可获取Department对象的数据。 需要注意的是,在进行级联查询时,需要定义好关联属性的类型和对应的嵌套查询语句,否则会导致查询失败或数据不完整。同时,级联查询也会增加数据库的查询开销,应该根据实际情况进行使用。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值