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. oracle数据库中将clob字段内容利用java提取出至文本文档中

    代码段: 1.执行clob转String public static String ClobToString(Clob sc) throws SQLException, IOException { S ...

  2. 2. Python的划分

    解释型:当程序运行时,将代码从上至下,一句一句解释成二进制,在执行. 典型:python,php 优点:开发速度快,可以跨平台. 缺点:执行效率慢 编译型:将源码一次性转化成二进制文件,然后在执行. ...

  3. ocp题库更新,052最新考试题及答案整理-31

    31.Which two events always request the LGWR to write? A) when LGWR is notified by a server process t ...

  4. 使用concat做字符串拼接和数据迁移

    作用: 解决一开始数据库建立不合理造成的字段冗余,从而提取部分字段,数据迁移.拼接字符串的功能. 格式: concat(字段1,'间隔符',字段2....) concat_ws('间隔符',字段1,字 ...

  5. [ActionScript 3.0] 利用InteractivePNG.as类精确选择识别png图片有像素的区域

    用法:如果是把png直接导入flash转换成影片剪辑,只需在影片剪辑属性中勾选为ActionScript导出(x),并把基类里的flash.display.MovieClip替换成Interactiv ...

  6. iOS 设备定位功能可用的判断

    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] ...

  7. 使用私有git仓库备份服务器脚本和配置文件

    1. 创建私有git仓库 服务器端配置: # 安装 git yum -y install git # 创建 git 用户 useradd git # 创建私有仓库数据存储目录 mkdir /git_b ...

  8. nginx如何设置禁止访问文件或文件夹

    目标: 1. 根目录 webroot: 2. 设置目标文件 /webroot/proj/deny.txt 不能访问: 做法: 1. 设置 nginx.conf ,添加一个“location”段落: 2 ...

  9. Oracle WebLogic Server 12c 新特性

    美国时间2011年 12月9日,Oracle公司正式发布WebLogic 12c版本,c是cloud的缩写.截止当前(2013年8月)最新版本为Oracle WebLogic Server 12c ( ...

  10. php尝试调用一个图灵机器人

    1.需要到图灵机器人的网址,去注册一下账号.网址:http://www.tuling123.com/sso-web/index.html?ReturnURL=http%3A%2F%2Fwww.tuli ...