Mybatis <where> <if> <set> <trim> <choose>标签
2017-09-16 22:24
721 查看
默认表格 student
<where> <if>
<if>标签单独使用,如下
如果name为空或空串 则进行全部查询,效率会下降
当进行多条件下查询是,<where> <if> 可以提高查询速度 和优化查询语句
<set> <if>
使用
<trim> <if>
我个人感觉<trim>标签就是<where><set>结合 他同时具有两者的功能
<choose> 应用场景 多选一 跟switch 功能性差不多
还有<foreache> 标签 不过我个人还是觉得sqlSessionTemplate进行批处理比较好一点 各位觉得呢?
字段名称 | 字段类型 | 大小 | 说明 |
no | CHAR | 10 | 主键 |
name | VARCHAR2 | 8 | |
sex | CHAR | 2 | 默认值为男,只能输入男或女 |
<if>标签单独使用,如下
<select id="selectSudent" parameterType="String" resultType="java.util.List"> SELECT * FROM student <if test="name!=null and name!=''"> WHERE
name like CONCAT('%',#{name},'%')</if></select>
如果name为空或空串 则进行全部查询,效率会下降
当进行多条件下查询是,<where> <if> 可以提高查询速度 和优化查询语句
<select id="selectSudent" parameterType="String" resultType="java.util.List"> SELECT name,sex FROM student <where> <if test="name!=null and name!=''">
name like CONCAT('%',#{name},'%')</if> <if test="sex!=null and sex!=''"> AND sex=#{sex} </if> </where></select>
<set> <if>
使用
<update id="updateStudent" parameterType="com.frank.Student"> UPDATE student <set> <if test="name!=null and name!=''"> name= #{name}, </if> <if test="sex!=null and sex!=''"> sex=#{sex} </if> </set> WHERE no=#{no} </update>
<trim> <if>
我个人感觉<trim>标签就是<where><set>结合 他同时具有两者的功能
<select id="selectSudent" parameterType="String" resultType="java.util.List"> SELECT name,sex FROM student <trim prefix="WHERE" prefixOverrides="AND|OR"> <if test="name!=null and name!=''"> name like CONCAT('%',#{name},'%') </if> <if test="sex!=null and sex!=''"> AND sex=#{sex} 4000 </if> </trim> </select>
<update id="updateStudent" parameterType="com.frank.Student"> UPDATE student <trim prefix="SET" suffixOverrides=","> <if test="name!=null and name!=''"> name= #{name}, </if> <if test="sex!=null and sex!=''"> sex=#{sex} </if> </trim> WHERE no=#{no} </update>
<choose> 应用场景 多选一 跟switch 功能性差不多
<select id="selectSudent" parameterType="String"> SELECT name,sex FROM student <choose> <when test="name!=null and name!=''"> WHERE name like CONCAT('%',#{name},'%') </when> <when test="sex!=null and sex!=''"> WHERE sex=#{sex} </when> <otherwise> //do something </otherwise> </choose> </select>
<select id="selectSudent" parameterType="String"> SELECT name,sex FROM student <where> <choose> <when test="name!=null and name!=''"> name like CONCAT('%',#{name},'%') </when> <when test="sex!=null and sex!=''"> AND sex=#{sex}//这里我不是太明白为什么要加 AND 有知道的小伙伴告诉我一下 谢谢啦 </when> <otherwise> //do something </otherwise> </choose> </where> </select>
还有<foreache> 标签 不过我个人还是觉得sqlSessionTemplate进行批处理比较好一点 各位觉得呢?
相关文章推荐
- mybatis <where>、<set>、<trim>、<sql>、<foreach>标签的使用
- mybatis中<where>标签、<set>标签、<trim>标签、<sql>标签、<foreach>标签的使用
- Mybatis的<where><foreach><set><trim>等标签详解
- mybatis <where>、<set>、<trim>、<sql>、<foreach>标签的使用
- Mybatis中的三个标签<where> <set> <trim>
- JSP的<c:choose><c:forEach><c:if><c:when><c:set>标签
- Mybatis的<where><trim><set>标签使用和排重
- Mybatis 使用<where>标签时遇到的一个问题与<trim>标签的使用
- 核心标签库<c:set>标签、<c:choose>标签、<c:forEach>标签&<c:if>标签的使用例子
- JSP的<c:choose><c:forEach><c:if><c:when><c:set>标签
- mybatis <where> <set>标签的作用
- mybatis 的<choose> <when>标签和<if test >标签的区别
- mybatis <where>、<set>、<trim>、<sql>、<foreach>标签的使用
- JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
- Mybatis最入门---动态查询( <trim prefix="where" prefixOverrides="and|or"> )
- JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
- Mybatis的<where><foreach><set>等标签详解
- Mybatis的<where><foreach><set>等标签详解
- JSTL标签<c:choose> <c:when> <c:if>
- Mybatis 动态SQL之<trim>,<where>,<set>源码解析