1.if语句
如果empno不为空,则在WHERE参数后加上AND empno = #{empno},这里有1=1所以即使empno为null,WHERE后面也不会报错。 映射文件 <select id="getEmpById2" resultType="emp">
SELECT * FROM emp WHERE 1=1
<if test="empno != null">
AND empno = #{empno}
</if>
</select>
EmpMapper接口 public Emp getEmpById2(@Param("empno")Integer empno) throws IOException;
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。而使用if标签时,只要test中的表达式为 true,就会执行 if 标签中的条件。MyBatis 提供了 choose 元素。if标签是与(and)的关系,而 choose 是或(or)的关系。 2.where语句和Choose(when,otherwise)
1.Where后面empno和ename为null,那where就不会出现在sql语句中。
2. choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。 映射文件 <select id="getEmpById3" resultType="emp" parameterType="emp">
SELECT * FROM EMP
<where>
<choose>
<when test="empno != null">
AND empno like #{empno}
</when>
<when test="ename != null">
AND ename like #{ename}
</when>
<otherwise>
AND job = "zz"
</otherwise>
</choose>
</where>
</select> EmpMapper接口 public Emp getEmpById3(Emp emp) throws IOException;
3.set语句
set主要也是用来解决更新问题的。
映射文件 <update id="updateEmprById2" parameterType="emp">
UPDATE emp
<set>
<if test="ename!=null"> ename=#{ename},</if>
<if test="job!=null"> job=#{job},</if>
</set>
<where>
<if test="empno!=null">
empno=#{empno};
</if>
</where>
</update>
EmpMapper接口 public Integer updateEmprById2(Emp emp) throws IOException;
4.trim
trim标记是一个格式化的标记,可以完成set或者是where标记的功能。
相关属性:
Prefix:前缀。
prefixOverrides:去掉第一个指定内容。
suffix:后缀。
suffixoverride:去掉最后一个指定内容。
映射文件 <!-- 代替where -->
<select id="getEmpById4" resultType="emp" parameterType="emp">
SELECT * FROM emp
<!-- <where> <if test="username!=null"> and name = #{username} </if> </where> -->
<trim prefix="where" prefixOverrides="AND |OR ">
<if test="empno != null">
and empno = #{empno}
</if>
<if test="ename!=null">
AND ename = #{ename}
</if>
</trim>
</select>
映射文件 <!-- 代替set -->
<update id="updateEmprById3" parameterType="emp">
update emp
<trim prefix="set" suffixOverrides=",">
<if test="ename!=null">
ename = #{ename},
</if>
<if test="job != null">
job = #{job}
</if>
</trim>
<trim prefix="where" prefixOverrides="AND |OR ">
<if test="empno != null">
and empno = #{empno}
</if>
</trim>
</update>
EmpMapper接口 public Emp getEmpById4(Emp emp) throws IOException;
public Integer updateEmprById3(Emp emp) throws IOException;
5.foreach语句
foreach用来遍历,遍历的对象可以是数组,也可以是集合。
相关属性:
Collection:collection属性的值有三个分别是list、array、map三种。
Open:前缀。
Close:后缀。
Separator:分隔符,表示迭代时每个元素之间以什么分隔。
Item:表示在迭代过程中每一个元素的别名。
Index:用一个变量名表示当前循环的索引位置。
映射文件 <insert id="addEmp6">
insert into emp(ename,job)values
<foreach collection="emps" item="emp" separator=",">
(#{emp.ename},#{emp.job})
</foreach>
</insert>
EmpMapper接口 public int addEmp6(@Param("emps")List<Emp> emps);
6.SQL块
映射文件 <!-- 定义重复使用的SQL内容 -->
<sql id="baseSql">
empno,ename,job
</sql> <!-- 使用include引入sql块 -->
<select id="selEmp1" resultType="emp">
select
<include refid="baseSql"/>
from emp
</select>
EmpMapper接口 public List<Emp> selEmp1() throws IOException;
7.bind
映射文件 <select id="getEmpById6" resultType="emp">
<!-- 声明了一个参数empno 在后面就可以使用了 -->
<bind name="empno" value="7975" />
select * from emp where empno=${empno}
</select>
EmpMapper接口 public Emp getEmpById6()throws IOException;
全部代码:
EmpMapper接口 package com.zsl.dao; import java.io.IOException;
import java.util.List; import org.apache.ibatis.annotations.Param; import com.zsl.pojo.Emp; public interface EmpMapper {
public Integer addEmp(Emp emp) throws IOException; public Integer deleteEmpById(Integer empno) throws IOException;
public Integer updateEmprById(Emp emp) throws IOException; public Emp getEmpById(@Param("empno")Integer empno) throws IOException; public Integer addEmp1(String ename,String job) throws IOException;
public Integer addEmp2(String ename,String job) throws IOException; public Integer addEmp3(@Param("ename")String ename,@Param("job")String job) throws IOException; public Integer addEmp4(@Param("ename")String ename,@Param("job")String job) throws IOException; public List<Emp> selEmp() throws IOException; public Emp getEmpById2(@Param("empno")Integer empno) throws IOException; public Emp getEmpById3(Emp emp) throws IOException;
public Integer updateEmprById2(Emp emp) throws IOException;
public Emp getEmpById4(Emp emp) throws IOException;
public Integer updateEmprById3(Emp emp) throws IOException;
// 如果不指定@Param 默认是array
public List<Emp> getEmpById5(@Param("empnos")List<Integer> empno);
public int addEmp6(@Param("emps")List<Emp> emps);
public List<Emp> selEmp1() throws IOException;
public Emp getEmpById6()throws IOException;
}
EmpMapper.XML映射文件 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsl.dao.EmpMapper">
<!-- <insert id="addEmp" parameterType="emp">
insert into emp(ename,job)values(#{ename},#{job})
</insert> -->
<delete id="deleteEmpById" parameterType="int">
delete from emp where empno=#{empno}
</delete>
<update id="updateEmprById" parameterType="emp">
update emp set name = #{ename} where empno=#{empno}
</update> <select id="getEmpById" resultType="emp">
select * from emp where empno=${empno}
</select> <insert id="addEmp1">
insert into emp(ename,job)values(#{arg0},#{arg1})
</insert> <insert id="addEmp2">
insert into emp(ename,job)values(#{param1},#{param2})
</insert> <insert id="addEmp3">
insert into emp(ename,job)values(${ename},${job})
</insert> <insert id="addEmp4">
insert into emp(ename,job)values(#{ename},#{job})
</insert> <select id="selEmp3" resultType="emp">
select empno empno,ename ename,job job,mgr mgrA from emp
</select> <resultMap type="emp" id="baseMap">
<id column="empno" property="empno" />
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="mgrA" column="mgr" />
</resultMap> <select id="selEmp" resultType="emp" resultMap="baseMap">
select * from emp
</select> <!-- useGeneratedKeys:使用生成的主键 keyProperty="id":将生成的主键的值保存到对象的id属性中 -->
<insert id="addEmp" parameterType="emp" useGeneratedKeys="true"
keyProperty="empno">
insert into emp(ename,job)values(#{ename},#{job})
</insert> <insert id="addEmp6" parameterType="emp">
<selectKey keyProperty="empno" resultType="int">
select
LAST_INSERT_ID()
</selectKey>
insert into emp(ename,job)values(#{ename},#{job})
</insert> <select id="getEmpById2" resultType="emp">
SELECT * FROM emp WHERE 1=1
<if test="empno != null">
AND empno = #{empno}
</if>
</select> <select id="getEmpById3" resultType="emp" parameterType="emp">
SELECT * FROM EMP
<where>
<choose>
<when test="empno != null">
AND empno like #{empno}
</when>
<when test="ename != null">
AND ename like #{ename}
</when>
<otherwise>
AND job = "zz"
</otherwise>
</choose>
</where>
</select> <update id="updateEmprById2" parameterType="emp">
UPDATE emp
<set>
<if test="ename!=null"> ename=#{ename},</if>
<if test="job!=null"> job=#{job},</if>
</set>
<where>
<if test="empno!=null">
empno=#{empno};
</if>
</where>
</update> <!-- 代替where -->
<select id="getEmpById4" resultType="emp" parameterType="emp">
SELECT * FROM emp
<!-- <where> <if test="username!=null"> and name = #{username} </if> </where> -->
<trim prefix="where" prefixOverrides="AND |OR ">
<if test="empno != null">
and empno = #{empno}
</if>
<if test="ename!=null">
AND ename = #{ename}
</if>
</trim>
</select> <!-- 代替set -->
<update id="updateEmprById3" parameterType="emp">
update emp
<trim prefix="set" suffixOverrides=",">
<if test="ename!=null">
ename = #{ename},
</if>
<if test="job != null">
job = #{job}
</if>
</trim>
<trim prefix="where" prefixOverrides="AND |OR ">
<if test="empno != null">
and empno = #{empno}
</if>
</trim>
</update> <select id="getEmpById5" resultType="emp">
select * from emp where empno in
<foreach collection="empnos" open="(" close=")" separator=","
item="empno">
#{empno}
</foreach>
</select> <insert id="addEmp6">
insert into emp(ename,job)values
<foreach collection="emps" item="emp" separator=",">
(#{emp.ename},#{emp.job})
</foreach>
</insert> <!-- 定义重复使用的SQL内容 -->
<sql id="baseSql">
empno,ename,job
</sql> <!-- 使用include引入sql块 -->
<select id="selEmp1" resultType="emp">
select
<include refid="baseSql"/>
from emp
</select> <select id="getEmpById6" resultType="emp">
<!-- 声明了一个参数empno 在后面就可以使用了 -->
<bind name="empno" value="7975" />
select * from emp where empno=${empno}
</select>
</mapper>

动态SQL基本语句用法的更多相关文章

  1. 9.mybatis动态SQL标签的用法

    mybatis动态SQL标签的用法   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...

  2. mybatis动态SQL标签的用法

    动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格 ...

  3. 获取动态SQL查询语句返回值(sp_executesql)

    在写存储过程时经常会遇到需要拼接SQL语句的情况,一般情况下仅仅是为了执行拼接后的语句使用exec(@sql)即可. 而今天的一个存储过程却需要获取动态SQL的查询结果. 需求描述:在某表中根据Id值 ...

  4. mybatis动态sql foreach的用法

    <select id="findUserByIds" parameterType="com.pojo.QueryVo" resultType=" ...

  5. [刘阳Java]_MyBatis_动态SQL标签用法_第7讲

    1.MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. 2.MyBatis中用于实现动态SQL的元素主要有 if choose(when,otherwi ...

  6. 一步步学Mybatis-怎么样实现动态SQL查询(6)

    上一章我们已经讲完了关于Mybatis的分页用法,其实MyBatis 还具有的一个强大的特性之一通常是它的动态 SQL 能力. 如果你有使用 JDBC 或其他 相似框架的经验,你就明白要动态的串联 S ...

  7. Mybatis系列全解(八):Mybatis的9大动态SQL标签你知道几个?提前致女神!

    封面:洛小汐 作者:潘潘 2021年,仰望天空,脚踏实地. 这算是春节后首篇 Mybatis 文了~ 跨了个年感觉写了有半个世纪 ... 借着女神节 ヾ(◍°∇°◍)ノ゙ 提前祝男神女神们越靓越富越嗨 ...

  8. mybatis 详解(五)------动态SQL

    前面几篇博客我们通过实例讲解了用mybatis对一张表进行的CRUD操作,但是我们发现写的 SQL 语句都比较简单,如果有比较复杂的业务,我们需要写复杂的 SQL 语句,往往需要拼接,而拼接 SQL ...

  9. 【转载】 mybatis入门系列四之动态SQL

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

随机推荐

  1. JDk8的新特性-流和内部iteration

    JDK8到今天已经出了好几年了  但是在公司能用到新特性的地方还是很少, 去年的时候当时项目老大要求我们用最新的写法来写Java 刚开始看到用stream写出来的代码一脸懵逼,内心就在想  这是Jav ...

  2. idea使用帮助

    IDEA激活码形式,扫码二维码回复 激活码 自提,秒激活,持续更新.回复的是> 激活码 2020.2以上版本的 IDEA 请跳转至该链接:https://t.1yb.co/3ntg 2018.3 ...

  3. JZOJ2020年8月7日提高组反思

    JZOJ2020年8月7日提高组反思 T1 暴力枚举 枚举起点和\(p\) 然后就 过了?! 根据本人不严谨的推算 时间复杂度\(O(\dfrac{n^7}{4})\) 数据太水就过去了QAQ T2 ...

  4. 因为一个Docker问题,我顺手整理从安装到常用命令操作手册

    今天,自己写了一部分业务代码,是常规代码的另外一种方式,不能在公司的服务器上测试,就自己在PC端搭建了一套和公司集群一样的模板,因为公司的业务模块的测试有单独的服务器(这一块还是我很稀罕的),但是,第 ...

  5. Python类知识学习时的部分问题

    Python的富比较方法__eq__和__ne__之间的关联关系分析 Python的富比较方法__le__.ge__之间的关联关系分析 Python的富比较方法__lt.__gt__之间的关联关系分析 ...

  6. PyQt(Python+Qt)学习随笔:model/view架构中的排序和代理模型QSortFilterProxyModel

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 在Model/View体系架构中,有两种方法可以进行排序:选择哪种方法取决于底层模型. 如 ...

  7. django 删除数据库表后重新同步的方法

    python manage.py sqlmigrate your_app_name 0001 把your_app_name换成自己的app名字即可看到框架自动生成的创建表sql语句,于是我就这样看到了 ...

  8. 团队作业4-Day2

    团队作业4-Day2 项目git地址 1. 站立式会议 2. 项目燃尽图 3. 适当的项目截图(部分) 4. 代码/文档签入记录(部分) 5. 每人每日总结 吴梓华:今日进行了小程序与网页代码编写的区 ...

  9. 深入解析ConcurrentHashMap:感受并发编程智慧

    如果有一个整型变量count,多个线程并发让count自增1,你会怎么设计? 你知道如何让多个线程协作完成一件事件吗? 前言 很高兴遇见你~ ConcurrentHashMap是个老生常谈的集合类了, ...

  10. 题解-CmdOI2019 口头禅

    题面 CmdOI2019 口头禅 给 \(n\) 个 \(01\) 串 \(s_i\),\(m\) 个询问问 \(s_{l\sim r}\) 的最长公共子串长度. 数据范围:\(1\le n\le 2 ...