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的更多相关文章

  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. 4.client、offset、scroll系列

    client.offset.scroll系列 他们的作用主要与计算盒模型.盒子的偏移量和滚动有关 clientTop 内容区域到边框顶部的距离 ,说白了,就是边框的高度 clientLeft 内容区域 ...

  2. c++实验3 链式存储线性表

    1.线性表链式存储结构及基本操作算法实现 (1)单链表存储结构类的定义: #include <iostream> using namespace std; template <cla ...

  3. 多线程DP

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. HTML5本地存储——IndexedDB二:索引

    HTML5本地存储——IndexedDB(二:索引)   在HTML5本地存储——IndexedDB(一:基本使用)中介绍了关于IndexedDB的基本使用方法,很不过瘾,这篇我们来看看indexed ...

  5. java中锁

    s锁的作用是就是保证线程安全,但是从另外成都讲影响了效率: 1 synchronized关键字 这个是虚拟机底层实现的, java中的关键字,内部实现为监视器锁,主要是通过对象监视器在对象头中的字段来 ...

  6. 第九届蓝桥杯大赛个人赛决赛(软件类)真题Java

    更新中.......... 同一年的题解:https://www.cnblogs.com/dgwblog/p/10111903.html   01 结果填空 (满分11分) 标题:年龄问题 s夫人一向 ...

  7. shell (3) 磁盘挂载

    #!/bin/sh disk=`df -h |grep /dev/sd|awk '{print $1}'` echo $disk for d in $disk do echo $d uuid=`blk ...

  8. CAS总结

    n++的问题不能保证原子操作. 因为被编译后拆分成了3个指令,先获取值,然后加一,然后写回内存.把变量声明为volatile,volatile只能保证内存可见性,但是不能保证原子性,在多线程并发下,无 ...

  9. linux普通用户免秘钥登录(xshell工具环境)

    一.xshell生成密钥 1)工具->新建用户密钥生成向导 2)选择密钥类型.密钥长度(默认即可) 3)生成密钥(生成公钥和私钥) 4)为密钥加密,增加密码(可选),建议加上 5)将公钥保存为文 ...

  10. tomcat各文件夹及作用

    1.bin目录:这个文件夹包含的是启动/关闭tomcat的脚本 2.conf目录:主要是用来存放一些Tomcat的配置文件,都是一些.xml部署文件,其中重要的有: server.xml:是Tomca ...