mybatis的动态sql语句很强大,在mapper映射文件中使用简单的标签即可实现该效果,下面一个个记录:

1、select查询

简单的select类似如下:

  1. <select id="findById" resultMap="StudentResult" parameterType="Integer">
  2. select * from t_student where id = #{id}
  3. </select>

1)if(常用于各种查询的条件判断部分)

  1. <select id="searchStudents" parameterType="Map" resultMap="StudentResult">
  2. select * from t_student
  3. where gradeId = #{gradeId}
  4. <if test="name != null">
  5. and name like #{name}
  6. </if>
  7. <if test="age != null">
  8. and age = #{age}
  9. </if>
  10. </select>

结合where标签使用如下:

  1. <select id="searchStudents3" parameterType="Map" resultMap="StudentResult">
  2. select * from t_student
  3. <where>
  4. <if test="gradeId != null">
  5. gradeId = #{gradeId}
  6. </if>
  7. <if test="name != null">
  8. and name like #{name}
  9. </if>
  10. <if test="age != null">
  11. and age = #{age}
  12. </if>
  13. </where>
  14. </select>

2)choose(同if..else..类似)

  1. <select id="searchStudents2" parameterType="Map" resultMap="StudentResult">
  2. select * from t_student
  3. <choose>
  4. <when test="searchBy=='gradeId'">
  5. where gradeId = #{gradeId}
  6. </when>
  7. <when test="searchBy=='name'">
  8. where name like #{name}
  9. </when>
  10. <otherwise>
  11. where age = #{age}
  12. </otherwise>
  13. </choose>
  14. </select>

3)trim

  1. <select id="searchStudents4" parameterType="Map" resultMap="StudentResult">
  2. select * from t_student
  3. <trim prefix="where" prefixOverrides="and|or">
  4. <if test="gradeId != null">
  5. gradeId = #{gradeId}
  6. </if>
  7. <if test="name != null">
  8. and name like #{name}
  9. </if>
  10. <if test="age != null">
  11. and age = #{age}
  12. </if>
  13. </trim>
  14. </select>

prefix前置,prefixOverrides前置覆盖,简单理解为:trim子句中最前面的and或者or用where替换。

4)foreach

  1. <select id="searchStudents5" parameterType="Map" resultMap="StudentResult">
  2. select * from t_student
  3. <if test="gradeIds != null">
  4. <where>
  5. gradeId in
  6. <foreach collection="gradeIds" item="gradeId" open="("
  7. close=")" separator=",">
  8. #{gradeId}
  9. </foreach>
  10. </where>
  11. </if>
  12. </select>

collections即数组集合,可以是list类型,如arrayList等,open指定左侧拼接方式,close指定右侧,separator指定分隔符,这里拼接后为括号括起来逗号隔开的字符串,从而用于in查询。

2、update

  1. <update id="updateStudent" parameterType="Student">
  2. update t_student
  3. <set>
  4. <if test="name != null">
  5. name = #{name},
  6. </if>
  7. <if test="age != null">
  8. age = #{age} <!-- 自动剔除最后的逗号 -->
  9. </if>
  10. </set>
  11. where id = #{id}
  12. </update>
  1. <update id="update" parameterType="Student">
  2. update t_student set name =
  3. #{name},age = #{age} where id = #{id}
  4. </update>

3、delete

  1. <delete id="delete" parameterType="Integer">
  2. delete from t_student where id = #{id}
  3. </delete>

4、insert

  1. <!-- 插入 -->
  2. <insert id="add" parameterType="Student">
  3. insert into t_student(id,name,age) values(null,#{name},#{age})
  4. </insert>

mybatis学习之动态sql的更多相关文章

  1. mybatis学习 十 动态 SQL

    1.  根据方法传入的参数不同执行不同的 SQL 命令.称为动态 SQL, MyBatis 中动态 SQL 就是在 mapper.xml 中添加逻辑判断等. 2. <if>标签 <s ...

  2. mybatis 学习五 动态SQL语句

    3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...

  3. Mybatis学习笔记-动态SQL

    概念 根据不同环境生成不同SQL语句,摆脱SQL语句拼接的烦恼[doge] 本质:SQL语句的拼接 环境搭建 搭建数据库 CREATE TABLE `blog`( `id` VARCHAR(50) N ...

  4. 【mybatis深度历险系列】mybatis中的动态sql

    最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...

  5. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  6. mybatis 详解------动态SQL

    mybatis 详解------动态SQL   目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...

  7. mybatis中的动态SQL

    在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...

  8. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  9. 【长文】Spring学习笔记(七):Mybatis映射器+动态SQL

    1 概述 本文主要讲述了如何使用MyBatis中的映射器以及动态SQL的配置. 2 MyBatis配置文件概览 MyBatis配置文件主要属性如下: <settings>:相关设置,键值对 ...

随机推荐

  1. CYJian的水题大赛2 解题报告

    这场比赛是前几天洛谷上 暮雪﹃紛紛dalao的个人公开赛,当时基本上都在水暴力分......也没有好好写正解(可能除了T1) 过了几天颓废的日子之后,本蒟蒻觉得应该卓越一下了qwq,所以就打算写一个解 ...

  2. Ionic2文档整理

    来自:Rainey's Blog 原文地址:http://rainey.space/2016/04/06/Ionic2_Chinese_Document/ Github:https://github. ...

  3. iOS核心动画之蒙版

    应用场景 想让一些古卷文字慢慢渐变成背景色,而不是一个突兀的边界 layer的边框不是圆角,而是有星形框架的图片 方法 设置mask属性.这个属性也是一个layer属性,但只有alpha属性有效果.即 ...

  4. numpy之转置(transpose)和轴对换

    转置(transpose)和轴对换 转置可以对数组进行重置,返回的是源数据的视图(不会进行任何复制操作). 转置有三种方式,transpose方法.T属性以及swapaxes方法. 1 .T,适用于一 ...

  5. Kettle 行列互换之——行转列(多列数据合并成一列变为多行)

    原始需求如下: 业务系统设置成这样,见截图. 工资项目为了方便录入,都是做成列的. 但是这些数据需要和另外的费用报销系统的数据关联,费用报销系统的费用项目是横向的,用费用项目.金额的多行来表达.那么这 ...

  6. c++primer 学习笔记

    1.1 编写简单的c++程序 函数4元素:函数类型.函数名.形参表.函数体 调用GNU(UNIX) g++ prog1.cc -o prog1 //生成可执行文件prog1,UNIX下默认a.out ...

  7. QRegExp

    这段代码会越界,百思不得七姐(过了N久时间 之后^^)原来是把i写成了1  --! //#if 0 QRegExp re1("AT+CGATT?"); QRegExp re2(&q ...

  8. 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 ...

  9. vue使用nprogress页面加载进度条

    vue使用nprogress页面加载进度条 NProgress是页面跳转是出现在浏览器顶部的进度条 官网:http://ricostacruz.com/nprogress/ github:https: ...

  10. 构造函数与普通函数关于“new”操作符

    javascript中构造函数与普通函数的区别还有关于“new”操作符的一些原理   有一种创建对象的方法叫做工厂模式,例如: 1 function person(name,age){ 2 var o ...