qidao123.com技术社区-IT企服评测·应用市场

标题: 动态SQL [打印本页]

作者: 大连全瓷种植牙齿制作中心    时间: 2024-12-18 14:46
标题: 动态SQL
目录
if
where
trim
set
choose-when-otherwise
foreach
批量删除
 批量插入
sql标签与include标签


   不能使用&&,只能使用 and
  空值:name != null and name != ''
  if

  1. public List<Monster> findMonsterByAge(@Param(value = "agex") Integer age);
复制代码
  1.     <select id="findMonsterByAge" resultType="Monster" parameterType="Integer">
  2.         select * from `monster` where 1 = 1
  3.         <if test="agex >= 0">
  4.             and `age` > #{agex}
  5.         </if>
  6.         <if test="xxx">
  7.             and xxx
  8.         </if>
  9.     </select>
复制代码
 
   select * from monster where [1 = 1] [and] age > #{agex} and xxx
  假如第一个不成立,要加 1 = 1,where 1 = 1 and xxx
  假如全成立,要加and,where 1 = 1 and age > #{agex} and xxx
  where

   所有条件为空时,where标签保证不会生成where子句
  自动去除某些条件前面多余的and或or 
  1.     <select id="findMonsterByIdAndName" parameterType="Monster" resultType="Monster">
  2.         select * from `monster`
  3.         <where>
  4.             <if test="id >= 0">
  5.                 and `id` > #{id}
  6.             </if>
  7.             <if test="name != null and name != ''">
  8.                 and `name` = #{name}
  9.             </if>
  10.         </where>
  11.     </select>
复制代码
trim

   在trim标签前/后添加前/后缀
          preifx:加前缀
          suffix:加后缀
  把trim标签内容的前/后缀去掉
          prefixOverrides:删除前缀
          suffixOverrides:删除后缀
  
 
set

   重要用在update语句中,用来生成set关键字,同时去掉末了多余的 ","
  只更新提交的不为空的字段,假如提交的数据是空大概“ ”,那么这个字段将不更新数据
  正常的update语句:null也会传给数据库
  1.     <update id="setMonster" parameterType="map">
  2.         update `monster`
  3.         <set>
  4.             <if test="age != null and age != ''">
  5.                 `age` = #{age},
  6.             </if>
  7.             <if test="email != null and age != ''">
  8.                 `email` = #{email},
  9.             </if>
  10.         </set>
  11.         where id = #{id}
  12.     </update>
复制代码
choose-when-otherwise

   相当于if elseif elseif else
  先根据name查,name为空根据id查,否则根据salary查
  假如都为空,会实行末了一条语句
  1.     <select id="findchoose" parameterType="map" resultType="Monster">
  2.         select * from `monster`
  3.         <choose>
  4.             <when test="name != null and name != ''">
  5.                 where `name` = #{name}
  6.             </when>
  7.             <when test="id > 0">
  8.                 where `id` > #{id}
  9.             </when>
  10.             <otherwise>
  11.                 where `salary` > 100
  12.             </otherwise>
  13.         </choose>
  14.     </select>
复制代码
foreach

   常用于对集合进行遍历(尤其是在构建IN条件语句的时候)
  collection:指定命组或集合
  item:代表数组或集合中的元素
  separator:循环之间的分隔符
  open:foreach循环拼接的所有sql语句的最前面以什么开始
  close:foreach循环拼接的所有sql语句的末了面以什么开始
   
  底层map存放的key是array或arg0,value是这个数组
  map.put("array",数组)、map.put("arg0",数组)
  不写注解:collection= array 或 arg0
  写注解@Param("name"):collection = "name"
  批量删除

  1. int deleteByIds (@Param("ids") Long[] ids);
复制代码
  1. <select id="deleteByIds">
  2.         delete from `monster`
  3.         <if test="ids != null and ids != ''">
  4.             <where>
  5.                 id IN
  6.                 <foreach collection="ids" item="idx" open="(" separator="," close=")">
  7.                     #{idx}
  8.                 </foreach>
  9.             </where>
  10.         </if>
  11. </select>
复制代码
 批量插入


  1. List<Car> cars = Arrays.asList(car1, car2, car3);
复制代码
  1. int insertBatch (@Param("cars") List<Car> cars);
复制代码
  1. <insert id="insertBatchByForeach">
  2.   insert into t_car values
  3.   <foreach collection="cars" item="car" separator=",">
  4.     (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
  5.   </foreach>
  6. </insert>
复制代码
sql标签与include标签

   sql标签用来声明sql片段
  include标签用来将声明的sql片段包含到某个sql语句当中
  作用:代码复用。易维护。
  1.     <sql id="carCols">
  2.         id,
  3.         car_num carNum,
  4.         brand,guide_price guidePrice,
  5.         produce_time produceTime,
  6.         car_type carType
  7.     </sql>
  8.     <select id="selectAllRetMap" resultType="map">
  9.         select
  10.             <include refid="carCols"/>
  11.         from t_car
  12.     </select>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4