Mybatis学习总结(二)

重点内容###动态SQL拼接与参数传递
parameterType:参数的数据类型,即定义输入到sql中的映射类型。
resultType:执行sql语句结果的数据类型,如果是pojo则应该给出全路径。
resultMap:结果映射,值为resultMap标签的id值。
#{id}:表示使用PreparedStatement设置占位符号并将输入变量id传到sql中。说白点,#{}作用就是占位符,相当于JDBC中的?。(可以这样理解:当碰到#{}时,用?代替,然后再通过{}里面的变量名找到对应的值填充,实质上就是原始的jdbc操作)
${id}:与字符串拼接无异,用来控制不需要预编译的地方
还有一点:Mybatis标签使用了OGNL表达式,它与#{}是不一样的机制,不要搞混
resultType和resultMap的区别:两种都是表示结果集与java对象的一直关系,好让Mybatis自动处理结果集,将结果集放到java对象中,当使用resultType时,比如resultType=“com.yhc.bean.Message”,我们就不用配置resultMap了,这时查询的结果集如何与pojo对象对应呢,列名对应属性名,不区分大小写,resultType也有其他应用方式,比如说可以放Map,resultType=“java.util.Map”,这样的话key为列名,value为查询的值,这样写大小写敏感
if标签

<select id="queryMessageList" parameterType="com.yhc.bean.Message" resultMap="MessageResult">
    select id, command, description, content from message where 1=1
    <if test="command != null and !&quot;&quot;.equals(command.trim())">
        and command = #{command}
    </if>
    <if test="description != null and !&quot;&quot;.equals(description.trim())">
        and description like '%' #{description} '%'
    </if>
</select>

foreach标签

<delete id="deleteBatch" parameterType="java.util.List">
    delete from message where id in (
        <foreach collection="list" item="item" separator=",">
            #{item}
        </foreach>
    )
</delete>

where标签
作用:自动实现SQL语句的where的功能

sql标签

<sql id="columns">id, command, description, content</sql>
//然后我们就可以引用它了
select <include refid="columns"/> from message

set标签

<update id="">
    update message 
    <set> 
    <if test="command != null and !&quot;&quot;.equals(command.trim())">
        command = #{command}
    </if>
    <if test="description != null and !&quot;&quot;.equals(description.trim())">
        description = #{description}
    </if>
    </set>
    where id = #{id}
</update>

trim标签
作用:组合功能

<trim prefix="where" suffix="" prefixOverrides="" suffixOverrides="">

</trim>

choose标签
作用:相当于if…else if …else if…else或者switch case

<choose>
    <when test="">
    </when>
    <when test="">
    </when>
    <when test="">
    </when>
    <otherwise></otherwise>
</choose>

这里写图片描述

这里写图片描述

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值