zoukankan      html  css  js  c++  java
  • Mybatis动态sql语句

    
    1.if
    通过if把会做的事情有条件的包含在where里面,例子:
    select * from tb_employee where state='active'
    <if test='id!=null and sex!=null'>
    and id=#{id} and sex=#{sex}
    </if>
    这句话意思是如果没有传入id这个参数,那么返回所有state='active'的结果,如果传入id以及sex参数,那么只返回特定id
    的active状态的结果。
    
    2.choose(when otherwise)
    choose相当于JAVA中的switch,通过用户传入的参数进行条件选择,从而执行相应的语句。例子:
    <select id="selectByChoose" resultMap="userResultMap">
    	 select * from tb_user where tb_name='12'
    	 <choose>
    	 <when test="tb_id!=null">
    	 and tb_id=#{tb_id}
    	 </when>
    	 <when test="tb_age!=null and tb_sex!=null">
    	 and tb_age=#{tb_age} and tb_sex=#{tb_sex}
    	 </when>
    	 <otherwise>
    	 and tb_sex='q'
    	 </otherwise>
    	 </choose>
    	</select>
    上述代码的意思是当传入tb_id参数执行tb_id,传入tb_sex以及tb_age的时候执行tb_age and tb_sex,当不传入参数
    执行otherwise的语句,如果同时传入三个参数,那么会按照第一个条件进行。
    
    3.where
    where元素知道只有在一个以上的if有值的情况下才去插入where子句。同时他会自动帮忙剔除或增加AND 或者OR开头的。
    <select id="selectByChoose" resultMap="userResultMap">
    	 select * from tb_user 
    	 <where>
    	 <if test="tb_id!=null">
    	tb_id=#{tb_id}
    	 </if>
    	 <if test="tb_age!=null and tb_sex!=null">
    	 and tb_age=#{tb_age} and tb_sex=#{tb_sex}
    	 </if>
    	 </where>
    	</select>
    上述代码中的tb_age以及tb_sex条件判断内容是AND语句,如果前面没有传入tb_id,那么where会自动把AND前缀去掉。
    
    4.set
    set元素可以被用于动态包含需要更新的列,而舍去其他的,也就是说可以通过用户提交的信息选择性的进行更新。例子:
    <update id="updateIfNecessary" parameterType="com.vo.User">
    		update  tb_user 
    		<set>
    		 <if test="name!=null">tb_name=#{name}</if>
    		 <if test="sex!=null">tb_sex=#{sex}</if>
    		 <if test="age!=null">tb_age=#{age}</if>
    		</set>
           where tb_id=#{id}
    	</update>
    上述代码中if语句里面包括了要更新的列名,当传入的User对象中的某些属性没有设置的时候则会过滤掉这些属性,而只修改
    传入的属性。
    
    5.foreach
    主要用来遍历集合查询相关结果。例子:
    <select id="selectByList" resultMap="userResultMap">
    	select * from tb_user where tb_id in
    	<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
    	#{item}
    	</foreach>
    	</select>
    上述代码中
    item表示集合中每一个元素进行迭代时的别名,
        index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
        open表示该语句以什么开始,
        separator表示在每次进行迭代之间以什么符号作为分隔 符,
        close表示以什么结束。
    在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况
    下,该属性的值是不一样的,主要有一下3种情况:
    
        1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
        2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
        3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
    以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参
    数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
    
    6.bind
    主要用来从OGNL表达式中创建一个变量并绑定要上下文,例如查询名字中含有“周”的用户信息。例子:
    <select id="selectByLike" resultMap="userResultMap">
    	<bind name="pattern" value="'%'+_parameter.getId()+'%'"></bind>
    	select * from tb_user where tb_id like #{pattern}
    	</select>
    上述代码中要注意value的取值,_parameter表示的是获取到的对象,这个很关键,要不然为空会获取不到。整个意思是查询
    id中含有2的数据信息。
    
    
  • 相关阅读:
    信息安全系统设计基础实验五:通讯协议设计 20135211李行之 20135216刘蔚然
    信息安全系统设计基础实验四:外设驱动程序设计 20135211李行之&20135216刘蔚然
    信息安全系统设计基础第十二周学习总结
    实验三-实时系统的移植 20135211李行之 20135216刘蔚然
    实验二-固件设计 20135211李行之 20135216刘蔚然
    信息安全系统设计基础第十一周学习总结
    信息安全系统设计基础实验一 20135211&20135216
    信息安全设计基础第十周学习总结
    Oracle如何创建表空间、用户及授权
    设置nginx支持虚拟机,支持多域名多网站
  • 原文地址:https://www.cnblogs.com/lovellll/p/10222508.html
Copyright © 2011-2022 走看看