Java_ResultMap映_查询-多条件-动态条件查询-增删改-SQ

ResultMap映射

<mapper namespace="com.it.mapper.BrandMapper">
    <!-- id, brand_name as brandName, company_name as companyName, ordered, description, status -->
    <!--
            数据库表的字段名称 和 实体类的属性名称不一样,则不能自动封装数据
                *起别名:对不一样的列名起别名,让别名和实体类的属性名一样
                    *缺点:每次查询都要定义一次别名
                        * sqL片段
                            *缺点:不灵活
                * resultMap:
                    1·定义<resuLtMap>标签
                    2·在<select>标签中,使用resultMap属性替换resultType属性
    -->

<!--     注意:只需要定义字段名和属性名不一样的映射,而一样的则不需要专门定义出来。-->
    <resultMap id="brandResultMap" type="brand">
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName"/>
    </resultMap>

查询全部SQL

    <select id="selectAll" resultMap="brandResultMap">
        select *
        from tb_brand;
    </select>

根据id查询

    <select id="selectById" resultMap="brandResultMap">
        select *
        from tb_brand
        where id = #{id};
    </select>

多条件查询

    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="companyName != null and companyName !='' ">
                and company_name like #{companyName}
            </if>
            <if test="brandName != null and brandName !='' ">
                and brand_name like #{brandName};
            </if>
        </where>
    </select>

单条件查询

<select id="selectByConditionSingle" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <choose>
                <when test="status != null">
                    status = #{status}
                </when>
                <when test="companyName != null and companyName !=''">
                    company_name like #{companyName}
                </when>
                <when test="brandName != null and brandName !=''">
                    brand_name like #{brandName}
                </when>
            </choose>
        </where>
    </select>

添加

    <insert id="add" useGeneratedKeys="true" keyProperty="id">
        insert into tb_brand (brand_name, company_name, ordered, description, status)
        values (#{brandName},#{companyName},#{ordered},#{description},#{status});
    </insert>

根据id修改

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

根据id删除(删除单个)

    <delete id="deleteById">
        delete
        from tb_brand
        where id = #{id};
    </delete>

根据id删除(批量删除)

<!--
        mybatis会将数组参数,封装为一个Map集合。
            *默认: array=数组
            *使用@Param注解改变map集合的默认key的名称
-->
    <delete id="deleteById">
        delete from tb_brand where id in
        <foreach collection="array" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七@归七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值