resultMap和动态SQL

结果集映射 resultMap

1.当数据库字段名和POJO属性名不一致时可以使用resultMap,起一个别名
例:

 <resultMap id="book" type="com.lanou3g.mybatis.bean.Book">
        <id column="bid" property="id" />
       <result column="author_gender" property="authorGender" />
    </resultMap>

2.多表关联查询一对一使用resultMap的association

<resultMap id="book" type="com.lanou3g.mybatis.bean.Book">
        <id column="bid" property="id" />
        <result column="bname" property="bname" />
        <result column="author" property="author" />
        <result column="author_gender" property="authorGender" />
        <result column="price" property="price" />
        <result column="description" property="description" />
        <association property="bookType" javaType="com.lanou3g.mybatis.bean.BookType">
            <id column="bt_id" property="id" />
            <result column="tname" property="tname" />
        </association>
    </resultMap>


    <select id="queryBooks" resultMap="book">
        select b.*,bt.*, b.id bid, bt.id bt_id from book b, booktype bt where b.btype = bt.id;
    </select>

多对多时使用collection

<mapper namespace="com.lanou3g.mybaties.dao.TushubiaoDao">
    <resultMap id="tushubiao" type="com.lanou3g.mybaties.bean.Tushubiao">
    <id column="tid" property="id"/>
    <result column="bname" property="bname"/>
    <result column="author" property="author"/>
    <result column="gender" property="gender"/>
    <result column="price" property="price"/>
    <result column="miaoshu" property="miaoshu"/>
    <collection property="btype" ofType="com.lanou3g.mybaties.bean.Leixing">
        <id column="id" property="id"/>
        <result column="tname" property="tname"/>
    </collection>
</resultMap>
    <select id="queryAllTu" resultMap="tushubiao">
        select t.*,l.*,t.id tid,l.id lid from tushubiao t,leixing l where t.btype=l.id;
    </select>

动态sql

**1. if标签 **

<select id="queryStudentByCondition" resultType="Student">
        select * from student
        <where>
            <if test="sname != null">
                sname = #{sname}
            </if>
            <if test="nickName != null">
                and nick_name = #{nickName}
            </if>
            <if test="id != null">
                and id = #{id}
            </if>
        </where>
    </select>

**2.choose标签 when标签 otherwise标签 **

有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句

<select id="queryChooseWhen" resultType="Student">
        select * from student
        <where>
            <choose>
                <when test="sname != null">
                    and sname = #{sname}
                </when>
                <when test="nickName != null">
                    and nick_name = #{nickName}
                </when>
                <otherwise>
                  and id = 5
                </otherwise>
            </choose>
        </where>
    </select>

3.set标签
set 元素会动态前置 SET 关键字,同时也会删掉无关的逗号(如:语句最后的逗号)

<update id="updateById" parameterType="Student">
        update student
        <set>
            <if test="sname != null">
                sname = #{sname},
            </if>
            <if test="nickName != null">
                nick_name = #{nickName},
            </if>
        </set>
        where id = #{id}
    </update>

4.trim标签
常用属性有:
prefix: 添加指定前缀
prefixOverrides: 删除指定前缀
suffixOverrides: 删除指定后缀

<update id="updateById" parameterType="Student">
        update student
        <trim prefix="set" suffixOverrides=",">
            <if test="sname != null">
                sname = #{sname},
            </if>
            <if test="nickName != null">
                nick_name = #{nickName},
            </if>
        </trim>
        where id = #{id}
    </update>

5.where标签
当我们拼接动态SQL时,如果一个查询条件都没有,那我们就不需要where子句,而如果有至少一个条件我们就需要where子句。这样,我们就需要做个判断,而mybatis里的标签就省去了我们自己做这个判断。

<select id="queryStudentByCondition" resultType="Student">
        select * from student
        <where>
            <if test="sname != null">
                sname = #{sname}
            </if>
            <if test="nickName != null">
                and nick_name = #{nickName}
            </if>
            <if test="id != null">
                and id = #{id}
            </if>
        </where>
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值