zoukankan      html  css  js  c++  java
  • 动态sql语句(即sql语句的拼接)

    10.1  if标签(属性:test(判断条件))

      缺点:如果仅仅是第二个条件满足导致sql语句出错(故使用if+where)

    1 <select id="select1" resultType="com.zhiyou.clg.bean.User">
    2         select *from user
    3         <if test="name!=null">
    4             where name=#{name}
    5         </if>
    6         <if test="age!=null and age!=''">
    7             and age=#{age}
    8         </if>
    9     </select>

    10.2  if+where ( where标签会知道如果它包含的标签中有返回值的话,就会插入where 若其以and或or开头会将and或or剔除)

     1 <select id="select2" resultType="com.zhiyou.clg.bean.User">
     2         select *from user
     3         <where>
     4             <if test="name!=null">
     5                 and name=#{name}
     6             </if>
     7             <if test="sex!=null">
     8                 and sex=#{sex}
     9             </if>
    10             <if test="age!=null and age!=''">
    11                 and age=#{age}
    12             </if>
    13         </where>
    14     </select>

    10.3 if+set( set标签会知道如果它包含的标签中有返回值的话,就会插入set并且剔除最后一个满足条件的逗号“,”)

     1 <update id="update1" parameterType="com.zhiyou.clg.bean.User">
     2         update user
     3         <set>
     4             <if test="name!=null">
     5                 name=#{name},
     6             </if>
     7             <if test="sex!=null">
     8                 sex=#{sex},
     9             </if>
    10             <if test="age!=null and age!=''">
    11                 age=#{age},
    12             </if>
    13         </set>
    14         <where>
    15             <if test="id!=null">
    16                 id=#{id}
    17             </if>
    18         </where>
    19     </update

    10.4 choose(标签when+标签otherwise)----类似于switch语句,有且仅有一个条件会满足

     1 <sql id="usercolumn" >
     2          id,name,age,sex
     3      </sql>
     4     <select id="select3" resultType="com.zhiyou.clg.bean.User">
     5         select 
     6             <include refid="usercolumn"></include>
     7          from user
     8         <where>
     9             <choose>
    10                 <when test="name!=null">
    11                     and name=#{name}
    12                 </when>
    13                 <when test="age!=null and age!=''">
    14                     and age=#{age}
    15                 </when>
    16                 <otherwise>
    17                     and sex=#{sex}
    18                 </otherwise>
    19             </choose>
    20         </where>
    21     </select>

    10.5 trim(trim标记是一个格式化的标记,可以完成set或者是where标记的功能;)

      属性:prefix:前缀      

            prefixoverrides:去掉前缀

            suffix:后缀      

            suffixoverrides:去掉后缀

     1 <update id="update2" parameterType="com.zhiyou.clg.bean.User">
     2         update user
     3             <trim prefix="set" suffixOverrides=",">
     4                 <if test="name!=null">
     5                     name=#{name},
     6                 </if>
     7                 <if test="sex!=null">
     8                     sex=#{sex},
     9                 </if>
    10                 <if test="age!=null and age!=''">
    11                     age=#{age},
    12                 </if>
    13             </trim>
    14         <where>
    15             <if test="id!=null">
    16                 id=#{id}
    17             </if>
    18         </where>
    19     </update>

    10.6 sql片段------使用sql标签定义(属性:id);引用片段时用include标签(属性:refid)

    10.7 foreach

      属性:  collection:指定输入对象中的集合属性

                   item:每次遍历生成的对象

                   open:开始遍历时的拼接字符串

                   close:结束时拼接的字符串

                   separator:遍历对象之间需要拼接的字符串

     1 <sql id="namecolumn" >
     2          name
     3      </sql>
     4     <select id="select4" resultType="com.zhiyou.clg.bean.User">
     5         select 
     6             <include refid="namecolumn"></include>
     7         from user where id in 
     8             <foreach collection="ids" open="(" close=")" separator="," item="id">
     9                 #{id}
    10             </foreach>
    11     </select>

    10.8模糊查询-----like后使用concat函数拼接(‘%’,#{name},’%’)

            name  like  concat(‘%’,#{name},’%’)

  • 相关阅读:
    打印乘法口诀
    打印三角形
    java语言基础
    java环境配置
    postgresql新建插入存储过程
    postman做自动化
    fiddler抓包篡改数据实例
    log4j常用4个日志级别
    来一个简单点的表单提交
    mapper文件简单格式
  • 原文地址:https://www.cnblogs.com/lwgok1003/p/11442717.html
Copyright © 2011-2022 走看看