MyBatis动态sql详解(一)
来源:互联网 发布:云计算都招聘什么岗位 编辑:程序博客网 时间:2024/06/05 18:36
MyBatis动态SQL非常简单,即是根据条件动态的变化sql语句,就不用自己去拼写了,MyBatis中用于实现动态SQL的元素主要有:
if
choose(when,otherwise)
trim
where
set
foreach
1、if判断语句:
可以看出,我前面已经加上了where语句,那么在后面,只要传入的user的id不为空,就加上id判断语句,只要name不为空,就加上name判断语句,非常方便。
2、where+if判断语句:
如果我想全部在后面加上where的判断,那么只需要写在where嵌套里面即可,MyBatis会智能的把首个and 或 or 给忽略,而后面的and、or都不会忽略。
比如下面这条语句,如果把name中的改为or,那么当输入的user的id不为空,name也不为空时,那么sql语句为
select * from users where id = 7 or name = 'happyheng'
3、choose(when,otherwise) 语句:
choose语句跟java中的switch语句相同,when相当于case,when中一个判断语句,只要判断语句成立,那么就跳出choose语句,如果所有的choose都不成立,那么就执行otherwise中的sql。
为了方便,在前面加上了where 1=1,这样1=1肯定成立,也加上了where:
那么,当传入的id为空,name为zhangsan时,执行的是
select * from users where 1=1 and name = 'zhangsan'
当传入的id为空,name也为空时,执行的是
select * from users where 1=1 and name = 'happyheng'
4、trim语句:
trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容忽略,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能,
这样,当我们传入的id不为空时,那么sql语句即为:
select * from users where id = 7;
5、set语句:
set语句主要是在更新时进行使用,其会自动把最后一个逗号给删除,比如我想更新一个user,把user的id为7的name改为libenben,但是其age我不想改,那么就可以这样:
那么我这样使用即可:
if
choose(when,otherwise)
trim
where
set
foreach
1、if判断语句:
if判断语句即是当满足条件时,sql将会加上此条语句,比如我想根据传入的user动态的添加判断语句:
<select id="dynamicIfGetUser" parameterType="com.happyheng.entity.User" resultType="com.happyheng.entity.User"> select * from users where age > 0 <if test="id!=null"> and id = #{id} </if> <if test="name!=null"> and name = #{name} </if> </select>
可以看出,我前面已经加上了where语句,那么在后面,只要传入的user的id不为空,就加上id判断语句,只要name不为空,就加上name判断语句,非常方便。
2、where+if判断语句:
如果我想全部在后面加上where的判断,那么只需要写在where嵌套里面即可,MyBatis会智能的把首个and 或 or 给忽略,而后面的and、or都不会忽略。
<select id="dynamicWhereIfGetUser" parameterType="com.happyheng.entity.User" resultType="com.happyheng.entity.User"> select * from users <where> <if test="id!=null"> and id = #{id} </if> <if test="name!=null"> and name = #{name} </if> <if test="age!=null"> and age = #{age} </if> </where> </select>
比如下面这条语句,如果把name中的改为or,那么当输入的user的id不为空,name也不为空时,那么sql语句为
select * from users where id = 7 or name = 'happyheng'
<select id="dynamicWhereIfGetUser" parameterType="com.happyheng.entity.User" resultType="com.happyheng.entity.User"> select * from users <where> <if test="id!=null"> and id = #{id} </if> <if test="name!=null"> or name = #{name} </if> <if test="age!=null"> and age = #{age} </if> </where> </select>
3、choose(when,otherwise) 语句:
choose语句跟java中的switch语句相同,when相当于case,when中一个判断语句,只要判断语句成立,那么就跳出choose语句,如果所有的choose都不成立,那么就执行otherwise中的sql。
为了方便,在前面加上了where 1=1,这样1=1肯定成立,也加上了where:
<select id="dynamicChooseGetUser" parameterType="com.happyheng.entity.User" resultType="com.happyheng.entity.User"> select * from users where 1=1 <choose> <when test="id!=null"> and id = #{id} </when> <when test="name!=null"> and name = #{name} </when> <otherwise> and name = 'happyheng' </otherwise> </choose> </select>
那么,当传入的id为空,name为zhangsan时,执行的是
select * from users where 1=1 and name = 'zhangsan'
当传入的id为空,name也为空时,执行的是
select * from users where 1=1 and name = 'happyheng'
4、trim语句:
trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容忽略,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能,
<select id="dynamicTrimGetUser" parameterType="com.happyheng.entity.User" resultType="com.happyheng.entity.User"> select * from users <trim prefix="where" prefixOverrides="and|or"> <if test="id!=null"> and id = #{id} </if> <if test="name!=null"> or name = #{name} </if> <if test="age!=null"> and age = #{age} </if> </trim> </select>
这样,当我们传入的id不为空时,那么sql语句即为:
select * from users where id = 7;
5、set语句:
set语句主要是在更新时进行使用,其会自动把最后一个逗号给删除,比如我想更新一个user,把user的id为7的name改为libenben,但是其age我不想改,那么就可以这样:
<update id="dynamicSetUser" parameterType="com.happyheng.entity.User"> update users <set> <if test="name!=null"> name = #{name}, </if> <if test="age!=null"> age = #{age}, </if> </set> where id = #{id} </update>
那么我这样使用即可:
SqlSession session = MyBatisUtil.getSqlSession(true); User user = new User(); user.setId(7l); user.setName("libenben"); List<User> users = session.selectList("dynamicSetUser",user); for(User getUser: users){ System.out.println("结果为"+getUser); } session.close();
0 0
- MyBatis动态sql详解(一)
- MyBatis动态SQL详解
- Mybatis动态sql详解
- MyBatis动态SQL详解
- MyBatis动态SQL详解
- MyBatis动态SQL详解
- mybatis 动态sql详解
- Mybatis 动态SQL详解
- MyBatis动态sql详解
- MyBatis 动态SQL 详解
- mybatis动态Sql详解
- Mybatis(八)-一动态SQL
- MyBatis的动态SQL详解
- MyBatis的动态SQL详解
- MyBatis的动态SQL详解
- MyBatis的动态SQL详解
- MyBatis的动态SQL详解
- MyBatis的动态SQL详解
- C++ 让人迷惑的复制构造函数和赋值运算符的区别
- Tarjan求割点——HDU 4587
- Permission is only granted to system apps
- singleton
- 七步精通Python机器学习
- MyBatis动态sql详解(一)
- 设计模式原则—开闭原则(OCP)
- 读书笔记之适配器模式
- 二叉树的后序遍历--非递归实现
- java Stringbuffer的使用
- 100003. Tree
- CodeForces
- C#中DllImport用法汇总
- hdu2852 KiKi's K-Number 线段树