SSM--MyBatis框架之动态SQL

SSM--MyBatis框架之动态SQL

文章目录

动态SQL

if+trim(一般用于增加)

prefix 给sql语句拼接的前缀

suffix 给sql语句拼接的后缀

prefixOverrides 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定

suffixOverrides 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定

xml 复制代码
<insert id="addBill" >
    insert into smbms_bill
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="billCode != null">billCode,</if>
        <if test="productName != null">productName,</if>
        <if test="totalPrice != null">totalPrice,</if>
        <if test="isPayment != null">isPayment</if>
    </trim>
    values
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="billCode != null"> #{billCode},</if>
        <if test="productName != null">#{productName},</if>
        <if test="totalPrice != null">#{totalPrice},</if>
        <if test="isPayment != null">#{isPayment}</if>
    </trim>
</insert>

if+set(用于修改)

set能够自动识别并去除多余的逗号

xml 复制代码
<update id="updateBill">
update smbms_bill
<set>
    <if test="billCode!=null">billCode = #{billCode},</if>
    <if test="productName!=null">productName = #{productName},</if>
    <if test="totalPrice!=null">totalPrice = #{totalPrice},</if>
    <if test="isPayment!=null">isPayment = #{isPayment}</if>
</set>
where id=#{id}
</update>

if+where(一般用于查询)

where标签能自动识别并去除多余的and

java 复制代码
<select id="findBillList" resultType="Bill">
    select
    	id,
        billCode,
        productName
        from smbms_bill 
    <where>
        <if test="billCode!=null">a.billCode like concat('%',#{billCode},'%')</if>
        <if test="productName!=null">and a.productName like concat('%',#{productName},'%')</if>
    </where>
</select>

foreach

List集合 collection:指定循环集合类型 list

xml 复制代码
<select id="findBillByProviderList" resultType="Bill">
    select  
        id,
        billCode,
        productName,
        productDesc
    from smbms_bill
    where providerId in
    <foreach item="id" collection="list" open="(" separator="," close=")">
        #{id}
    </foreach>
</select>
java 复制代码
//测试类
@Test
public void findBillByProviderList() {
	billDao.findBillByProviderList(Arrays.asList(1,2,3));
}

Array数组 collection:指定循环集合类型array

xml 复制代码
<select id="findBillByProviderArray" resultType="Bill">
     select  
         id,
         billCode,
         productName,
         productDesc
         from smbms_bill
     where providerId in
     <foreach item="id" collection="array" open="(" separator="," close=")">
         #{id}
     </foreach>
</select>
java 复制代码
@Test
public void findBillByProviderArray() {
	billDao.findBillByProviderArray(new Integer[]{1,2,3});
}

Map集合 collection:指定循环集合类型 map-key

#{ map-key值}

xml 复制代码
<select id="findBillByProviderMap" resultType="Bill">
    select  
    id,
    billCode,
    productName,
    productDesc
    from smbms_bill
    where providerId in
    <foreach item="id" collection="pIds" open="(" separator="," close=")">
        #{id}
    </foreach>
    and productName like concat('%',#{productName},'%')
</select>
java 复制代码
@Test
public void findBillByProviderMap() {
    Map<String,Object> map=new HashMap<>();
    map.put("pIds",Arrays.asList(1,2,3));
    map.put("productName","米");
    billDao.findBillByProviderMap(map);
}

sql片段

对于可重用的SQL代码片段使用sql标签

include标签引用SQL片段

xml 复制代码
<select id="findBillByProviderMap" resultType="Bill">
    <include refid="selectBill"/>
    where providerId in
    <foreach item="id" collection="pIds" open="(" separator="," close=")">
        #{id}
    </foreach>
    and productName like concat('%',#{productName},'%')
</select>
<sql id="selectBill">
        select id,
               billCode,
               productName,
               productDesc
        from smbms_bill
</sql>

choose(when otherwise)

xml 复制代码
<choose>
 <when test ="条件1"> ...</when>
 <when test ="条件2"> ...</when>
 <when test ="条件3"> ...</when>
	...
 <otherwise>...</otherwise>
</choose>
相关推荐
Sammyyyyy6 分钟前
Symfony AI 正式发布,PHP 原生 AI 时代开启
开发语言·人工智能·后端·php·symfony·servbay
C+-C资深大佬6 分钟前
C++逻辑运算
开发语言·c++·算法
汽车仪器仪表相关领域9 分钟前
光轴精准测量,安全照明保障——NHD-8101/8000型远近光检测仪项目实战分享
数据库·人工智能·安全·压力测试·可用性测试
掘根14 分钟前
【仿Muduo库项目】EventLoop模块
java·开发语言
大爱编程♡18 分钟前
Spring IoC&DI
数据库·mysql·spring
信码由缰20 分钟前
Java 中的 AI 与机器学习:TensorFlow、DJL 与企业级 AI
java
king_harry27 分钟前
金仓数据库KingbaseES中WalMiner接口使用
数据库·kingbase·walminer
爱潜水的小L31 分钟前
自学嵌入式day43,商城网页
数据库·oracle
IvorySQL36 分钟前
PostgreSQL 的 SQL 查询之旅
数据库·人工智能·postgresql·开源
꧁Q༒ོγ꧂39 分钟前
算法详解(三)--递归与分治
开发语言·c++·算法·排序算法