一双跑鞋的mybatis(四)---动态sql语句

本文详细介绍了如何使用MyBatis框架进行数据库操作,包括插入、查询、更新和删除等常见操作,以及如何利用XML配置文件和动态SQL实现灵活的数据访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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="org.senssic.bean.Address">
      
   <insert id="add" parameterType="org.senssic.bean.Address">
      insert into address(addrs,code) value(#{addrs},#{code})
   </insert>
   <!-- if -->
   <select id="slectif" parameterType="map" resultType="org.senssic.bean.Address">
      select * from address where addrs like #{addrs}
      <if test="code!=null">
         and code=#{code}
      </if>
    </select>
    <!-- choose,when,otherwise -->
      <select id="slectchoose" parameterType="map" resultType="org.senssic.bean.Address">
      select * from address where 
    <choose>
       <when test="code!=null">
           and code=#{code}
       </when>
       <when test="addrs!=null">
          and addrs like #{addrs}
       </when>
       <otherwise>
         and active=1
       </otherwise>
    </choose>
    </select>
    <!-- where避免and或者无条件时候 -->
    <select id="selectwhere" parameterType="map" resultType="org.senssic.bean.Address">
      select * from address
      <where>
         <if test="code!=null">
            code=#{code}
         </if>
         <if test="addrs!=null and active!=null">
           and addrs=#{addrs} and active=#{active}
         </if>
      </where>
      
    </select>
    <!-- set 动态设置set方法 -->
     <update id="updateAddress" parameterType="map">
        update address 
        <set>
           <if test="code!=null">code=#{code}</if>
           <if test="addrs!=null">addrs=#{addrs}</if>
        </set>
        where id=#{id}
        
     </update>
     <!-- foreach构建与in条件中,可以将外部的集合(list,数组等作为查询的子条件) -->
     <select id="selectPostIn" resultType="domain.blog.Post">
          SELECT *
          FROM POST P
           WHERE ID in
           <foreach item="item" index="index" collection="list"
              open="(" separator="," close=")">
              #{item}
           </foreach>
</select>
   </mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值