动态SQL做增删改查操作

1.什么是动态SQL

sql的内容是变化的, 可以根据条件获取到不同的sql语句.
主要是where部分发生变化。
动态sql的实现, 使用的是mybatis提供的标签

2.动态SQL常用标签

元素作用描述
if条件判断单条件判断
choose(when、otherwise)条件选择多条件分支判断
where.set辅助(条件)处理SQL语句拼接问题
foreach循环(批量插入 修改)循环遍历

3.具体使用

mapper.xml文件

<?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">
<mapper namespace="ShopInfoMapper">
    <sql id="tableName" >
        `shop_info`
    </sql>
    <sql id="columns">
        `shop_info_name`,
		`shop_info_price`,
		`shop_info_remark`,
        `modify_time`
    </sql>
    <sql id="params">
        #{shop_info_name},
        #{shop_info_price},
        #{shop_info_remark},
        #{modify_time}
    </sql>
    <sql id="primaryKey">
        `shop_info_id`
    </sql>
    <sql id="id">
        ${zyu.id}
    </sql>

    <!-- 动态查询语句 -->
    <select id="query" resultType="map" parameterType="map">
        select
        <include refid="primaryKey"></include>,<include refid="columns"></include>
        from
        <include refid="tableName"></include>
        <trim prefix="where" prefixOverrides="and |or" >
            <if test="map!=null">
                <foreach collection="map"  index ="key"  item="val" separator=" and ">
                    ${key} = #{val}
                </foreach>
            </if>
        </trim>
        where
        <![CDATA[ del=1 ]]>
    </select>
    <!-- 动态查询列表总数语句 -->
    <select id="queryCount" resultType="map" parameterType="map">
        select
        count(*) `count`
        from
        <include refid="tableName"></include>
        <trim prefix="where" prefixOverrides="and |or" >
            <if test="map!=null">
                <foreach collection="map"  index ="key"  item="val" separator=" and ">
                    ${key} = #{val}
                </foreach>
            </if>
        </trim>
    </select>

    <!-- 动态修改语句 -->
    <update id="update"  parameterType="map">
        update
        <include refid="tableName"></include>
        <set>
            <foreach collection="zyu"  index ="key"  item="val" separator=",">
            <if test="key !='id'">
                ${key} = #{val}
            </if>
            </foreach>
        </set>
        <where>
            <include refid="primaryKey"></include> = <include refid="id"></include>
        </where>
    </update>


    <!-- 动态新增语句 -->
    <insert id="add" parameterType="map">
        <selectKey resultType="INTEGER" order="AFTER" keyProperty="shop_info_id">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into
        <include refid="tableName"></include>
        (
        <include refid="columns"></include>
        )
        values
        (
        <include refid="params"></include>
        )
    </insert>

    <!-- 逻辑删除语句 -->
    <update id="del"  parameterType="map">
        update
        <include refid="tableName"></include>
        set
        <![CDATA[ del=0 ]]>

        where
        shop_info_id in (${id})
    </update>
</mapper>

具体使用可自己自行修改参数

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值