Mybatis <where> <if> <set> <trim> <choose>标签
来源:互联网 发布:阿里云负责人 编辑:程序博客网 时间:2024/05/29 21:29
默认表格 student
字段名称
字段类型
大小
说明
no
CHAR
10
主键
name
VARCHAR2
8
sex
CHAR
2
默认值为男,只能输入男或女
<where> <if>
<if>标签单独使用,如下
<select id="selectSudent" parameterType="String" resultType="java.util.List"> SELECT * FROM student <if test="name!=null and name!=''"> WHEREname 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} </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>
<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进行批处理比较好一点 各位觉得呢?
阅读全文
1 0
- Mybatis <where> <if> <set> <trim> <choose>标签
- mybatis动态sql查询Dynamic SQL之if,foreach,choose,trim,where,set
- MyBatis-动态SQL的if、choose、when、otherwise、trim、where、set、foreach使用
- Mybatis动态SQL——if、choose、where、set、trim、foreach标记实例
- MyBatis-动态SQL的if、choose、when、otherwise、trim、where、set、foreach使用
- Mybatis动态SQL之if、choose、where、set、trim、foreach实例
- mybatis-动态sql-if,choose,when,otherwis,trim,where,set,foreach,test,bind
- MySQL动态语句 if set choose where foreach trim
- Mybatis中的三个标签<where> <set> <trim>
- Mybatis动态标签--trim,where,set
- Mybatis的<where><trim><set>标签使用和排重
- Mybatis的<where><foreach><set><trim>等标签详解
- mybatis中<where>标签、<set>标签、<trim>标签、<sql>标签、<foreach>标签的使用
- mybatis where,foreach,choose when ,set
- Mybatis的<where><foreach><set><trim>详解
- MyBatis动态查询(where trim set)
- Mybatis的<where><if>标签
- mybatis的动态SQL(三)where、set、trim标签的使用
- char 与 unsigned char
- 新手起家之梦想飞航
- 十大算法
- 学习前辈系列:根据需求找代码
- Java中的注解详解
- Mybatis <where> <if> <set> <trim> <choose>标签
- 中医微信预约系统,你值得拥有!!!
- Ubuntu mate update 失败
- React DOM Diff
- java 大数取模(有可运行代码和详细注解)
- Android shape属性详解(自定义图片和形状)
- 利用java反射实现任意数组copy,reflect.Array
- Solr(一)——搭建solr全文搜索服务
- 这是一篇正经的博文