mybatis 学习笔记(三)动态SQL

MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。主要标签有


if
choose(when,otherwise)
trim
where
set
foreach



(一)if就是简单的条件判断语句 例如下代码


<select id="queryIFTest"  parameterType="com.lql.study.model.Person" resultMap="resultListPerson">
      select * from  person where 1=1
      <if test="name!=null">
         and name=#{name}
      </if>
       <if test="sex!=null">
         and sex=#{sex}
      </if>
      <if test="age!=null">
         and age>=#{age}
      </if>
    </select>



(二)上面这段代码发现 因为要拼接 条件 所在加上了1=1这样子的条件,其实可以换一个标签更灵活的使用,where语句的作用主要是简化SQL语句中where中的条件判断 它可以智能的去掉条件里的 and或者or 例如下代码

<select id="queryWhereTest"  parameterType="com.lql.study.model.Person" resultMap="resultListPerson">
      select * from  person 
      
      <where>
      <if test="name!=null">
          name=#{name}
      </if>
       <if test="sex!=null">
         and sex=#{sex}
      </if>
      <if test="age!=null">
         and age>=#{age}
      </if>
      
      </where>
    </select>



(三)
choose元素的作用就相当于JAVA中的switch语句,基本上跟JSTL中的choose的作用和用法是一样的只选择其中一个条件执行,通常都是与when和otherwise搭配的。看如下一个例子:

<select id="queryChooseTest"  parameterType="com.lql.study.model.Person" resultMap="resultListPerson">
      select * from  person where 1=1
      
      <choose>
      <when test="name!=null">
         and name=#{name}
      </when>
       <when test="sex!=null">
         and sex=#{sex}
      </when>
      <when test="age!=null">
         and age=#{age}
      </when>
      
      </choose>
    </select>



(四)trim标识为格式化标识,可以更灵活的去处多余关键字的标签,以及与其他标识完成where和set的功能

prefix  当前缀有 
suffix  当后缀有  
prefixOverrides 自动判断前置  
suffixOverrides 自动判断后置
看例子吧比较清楚一些

 <select id="queryTrimTest"  parameterType="com.lql.study.model.Person" resultMap="resultListPerson">
      select * from  person 
      
      <trim prefix="where" prefixOverrides="and |or">
      <if test="name!=null">
          name=#{name}
      </if>
       <if test="sex!=null">
         or sex=#{sex}
      </if>
      <if test="age!=null">
         and age>=#{age}
      </if>
      
      </trim>
    </select>



上面这段代码的意思是:当WHERE后紧随AND或则OR的时候,就去除AND或者OR。 除了WHERE以外, 其实还有一个比较经典的实现,那就是SET。




(五)
set元素主要是用在更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。有了set元素我们就可以动态的更新那些修改了的字段。下面是一段示例代码:


 <update id="testSetPerson" parameterType="com.lql.study.model.Person" >
        update person 
        <set>
        <if test="name!=null">
          name=#{name},
       </if>
       <if test="sex!=null">
         sex=#{sex},
      </if>
      <if test="age!=null">
         age>=#{age},
       </if>
       <if test="remark!=null">
         remark>=#{remark},
       </if>
        </set>
        
        where pid=#{pid}
        
    </update>
    




(六)
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key。

看下面两个例子一个是单参数,一个是多参数例子

1.单参数
 <select id="queryForeachTest"  parameterType="com.lql.study.model.Person" resultMap="resultListPerson">
      select * from  person where age in
      <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
    </select>


public void queryForeachTest(List<Integer>ages) 
	{
		SqlSession session = sessionFactory.openSession();
		 IPersonOption op=session.getMapper(IPersonOption.class);
		 List<Person> ps=op.queryForeachTest(ages);
		 if(ps.size()>0)
		 {
			 for(int i=0;i<ps.size();i++)
			 {
				 System.out.println(ps.get(i).getName()+","+ps.get(i).getSex()+" ,"+ps.get(i).getAge());
			 }
		 }
		 session.close();
	}

2.多参数
 <select id="queryForeachTest2"  parameterType="com.lql.study.model.Person" resultMap="resultListPerson">
      select * from  person where sex=#{sex} and age in
      <foreach collection="ages" index="index" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
    </select>



1)实现方法


public void queryForeachTest2(Map<String, Object> params) 
	{
		SqlSession session = sessionFactory.openSession();
		 IPersonOption op=session.getMapper(IPersonOption.class);
		 
		 List<Person> ps=op.queryForeachTest2(params);
		 if(ps.size()>0)
		 {
			 for(int i=0;i<ps.size();i++)
			 {
				 System.out.println(ps.get(i).getName()+","+ps.get(i).getSex()+" ,"+ps.get(i).getAge());
			 }
		 }
		 session.close();
	}


2)调用代码
ArrayList<Integer> ages=new ArrayList <Integer>();
			ages.add(35);
			ages.add(18);
			 Map<String, Object> params = new HashMap<String, Object>();
			 params.put("ages", ages);
			 params.put("sex", "女");
			 test.queryForeachTest2(params);
			 

猜你喜欢

转载自lqllinda01.iteye.com/blog/2303092
今日推荐