mybatis动态sql查询Dynamic SQL之if,foreach,choose,trim,where,set
来源:互联网 发布:软件shop floor editor 编辑:程序博客网 时间:2024/04/29 13:47
由于实际开发中,经常要用到拼动态sql进行查询,实际写的时候要用很多判断,是很麻烦的,有了mbatis,它可以帮我们很好的解决这个问题。
if用法
<select id="<span style="color:#FF0000;">getUserList</span>" parameterType="map" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id -->from userinfo where 1=1<if test="username!=null">and name like '%'||#{username}||'%' //这里仅对oracle数据库拼接有效,其他没试过</if></select>public List<User> <span style="color:#FF0000;">getUserList</span>(Map<String, Object> map);//调用的接口private static void printUsers(SqlSession sqlSession, UserDao userDao) { Map<String, Object> map = new HashMap<String, Object>(); map.put("username", "张");//如果注释掉,会查询到所有结果 List<User> users = userDao.getUserList(map); for (User user : users) { System.out.println(user.getId() + ":" + user.getName()); } }//测试类,
choose用法:
<select id="getUserList" parameterType="map" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id -->from userinfo where 1=1<choose><when test="username!=null">and name like '%'||#{username}||'%'</when><otherwise>and name ='张三' //如果username为null,则查张三这个条件的,只会执行其中一个分支,与jsp中的c:choose用法类似</otherwise></choose></select>
where用法:
<select id="getUserList" parameterType="map" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id -->from userinfo<where> <!-- where 会自动把最后结果(如果前面有and ,or)的最前面and or,给去掉 --><if test="1==2"> 1=2</if><if test="1==1"> and 1=1</if> <if test="username!=null"> and name like '%'||#{username}||'%' </if></where></select>
trim用法:
<select id="getUserList" parameterType="map" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id -->from userinfo<trim prefix="where" <span style="color:#FF0000;">prefix</span>Overrides="AND |OR "> <!-- where 会自动把最后结果(如果前面(<span style="color:#FF0000;">prefix</span>)有and ,or)的最前面and or,给去掉 --><if test="1==2"> 1=2</if><if test="1==1"> and 1=1</if> <if test="username!=null"> and name like '%'||#{username}||'%' </if></trim></select>
这里的trim相当于where element作用了,可以去and 和or,如果把上面的and去掉,会报下面错
### SQL: select id,name from userinfo where <span style="color:#FF0000;">and</span> 1=1 and name like '%'||?||'%'### Cause: java.sql.SQLSyntaxErrorException: ORA-00936: 缺失表达式
从上面可以看出,where 后面的and 没有被截去
把上面的or去掉也会报下面的错,
### SQL: select id,name from userinfo where <span style="color:#FF0000;">or</span> 1=1 or name like '%'||?||'%'### Cause: java.sql.SQLSyntaxErrorException: ORA-00936: 缺失表达式
set用法:
<update id="modifyuser" parameterType="user"><!-- sql的类型可以是 statement,preparement,callable,默认是preparement-->update userinfo <set> <!-- 会自动去掉最后面的逗号(,) --><if test="name!=null"> name=#{name},</if><if test="1==2"><!-- 为false不添加--> 1=1</if></set> where id=#{id}</update>
上面也可以用trim来代替,如下:
<update id="modifyuser" parameterType="user"><!-- sql的类型可以是 statement,preparement,callable,默认是preparement-->update userinfo <trim prefix="set" <span style="color:#FF0000;">suffix</span>Overrides=","> <!-- 会自动去掉最后面(suffix,注意它与where中的区别)的逗号(,) --><if test="name!=null"> name=#{name},</if><if test="1==2"><!-- 为false不添加--> 1=1</if></trim> where id=#{id}</update>
foreach用法:
根据它的语义就应该要循环,那么什么情况下会用到多次循环呢,可以想到是in查询了
<select id="getUserList" resultMap="getUserLists">select<include refid="useridNames" /><!-- 引用sql id --> <!-- 传的参数可以是list或者array -->from userinfo where id in<foreach item="item" index="index" collection="list" open="(" <span style="color:#FF0000;">list可以是array</span>,像下面String数组那样传,注意array大小是固定的,list不定separator="," close=")"> #{item}</foreach></select> public List<User> getUserList(List list); //调用的接口测试方法: private static void printUsers(SqlSession sqlSession, UserDao userDao) { List list = new ArrayList(); <span style="color:#FF0000;">String [] list=new String[3]; list[0]="8"; list[1]="9"; list[2]="21";</span> list.add("8"); list.add("9"); list.add("43"); list.add("21"); List<User> users = userDao.getUserList(list); for (User user : users) { System.out.println(user.getId() + ":" + user.getName()); } }执行结果:43:张四21:张五9:98:8
bind用法:
<select id="getUserList" resultMap="getUserLists"><bind name="var" value="'%'+name+'%'"/> <!-- 定义一个变量 -->select<include refid="useridNames" /><!-- 引用sql id --> <!-- 传的参数可以是list或者array -->from userinfo where name like #{var} <!-- 引用上面定义的变量 --></select>
自定义主键
<insert id="insert"><selectKey keyProperty="id" resultType="int" order="BEFORE">// before代表之前执行select seq_users.nextval from dual</selectKey>insert into users values (#{id}, #{name})</insert>
0 0
- mybatis动态sql查询Dynamic SQL之if,foreach,choose,trim,where,set
- 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、otherwise、trim、where、set、foreach使用
- mybatis-动态sql-if,choose,when,otherwis,trim,where,set,foreach,test,bind
- MySQL动态语句 if set choose where foreach trim
- Mybatis的动态sql----where,trim,set,foreach
- Mybatis的动态sql----where,trim,set,foreach
- Mybatis <where> <if> <set> <trim> <choose>标签
- where set trim sql if foreach 实现xml方式的动态sql
- Mybatis 动态SQL之<trim>,<where>,<set>源码解析
- Mybatis中动态SQL(if,where,foreach 的使用)
- Mybatis中动态SQL(if,where,foreach 的使用)
- MyBatis动态查询(where trim set)
- mybatis中<where>标签、<set>标签、<trim>标签、<sql>标签、<foreach>标签的使用
- mybatis中动态sql标签之if trim
- mybatis where,foreach,choose when ,set
- android按键的三种响应方式
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
- uva 514 Rails
- printf与scanf的返回值、注意点
- lua学习笔记15:table数组逆序
- mybatis动态sql查询Dynamic SQL之if,foreach,choose,trim,where,set
- 《GNU make项目管理》笔记--变量与宏(1)
- MFC 字体
- uva3027 ADT
- ViewPage
- 常见面试之机器学习算法思想简单梳理
- 移动Web程序的开发平台 Worklight
- STL常用容器成员函数列表
- codeforces 478D Red-Green Towers (dp)