MyBatis-动态sql语句-if用法——MySQL系列学习笔记
select
根据传入的参数来决定:如果用户名不为空,则对用户名进行模糊查询,如果邮箱不为空,则以邮箱为查询条件
<select id="selectByUser" parameterType="com.tgb.mybatis.entity.SysUser" resultType="com.tgb.mybatis.entity.SysUser">selectuser_name "userName",user_password "userPassword",user_info "userInfo",head_img "headImg",create_time "createTime"fromsys_userwhere1=1<if test="userName != null and userName !=''">and user_name like CONCAT('%',#{userName},'%')</if><if test="userEmail != null and userEmail != ''">and user_email =#{userEmail}</if></select>
update
找到id为xxx这条数据,逐个属性判断,如果该属性不为空,则进行更新操作
<update id="updateById">update sys_userSET<if test="userName != null and userName !=''">user_name = #{userName},</if><if test="userPassword != null and userPassword !=''">user_password = #{userPassword},</if><if test="userEmail != null and userEmail !=''">user_email = #{userEmail},</if><if test="userInfo != null and userInfo !=''">user_info = #{userInfo},</if><if test="headImg != null and headImg !=''">head_img = #{headImg},</if><if test="createTime != null and createTime !=''">create_time =#{createTime},</if>id = #{id}whereid = #{id}</update>
insert
<insert id="insert">insert into sys_user(user_name,user_password,<if test="userEmail !=null and userEmail != ''">user_email,</if>user_info,head_img,create_time)VALUES(#{userName},#{userPassword},<if test="userEmail != null and userEmail !=''">user_email = #{userEmail},</if>#{userInfo},#{headImg, jdbcType=BLOB},#{createTime,jdbcType=TIMESTAMP})</insert>
关于where1=1
的应用,参见我的这个博客——>一起去大厂系列】深入理解MySQL中where 1 = 1的用处
MyBatis-动态sql语句-if用法——MySQL系列学习笔记相关推荐
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦 ...
- Mybatis—动态SQL语句与逆向工程
Mybatis动态SQL语句与逆向工程 MyBatis动态SQL语句与逆向工程 1.动态SQL语句 1.1.动态SQL是什么 1.2.动态SQL有什么用 1.3.基于XML的实现 1.3.2.接口文件 ...
- Mybatis动态SQL语句大全
动态 SQL 语句大全 读完这篇文章里你能收获到 Mybatis动态SQL语句大全 Mybatis中如何定义变量 Mybatis中如何提取公共的SQL片段 1.if语句 需求:根据作者名字和博客名字来 ...
- MyBatis——动态SQL语句——if标签和where标签复合使用
功能需求 根据性别和名字查询用户 官方文档 MyBatis--动态 SQL SQL语句 SELECT id, username, birthday, sex, address FROM `user` ...
- Mybatis 动态Sql语句《常用》
MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...
- MyBatis动态sql语句使用
一.MyBatis动态语句分为4种元素: 元素 作用 描述 if 条件判断 单条件判断 choose(when.otherwise) 条件选择,相当Java when 多条件分支判断 where.se ...
- mybatis动态SQL语句
三.动态SQL语句 有些时候,sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.这是我们可以使 ...
- Mybatis 动态sql语句(if标签和where标签)
功能:根据性别和名字查询用户 查询sql语句: SELECT id, username, birthday, sex, address FROM `user` WHERE sex = 1 AND us ...
- 【转】mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
转自:除非申明,文章均为一号门原创,转载请注明本文地址,谢谢! 转载地址:http://blog.csdn.net/kutejava/article/details/9164353#t5 1. if ...
最新文章
- 【哈佛商学院和斯坦福要求学生必看的20部电影】中/英字幕
- mysql 9.0创建数据库_PHP与MySQL学习笔记9:创建Web数据库
- java 将换行代替_Java批量将文件中的段落替换成空格,根据指定分隔符换行(SQL示例)...
- mysql的rowscn_Oracle ORA_ROWSCN 伪列 说明
- 我的教师生涯4:被忘却的第一节课
- 同一个项目的同一DLL多版本的兼容问题
- 后缀树c语言算法,C语言数据结构之中缀树转后缀树的实例
- redis打开外网访问端口
- 数据库(基础SQL)
- 非线性系统离散线性化方法(一)
- Excel 2010 SQL应用052 将英文字母转换为小写字母
- 数据错误(循环冗余检查) 各种解决方法
- SQL语句中case、when、then的使用
- 怎么把pdf文件转换成word方法分享
- 个人QQ号码推广代码
- 史上最全的app相关的面试题【多测师】
- GRE词汇整理(magoosh版本)
- iterm配置alias
- 服务器芯片市场国产,飞腾发布国产腾云 S2500服务器芯片 2020中国服务器行业市场全景调研...
- android网页编辑软件,HTML5编辑器