mybatis学习之动态sql
mybatis的动态sql语句很强大,在mapper映射文件中使用简单的标签即可实现该效果,下面一个个记录:
1、select查询
简单的select类似如下:
- <select id="findById" resultMap="StudentResult" parameterType="Integer">
- select * from t_student where id = #{id}
- </select>
1)if(常用于各种查询的条件判断部分)
- <select id="searchStudents" parameterType="Map" resultMap="StudentResult">
- select * from t_student
- where gradeId = #{gradeId}
- <if test="name != null">
- and name like #{name}
- </if>
- <if test="age != null">
- and age = #{age}
- </if>
- </select>
结合where标签使用如下:
- <select id="searchStudents3" parameterType="Map" resultMap="StudentResult">
- select * from t_student
- <where>
- <if test="gradeId != null">
- gradeId = #{gradeId}
- </if>
- <if test="name != null">
- and name like #{name}
- </if>
- <if test="age != null">
- and age = #{age}
- </if>
- </where>
- </select>
2)choose(同if..else..类似)
- <select id="searchStudents2" parameterType="Map" resultMap="StudentResult">
- select * from t_student
- <choose>
- <when test="searchBy=='gradeId'">
- where gradeId = #{gradeId}
- </when>
- <when test="searchBy=='name'">
- where name like #{name}
- </when>
- <otherwise>
- where age = #{age}
- </otherwise>
- </choose>
- </select>
3)trim
- <select id="searchStudents4" parameterType="Map" resultMap="StudentResult">
- select * from t_student
- <trim prefix="where" prefixOverrides="and|or">
- <if test="gradeId != null">
- gradeId = #{gradeId}
- </if>
- <if test="name != null">
- and name like #{name}
- </if>
- <if test="age != null">
- and age = #{age}
- </if>
- </trim>
- </select>
prefix前置,prefixOverrides前置覆盖,简单理解为:trim子句中最前面的and或者or用where替换。
4)foreach
- <select id="searchStudents5" parameterType="Map" resultMap="StudentResult">
- select * from t_student
- <if test="gradeIds != null">
- <where>
- gradeId in
- <foreach collection="gradeIds" item="gradeId" open="("
- close=")" separator=",">
- #{gradeId}
- </foreach>
- </where>
- </if>
- </select>
collections即数组集合,可以是list类型,如arrayList等,open指定左侧拼接方式,close指定右侧,separator指定分隔符,这里拼接后为括号括起来逗号隔开的字符串,从而用于in查询。
2、update
- <update id="updateStudent" parameterType="Student">
- update t_student
- <set>
- <if test="name != null">
- name = #{name},
- </if>
- <if test="age != null">
- age = #{age} <!-- 自动剔除最后的逗号 -->
- </if>
- </set>
- where id = #{id}
- </update>
- <update id="update" parameterType="Student">
- update t_student set name =
- #{name},age = #{age} where id = #{id}
- </update>
3、delete
- <delete id="delete" parameterType="Integer">
- delete from t_student where id = #{id}
- </delete>
4、insert
- <!-- 插入 -->
- <insert id="add" parameterType="Student">
- insert into t_student(id,name,age) values(null,#{name},#{age})
- </insert>
mybatis学习之动态sql的更多相关文章
- mybatis学习 十 动态 SQL
1. 根据方法传入的参数不同执行不同的 SQL 命令.称为动态 SQL, MyBatis 中动态 SQL 就是在 mapper.xml 中添加逻辑判断等. 2. <if>标签 <s ...
- mybatis 学习五 动态SQL语句
3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...
- Mybatis学习笔记-动态SQL
概念 根据不同环境生成不同SQL语句,摆脱SQL语句拼接的烦恼[doge] 本质:SQL语句的拼接 环境搭建 搭建数据库 CREATE TABLE `blog`( `id` VARCHAR(50) N ...
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- Mybatis入门之动态sql
Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...
- mybatis 详解------动态SQL
mybatis 详解------动态SQL 目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...
- mybatis中的动态SQL
在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...
- Mybatis映射文件动态SQL语句-01
因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...
- 【长文】Spring学习笔记(七):Mybatis映射器+动态SQL
1 概述 本文主要讲述了如何使用MyBatis中的映射器以及动态SQL的配置. 2 MyBatis配置文件概览 MyBatis配置文件主要属性如下: <settings>:相关设置,键值对 ...
随机推荐
- CYJian的水题大赛2 解题报告
这场比赛是前几天洛谷上 暮雪﹃紛紛dalao的个人公开赛,当时基本上都在水暴力分......也没有好好写正解(可能除了T1) 过了几天颓废的日子之后,本蒟蒻觉得应该卓越一下了qwq,所以就打算写一个解 ...
- Ionic2文档整理
来自:Rainey's Blog 原文地址:http://rainey.space/2016/04/06/Ionic2_Chinese_Document/ Github:https://github. ...
- iOS核心动画之蒙版
应用场景 想让一些古卷文字慢慢渐变成背景色,而不是一个突兀的边界 layer的边框不是圆角,而是有星形框架的图片 方法 设置mask属性.这个属性也是一个layer属性,但只有alpha属性有效果.即 ...
- numpy之转置(transpose)和轴对换
转置(transpose)和轴对换 转置可以对数组进行重置,返回的是源数据的视图(不会进行任何复制操作). 转置有三种方式,transpose方法.T属性以及swapaxes方法. 1 .T,适用于一 ...
- Kettle 行列互换之——行转列(多列数据合并成一列变为多行)
原始需求如下: 业务系统设置成这样,见截图. 工资项目为了方便录入,都是做成列的. 但是这些数据需要和另外的费用报销系统的数据关联,费用报销系统的费用项目是横向的,用费用项目.金额的多行来表达.那么这 ...
- c++primer 学习笔记
1.1 编写简单的c++程序 函数4元素:函数类型.函数名.形参表.函数体 调用GNU(UNIX) g++ prog1.cc -o prog1 //生成可执行文件prog1,UNIX下默认a.out ...
- QRegExp
这段代码会越界,百思不得七姐(过了N久时间 之后^^)原来是把i写成了1 --! //#if 0 QRegExp re1("AT+CGATT?"); QRegExp re2(&q ...
- PIE SDK 文章目录索引
1.PIE SDK介绍 1.1. PIE软件介绍 1.2. PIE SDK介绍 1.3. PIE支持项目介绍 1.4. PIE.NET-SDK插件式二次开发介绍 1.5. PIE.NET-S ...
- vue使用nprogress页面加载进度条
vue使用nprogress页面加载进度条 NProgress是页面跳转是出现在浏览器顶部的进度条 官网:http://ricostacruz.com/nprogress/ github:https: ...
- 构造函数与普通函数关于“new”操作符
javascript中构造函数与普通函数的区别还有关于“new”操作符的一些原理 有一种创建对象的方法叫做工厂模式,例如: 1 function person(name,age){ 2 var o ...