Mybatis入门笔记(xml篇)


前言

简单学习一下Mybatis的xml入门操作.
跟学视频 : 黑马Mybatis入门视频.


知识点

1. mapper标签

<mapper namespace=“…”> 表示本xml文件对应的Mapper接口.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--对应接口为BrandMapper接口-->
<mapper namespace="com.donkiss.mybatis001.mapper.BrandMapper">
    ......
</mapper>

2. resultMap

<resultMap>映射, 当pojo类为驼峰命名且数据库字段为下划线命名时, 可以更方便地将数据库字段(column)与pojo类字段(property)做映射.

<!--resultMap映射-->
<!--可以只写无法匹配的映射,也可以全写-->
<!--property 为类属性名, column为数据库字段名-->
<resultMap id="brandMap" type="com.ricardo.mybatis001.pojo.Brand">
    <id property="id" column="id"/>
    <result property="brandName" column="brand_name"/>
    <result property="companyName" column="company_name"/>
    <result property="ordered" column="ordered"/>
    <result property="status" column="status"/>
    <result property="description" column="description"/>
</resultMap>

3. #{} 占位符

xml的sql中的#{var}表示Mapper中的传入参数var.

<!--Mapper该方法入参变量名为"id"-->
<select id="selectById" resultMap="brandMap">
    select *
    from tb_brand
    where id = #{id};
</select>

4. where标签

<where>标签, 代替where关键词, 可构建动态查询sql.

<select id="selectByCondition" resultMap="brandMap">
    SELECT
    *
    FROM tb_brand
    <where>
        <if test="companyName != null and companyName != ''">
            AND company_name LIKE #{companyName}
        </if>
        <if test="brandName != null and brandName != ''">
            AND brand_name LIKE #{brandName}
        </if>
        <if test="status != null">
            AND status = #{status}
        </if>
    </where>
</select>

5. choose标签

<choose>标签, 相当于Java的switch, 碰到符合的就返回.
where-if 和 where-choose-when 标签组的区别在于 :

  • if 后为 true 则会将对应sql语句拼接.
  • choose-when 后为true则停止, 不继续往下.
<!--
    choose(when, otherwise),类似Java的switch
-->
<select id="selectBySingleCondition" resultMap="brandMap">
    SELECT
    *
    FROM TABLE ( "tb_brand" )
    <where>
        <choose> <!--相当于switch-->
            <when test="companyName != null and companyName != ''"> <!--相当于case-->
                company_Name LIKE #{companyName}
            </when>
            <when test="brandName != null and brandName != ''">
                brand_name LIKE #{brandName}
            </when>
            <when test="status != null">
                status = #{status}
            </when>
        </choose>
    </where>
</select>

6. set标签

<set>标签, 适用于sql的update关键词, 做动态sql拼接.

<update id="update">
    UPDATE tb_brand
    <set>
        <if test="brandName != null and brandName != ''">
            brand_name = #{brandName},
        </if>
        <if test="companyName != null and companyName != ''">
            company_name = #{companyName},
        </if>
        <if test="ordered != null">
            ordered = #{ordered},
        </if>
        <if test="description != null and description != ''">
            description = #{description},
        </if>
        <if test="status != null">
            status = #{status},
        </if>
    </set>
    where id = #{id}
</update>

7. foreach标签

<foreach>标签, 适用于传入数组或列表时, 做动态遍历拼接sql.

<delete id="deleteByIds">
    DELETE FROM tb_brand
    WHERE id IN
    <foreach collection="ids" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
    ;
</delete>
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值