Bootstrap

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>
;