mybatis查询语句与动态sql

本文详细介绍了MyBatis框架中如何处理多参数,结果映射的定义与使用,包括id和property的配置、驼峰命名规则、动态SQL中的if/where标签、trim和set标签,以及foreach标签在批量操作中的应用,如删除和插入
摘要由CSDN通过智能技术生成

1.mybatis多参数

  • 如果是多个参数的话,mybatis框架底层是怎么做的呢?
    mybatis框架会自动创建一个Map集合,并且Map集合是以这种方式存储参数的:
map.put("arg0",name);
map.put("arg1",sex);
map.put("param1",name);
map.put("param2",sex);

2.mybatis查询之结果映射

<!--
    1.专门定义一个结果映射,在这个结果映射当中指定数据库表的字段名和java类的属性名的对应关系
    2.type属性:用来指定POJO类的类名
    3.id属性:指定resultMap的唯一标识,这个id将来要在select标签中使用。
-->
<resultMap id="carResultMap" type="Car">
    <!--如果数据库表中有主键,一般都是有主键,要不然不符合数据库设计第一范式-->
    <!--如果有主键,建议这里配置一个id标签,注意:这不是必须的,但是官方的解释是什么呢?这样的配置可以让mybatis提高效率-->
    <id property="id" column="id"/>
    <!--property后面填写POJO类的属性名-->
    <!--column后面填写数据库表的字段名-->
    <result property="carNum" column="car_num"/>
    <!--如果column和property是一样的,这个可以省略-->
    <!--<result property="brand" column="brand"/>-->
    <result property="guidePrice" column="guide_price"/>
    <result property="produceTime" column="produce_time"/>
    <result property="carType" column="car_type"/>
</resultMap>

<!--select标签的resultMap属性,用来指定使用哪个结果映射,resultMap后面的值是resultMap的id-->
<select id="selectAllByResultMap" resultMap="carResultMap">
    select * from t_car
</select>

3.开启驼峰自动映射

<settings>
    <!--在mybatis-config.xml文件中,开启驼峰自动映射-->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

4.动态sql

  1. if标签
<select id="selectByMultiCondition" resultType="car">
    select * from t_car where 1=1
    <!--
        1.if标签中test属性是必须的。
        2.if标签中test属性的值是false或者true。
        3.如果test是true,则if标签中的sql语句就会拼接,反之,则不会拼接。
        4.test属性中可以使用的是:
            当使用了@Param注解,那么test中要出现的是@Param注解指定的参数名。@Param("brand"),那么这里只能使用brand
            当没有使用@Param注解,那么test中要出现的是:param1 param2 param3 arg0 arg1 arg2...
            当使用了POJO,那么test中出现的是POJO类的属性名。
        5.在mybatis的动态sql中,不能使用&&,只能使用and。
    -->
    <if test="brand != null and brand != ''">
        and brand like "%"#{brand}"%"
    </if>
    <if test="guidePrice != null and guidePrice != ''">
        and guide_price > #{guidePrice}
    </if>
    <if test="carType != null and carType != ''">
        and car_type = #{carType}
    </if>
</select>
  1. where标签
<select id="selectByMultiConditionWithWhere" resultType="car">
    select * from t_car
    <!--where标签是专门负责where子句动态生成的
        会自动去掉前面多余的and或or
        不会去掉后面多余的and或or
    -->
    <where>
        <if test="brand != null and brand != ''">
            brand like "%"#{brand}"%"
        </if>
        <if test="guidePrice != null and guidePrice != ''">
            and guide_price > #{guidePrice}
        </if>
        <if test="carType != null and carType != ''">
            and car_type = #{carType}
        </if>
    </where>
</select>
  1. trim标签
<select id="selectByMultiConditionWithTrim" resultType="car">
    select * from t_car
        <!--
            prefix:加前缀
            suffix:加后缀
            prifixOverrides:删除前缀
            suffixOverrides:删除后缀
        -->
        <!--prefix="where" 是在trim标签所有内容的前面添加 where-->
        <!--suffixOverrides="and|or" 把trim标签中内容的后缀and或or去掉-->
    <trim prefix="where" suffixOverrides="and|or" prefixOverrides="and|or">
        <if test="brand != null and brand != ''">
            brand like "%"#{brand}"%"
        </if>
        <if test="guidePrice != null and guidePrice != ''">
            and guide_price > #{guidePrice}
        </if>
        <if test="carType != null and carType != ''">
            and car_type = #{carType}
        </if>
    </trim>
</select>
  1. set标签
<update id="updateBySet">
    update t_car
    <set>
        <if test="carNum != null and carNum != ''">car_num = #{carNum},</if>
        <if test="brand != null and brand != ''">brand = #{brand},</if>
        <if test="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if>
        <if test="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if>
        <if test="carType != null and carType != ''">car_type = #{carType}</if>
    </set>
    where id = #{id}
</update>
  1. choose标签
<select id="selectByChoose" resultType="car">
    select * from t_car
    <where>
        <choose>
            <when test="brand != null and brand != ''">brand like "%"#{brand}"%"</when>
            <when test="guidePrice != null and guidePrice != ''">guide_price > #{guidePrice}</when>
            <otherwise>
                car_type = #{carType}
            </otherwise>
        </choose>
    </where>
</select>
  1. foreach批量删除和批量插入
<!--
    foreach标签的属性:
        collection:指定数组或者集合
        item:代表数组或集合中的元素
        separator:循环之间的分隔符
        open:foreach循环拼接的所有sql语句的最前面以什么开始
        close:foreach循环拼接的所有sql语句的最后面以什么结束

    collection="ids" 第一次写这个的时候报错了,错误信息是:[array,arg0]
    什么意思?
        map.put("array",数组);
        map.put("arg0",数组);
-->
<delete id="deleteByIds">
    <!--delete from t_car where id in (
    <foreach collection="ids" item="id" separator=",">
        #{id}
    </foreach>
    )-->
    delete from t_car where id in
    <foreach collection="ids" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
</delete>

<!--批量删除,使用or关键字-->
<delete id="deleteByIds2">
    delete from t_car where
    <foreach collection="ids" item="id" separator="or">
        id = #{id}
    </foreach>
</delete>

<!--批量插入-->
<insert id="insertBatch">
    insert into t_car (id,car_num,brand,guide_price,produce_time,car_type) values
    <foreach collection="cars" item="car" separator=",">
        (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
    </foreach>
</insert>
  1. sql标签和include标签
<!--声明一个SQL片段-->
<sql id="carColumnNameSql">
    id,
    car_num as carNum,
    brand,
    guide_price as guidePrice,
    produce_time as produceTime,
    car_type as carType
</sql>

<select id="selectById2" resultType="car">
    select
        <!--将声明的SQL片段包含进来-->
        <include refid="carColumnNameSql"></include>
    from t_car
    where id = #{id}
</select>
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值