mybatis中对resultMap 做嵌套查询 多参数查询

<resultMap id="Organ" type="com.pskj.GSLZ.pojo.zfzb.Organ" >
        <result column="OR_ID" property="OR_ID" jdbcType="VARCHAR"></result>
        <result column="OR_NAME" property="OR_NAME" jdbcType="VARCHAR"></result>
        <result column="PARENT_ID" property="PARENT_ID" jdbcType="VARCHAR"></result>
        <association column="OR_ID" property="count" javaType="Integer" select="count"/>
        <collection column="OR_ID"  property="subOrgan" ofType="Organ" javaType="java.util.ArrayList" select="selectChildren"></collection>
    </resultMap>

    <!--查询一级组织架构-->
    <select id="checkList" parameterType="pd" resultMap="Organ" useCache="false">
        SELECT *
        FROM sys_organ t
        WHERE t.OR_ID = #{OR_ID} AND t.OR_STATE = ‘2‘
    </select>

    <!--查询下级组织架构-->
    <select id="selectChildren" parameterType="pd" resultMap="Organ"  useCache="false">
        SELECT *
        FROM sys_organ t WHERE t.PARENT_ID = #{OR_ID} AND t.OR_STATE = ‘2‘
    </select>

    <!--统计各支队故障数量-->
    <select id="count" parameterType="pd" resultType="Integer" useCache="false">
        SELECT IFNULL(sum(num),0) count from zfzb_subcheck zsk LEFT JOIN zfzb_equip_check zec on zec.EC_ID=zsk.EC_ID
        <where>
            zec.STATE=‘1‘
            <!--查询下级组织所有id,这是写的一个MySQL查询函数-->
            and ORGAN_ID in(SELECT OR_ID from sys_organ where OR_STATE=‘2‘ and FIND_IN_SET(OR_ID,findOrgan(#{OR_ID})))
        </where>
    </select>

上面是一个简单的示例,还有的时候我们需要传进来的参数,可以 先把这个参数隐射成结果集

<resultMap type="PersonalTrends" id="PersonalTrendsResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="buidingId" column="buiding_id"/>
        <result property="buidingName" column="buiding_name"/>
        <result property="trends" column="trends"/>
        <result property="likeNum" column="like_num"/>
        <result property="creatTime" column="creat_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="phone" column="phone"/>
        <association property="isLike" javaType="Integer" column="{tid = id,ph = phone}" select="getIsLikeBy"></association>
    </resultMap>

<!-- photo 是mapper 传进来的参数 把 这个字段 变成返回值-->

<sql id="selectPersonalTrendsVo">
        select id, user_id, user_name, buiding_id, buiding_name, trends,like_num, creat_time, update_time ,${phone} phone from personal_trends
    </sql>

<!-- 这是主要查询方法-->

<select id="selectPersonalTrendsByBid" parameterType="java.util.Map"
            resultMap="PersonalTrendsResult">
        <include refid="selectPersonalTrendsVo"/>
        where buiding_id = #{buidingId}
        and user_id = #{userId}
        order by creat_time desc
        limit 20
    </select>

<!-- 这是resultMap 中 isLike 的查询-->

<select id="getIsLikeBy" parameterType="java.util.Map" resultType="java.lang.Integer">
        select count(id) from likes_record where trends_id = #{tid} and customer_phone = #{ph} and likes = "1"
    </select>

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于Mybatis在insert嵌套查询的问题,可以使用Mybatis的动态SQL语句来实现,具体方法如下: 1. 在mapper文件定义一个包含子查询SQL语句,比如: ``` <select id="getUserIdByName" parameterType="java.lang.String" resultType="java.lang.Integer"> SELECT user_id FROM user WHERE user_name = #{name} </select> ``` 2. 在insert语句使用动态SQL语句来调用子查询,比如: ``` <insert id="insertOrder" parameterType="Order"> INSERT INTO order (order_no, user_id, order_time) VALUES (#{orderNo}, #{userId, jdbcType=INTEGER}, #{orderTime}) <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> </insert> ``` 其,#{userId, jdbcType=INTEGER}是插入语句的一个参数,它的值通过调用getUserIdByName子查询来获取。 关于Mybatis查询结果resultMap的使用概述,可以参考以下几点: 1. resultMapMybatis用于映射查询结果集的标签,它可以将查询结果集的列名映射为Java对象的属性名。 2. resultMap标签可以定义在mapper文件,也可以定义在公共的resultMap文件,以便在多个mapper文件复用。 3. resultMap标签支持多种映射方式,如一对一、一对多、多对一、多对多等,可以根据查询结果集的实际情况选择不同的映射方式。 4. resultMap标签还支持映射嵌套对象、映射对象属性、映射枚举值等高级功能,可以根据具体情况选择使用。 总之,MybatisresultMap标签是非常强大且灵活的,可以帮助我们快速、方便地实现查询结果集的映射。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值