Mybatis常用标签

本文详细介绍了Mybatis中ResultMap的基本使用,如何映射自定义实体类,以及ResultType的用途,包括处理不同数据类型和一对一或多对多关联查询的技巧。特别关注了collectionOfType和javaType的区别,并展示了sql片段和inclusion标签的实际应用。
摘要由CSDN通过智能技术生成

Mybatis常用标签

在这里插入图片描述

ResultMap基本使用

适合使用返回值是自定义实体类的情况,映射实体类的数据类型

id:resultMap的唯一标识,column: 库表的字段名,property: 实体类里的属性名

    <resultMap type="RuralWarning" id="RuralWarningResult">
        <result property="id"    column="id"    />
        <result property="waterId"    column="water_id"    />
        <result property="userName"    column="userName"    />
        <result property="userNumber"    column="user_number"    />
        <result property="problemDescribtion"    column="problem_describtion"    />
        <result property="iphone"    column="iphone"    />
        <result property="address"    column="address"    />
        <result property="submitTime"    column="submit_time"    />
        <result property="handlerName"    column="handlerName"    />
        <result property="result"    column="result"    />
        <result property="handleTime"    column="handle_time"    />
    </resultMap>

ResultType使用

resultType可以直接返回给出的返回值类型,比如String、int、Map,等等,其中返回List也是将返回类型定义为Map,然后mybatis会自动将这些map放在一个List中,resultType还可以是一个对象,

  <select id="getLogCount" resultType="int">
    select COUNT(*) from AttLog where attTime = #{attTime} and userId = #{userId};
  </select>

mybatis collection ofType和javaType区别
ofType 用于多级联映射
javaType用于一对一映射
在这里插入图片描述

resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型

MyBatis中标签的使用

使用进行一对多的查询

在这里插入图片描述

关联-association

用于一对一和多对一

sql片段标签

作用是:
通过该标签可定义能复用的sql语句片段,在执行sql语句标签中直接引用即可。
这样既可以提高编码效率,还能有效简化代码,提高可读性。

<!--定义sql片段-->
    <sql id="selectRuralWarningVo">
        select id, water_id, user_number, problem_describtion, iphone, address, submit_time, result, handle_time from rural_warning
    </sql>

<include refid=“selectRuralWarningVo”/ 这个在MyBatis查询数据库的sql中经常会出现。作用相当于 * ,selectRuralWarningVo是固定的几个字段,而用*号的话会降低查询效率,因为后期数据库的字段会不断增加。

    <select id="selectRuralWarningList" parameterType="RuralWarning" resultMap="RuralWarningResult">
    <!--引用sql片段-->
        <include refid="selectRuralWarningVo"/>
        <where>  
            <if test="waterId != null  and waterId != ''"> and water_id = #{waterId}</if>
            <if test="userName != null  and userName != ''"> and user_name = #{userName}</if>
            <if test="userNumber != null  and userNumber != ''"> and user_number = #{userNumber}</if>
            <if test="problemDescribtion != null  and problemDescribtion != ''"> and problem_describtion = #{problemDescribtion}</if>
            <if test="iphone != null  and iphone != ''"> and iphone = #{iphone}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="submitTime != null "> and submit_time = #{submitTime}</if>
            <if test="handlerName != null  and handlerName != ''"> and handler_name = #{handlerName}</if>
            <if test="result != null  and result != ''"> and result = #{result}</if>
            <if test="handleTime != null "> and handle_time = #{handleTime}</if>
        </where>
    </select>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值