mybatis入门篇(四):mybatis动态SQL
2019独角兽企业重金招聘Python工程师标准>>>
这里提到的动态SQL用法都是基于mapper的xml配置文件的。
1、if 这个标签可以用于多条件查询,也可以用于新增/更新数据时的空值判断。
<select id="selectUser" resultType="com.forest.owl.entity.User">select * from userwhere 1=1<if test="userName != null and userName != '' ">and user_name like concat('%', #{userName}, '%')</if><if test="userPhone != null and userPhone !='' ">and user_phone=#{userPhone}</if>
</select>
2、choose,用于模式匹配
<select id="selectUser" resultType="com.forest.owl.entity.User">select * from userwhere 1=1 /*此处不可忽略*/<choose><when test="id != null">and id=#{id}</when><when test="userName != null and userName !='' ">and user_name=#{userName}</when><otherwise>and 1=2 /*此处不可忽略*/</otherwise></choose>
</select>
3、where,结合if使用
where标签内如果没有符合条件的选项,则最后生成的sql语句不含where;如果有符合条件的,则生成的sql语句会自动去除两端的and
<select id="selectUser" resultType="com.forest.owl.entity.User">select * from user<where><if test="userName != null and userName != '' ">and user_name like concat('%', #{userName}, '%')</if><if test="userEmail != null and userEmail != '' ">and user_email = #{userEmail}</if></where>
</select>
4、set,结合if使用
<update id="updateUserById">update user<set><if test="userName != null and userName != '' ">user_name=#{userName},</if>id=#{id} /*此处不可忽略*/</set>where id=#{id}
</update>
5、foreach
collection:必填,值为要迭代循环的属性名。
item:变量名,值为要从迭代对象中取出的每一个值
index:索引的属性名。在集合数组下为索引值,在Map对象下为Map的key值。
参数为List的情况
<select id="selectByIdList" resultType="com.forest.owl.entity.User">select * from userwhere id in<foreach collection="list" open="(" close=")" separator="," item="id" index="i">#{id}</foreach>
</select>
参数为Map的情况
<update id="updateByMap">update userset<foreach collection="_parameter" item="val" index="key" separator=",">${key}=#{val}</foreach>where id=#{id}</update>
6、bind
bind标签可以使用OGML表达式创建一个变量并绑定到上下文中。如:
<if test="userName != null and userName != '' ">
and user_name like concat('%', #{userName}, '%')
</if>
可以通过bind改写成
<if test="userName != null and userName != '' "><bind name="userNameLike" value=" '%' + userName + '%' "/>and user_name #{userNameLike}
</if>
7、如果测试的时候想知道映射XML中方法执行的参数 可以这么做:
public class StringUtil{public static void print(Object parameter){System.out.println(parameter);}
}
<bind name="print" value="@com.forest.owl.util.StringUtil@print(_parameter)" />
8、鉴别器映射(discriminator) 有时单独一个映射会需要返回不同数据类型的结果集,discriminator就是为了用来处理这种情况。
因为感觉这个标签不会很常用,所以不做进一步了解,暂时给出简单的代码,后续有需要再回来翻阅:
<resultMap id="UserAndRole" extends="BaseResultMap" type="com.forest.owl.entity.User"><discriminator javaType="int" column="enabled"><case value="1" resultMap="resultMap1"/><case value="2" resultMap="resultMap2"/></discriminator></resultMap>
9、既然提到了动态sql,就额外提一下在注解形式下怎么写mapper吧。因为当前做的项目用到了这种方式,就在此mark一下。 举个多条件查询的小例子):
@Select("<script>select id, namefrom user<where><if test='user.id != null'>id = #{user.id}</if><if test='user.name != null'>name like concat('%',#{user.name},'%')</if></where>
</script>")
User selectUser(@Param("user") User user);
转载于:https://my.oschina.net/u/4108765/blog/3059545
mybatis入门篇(四):mybatis动态SQL相关推荐
- mybatis学习笔记四(动态sql)
直接贴图,注解在代码上,其他的配置文件在学习一中就不贴了 1 数据库 2 实体类 package com.home.entity;/*** 此类是: 用户实体类* @author hpc* @2017 ...
- Mybatis的特性详解——动态SQL
Mybatis的特性详解--动态SQL 前言 一.动态sql的元素 1.MyBatis if标签:条件判断 2.MyBatis choose.when和otherwise标签 3.MyBatis wh ...
- FPGA基础入门篇(四) 边沿检测电路
FPGA基础入门篇(四)--边沿检测电路 一.边沿检测 边沿检测,就是检测输入信号,或者FPGA内部逻辑信号的跳变,即上升沿或者下降沿的检测.在检测到所需要的边沿后产生一个高电平的脉冲.这在FPGA电 ...
- mybatis入门(四)之动态SQL
转载自 mybatis 动态SQL 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦. ...
- Mybatis学习日记(四)——动态SQL第一部分
Mybatis的强大特性之一是它的动态SQL,在进行项目开发的时候,我们对数据库的操作不可能全部是定式的,当对数据库的操作根据不同情况发生变化时,就可以用到Mybatis的动态SQL.而Mybatis ...
- 【Mybatis 之应用篇】 4_动态SQL、缓存
文章目录 Mybatis 十二.动态SQL 1.IF 2.choose(when,otherwise) 3.where,set 4.SQL片段 5.Foreach 十三.缓存 (了解) 1.简介 2. ...
- MyBatis之基于XML的动态SQL
先说下我的梦想,大学的时候一直想着是能开店卖胡辣汤,到目前依然还是我的梦想,上周一家出版社联系我问我有没有时间可以合作出书,这也是我的梦想之一,想了想还是放弃了,至少觉得目前不行,毕竟工作还不到五年, ...
- Mybatis Plus 是如何实现动态 SQL 语句的?原理你懂吗?
作者 | 稻草江南 来源 | https://juejin.cn/post/6883081187103866894 Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,那么它是怎么 ...
- Mybatis中输入输出映射和动态Sql
一.输入映射 我们通过配置parameterType的值来指定输入参数的类型,这些类型可以是简单数据类型.POJO.HashMap等数据类型 1.简单类型 2.POJO包装类型 ①这是单表查询的时候传 ...
最新文章
- 如何使用Leangoo敏捷工具做测试管理。
- RabbitMQ系列之【启动过程中遇到问题及解决方案】
- jquery学习手记(2)jQuery对象和DOM对象相互转换
- redis pub/sub 聊天
- ARC下,不显式指定任何属性关键字时,默认的关键字都有哪些
- html5 将资源存于客户端,HTML5离线应用与客户端存储的实现
- boost::movelib::unique_ptr相关的测试程序
- OSGi服务测试助手:ServiceRegistrationRule
- Java_数组练习答案_Java数组练习题带答案.doc
- PRD:「FITLIFE」小程序产品需求文档(用户端)
- JavaScript学习(二十一)—scrollTop属性和scrollLeft属性
- 怀念本科时代----科学入门的导师肖建华老师
- 《原则》瑞达利欧_epub+mobi+azw3
- 理解直推式学习和归纳式学习
- 中国通胀真相:美国向全球转嫁经济调整成本
- 电感 磁珠 对比分析
- linux密码中逗号怎么输入,linux一窜数字后面的逗号怎么去掉?
- 用python写一个魂斗罗
- 计算机应用技术职业生涯规划书5000字,计算机应用职业生涯规划书5000字
- Android Service的启动过程