1. 定义sql语句

select 标签 

属性介绍:

  • id :唯一的标识符.
  • parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user
  • resultType :语句返回值类型或别名。注意,如果是集合,那么这里填写的是集合的泛型,而不是集合本身(resultType 与resultMap 不能并用)
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object">
select * from student where id=#{id}
</select>

insert标签

属性介绍:

  • id :唯一的标识符
  • parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User  
<insert id="insert" parameterType="Object">
insert into student
  <trim prefix="("suffix=")" suffixOverrides="," >
  <if test="name != null "> NAME, </if>
</trim>
  <trim prefix="values("suffix=")" suffixOverrides="," >
  <if test="name != null "> #{name}, </if>
</trim>
</insert>

delete标签

属性同 insert  

<delete id="deleteByPrimaryKey" parameterType="Object">
delete from student where id=#{id}
</delete>

update标签

属性同 insert

2. 配置JAVA对象属性与查询结果集中列名对应关系

resultMap 标签的使用
基本作用:

  • 建立SQL查询结果字段与实体属性的映射关系信息
  • 查询的结果集转换为java对象,方便进一步操作。
  • 将结果集中的列与java对象中的属性对应起来并将值填充进去

!注意:与java对象对应的列不是数据库中表的列名,而是查询后结果集的列名

<resultMap id="BaseResultMap" type="com.online.charge.platform.student.model.Student">
<id property="id" column="id" />
<result column="NAME" property="name" />
<result column="HOBBY" property="hobby" />
<result column="MAJOR" property="major" />
<result column="BIRTHDAY" property="birthday" />
<result column="AGE" property="age" />
</resultMap> <!--查询时resultMap引用该resultMap -->
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object">
select id,name,hobby,major,birthday,age from student where id=#{id}
</select>

标签说明:

主标签:

  • id:该resultMap的标志
  • type:返回值的类名,此例中返回Studnet类

子标签:

  • id:用于设置主键字段与领域模型属性的映射关系,此处主键为ID,对应id。
  • result:用于设置普通字段与领域模型属性的映射关系

3. 动态sql拼接

if 标签

if标签通常用于WHERE语句、UPDATE语句、INSERT语句中,通过判断参数值来决定是否使用某个查询条件、判断是否更新某一个字段、判断是否插入某个字段的值。  

<if test="name != null and name != ''">
and NAME = #{name}
</if>

foreach 标签

foreach标签主要用于构建in条件,可在sql中对集合进行迭代。也常用到批量删除、添加等操作中。

    <!-- in查询所有,不分页 -->
<select id="selectIn" resultMap="BaseResultMap">
select name,hobby from student where id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>

属性介绍:

  • collection:collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合。
  • item :表示在迭代过程中每一个元素的别名
  • index :表示在迭代过程中每次迭代到的位置(下标)
  • open :前缀
  • close :后缀
  • separator :分隔符,表示迭代时每个元素之间以什么分隔

choose标签

  有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。

if是与(and)的关系,而choose是或(or)的关系。  

<select id="getStudentListChoose" parameterType="Student" resultMap="BaseResultMap">
SELECT * from STUDENT WHERE 1=1
<where>
<choose>
<when test="Name!=null and student!='' ">
AND name LIKE CONCAT(CONCAT('%', #{student}),'%')
</when>
<when test="hobby!= null and hobby!= '' ">
AND hobby = #{hobby}
</when>
<otherwise>
AND AGE = 15
</otherwise>
</choose>
</where>
</select>
 

4. 格式化输出

where标签

当if标签较多时,这样的组合可能会导致错误。 如下:

<select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap">
SELECT * from STUDENT
WHERE
<if test="name!=null and name!='' ">
NAME LIKE CONCAT(CONCAT('%', #{name}),'%')
</if>
<if test="hobby!= null and hobby!= '' ">
AND hobby = #{hobby}
</if>
</select>

当name值为null时,查询语句会出现 “WHERE AND” 的情况,解决该情况除了将"WHERE"改为“WHERE 1=1”之外,还可以利用where标签。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

<select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap">
SELECT * from STUDENT
<where>
<if test="name!=null and name!='' ">
NAME LIKE CONCAT(CONCAT('%', #{name}),'%')
</if>
<if test="hobby!= null and hobby!= '' ">
AND hobby = #{hobby}
</if>
</where>
</select>

  

set 标签

没有使用if标签时,如果有一个参数为null,都会导致错误。当在update语句中使用if标签时,如果最后的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置set关键字,和剔除追加到条件末尾的任何不相关的逗号。  

<update id="updateStudent" parameterType="Object">
UPDATE STUDENT
SET NAME = #{name},
MAJOR = #{major},
HOBBY = #{hobby}
WHERE ID = #{id};
</update> <update id="updateStudent" parameterType="Object">
UPDATE STUDENT SET
<if test="name!=null and name!='' ">
NAME = #{name},
</if>
<if test="hobby!=null and hobby!='' ">
MAJOR = #{major},
</if>
<if test="hobby!=null and hobby!='' ">
HOBBY = #{hobby}
</if>
WHERE ID = #{id};
</update>

使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值。  

<update id="updateStudent" parameterType="Object">
UPDATE STUDENT
<set>
<if test="name!=null and name!='' ">
NAME = #{name},
</if>
<if test="hobby!=null and hobby!='' ">
MAJOR = #{major},
</if>
<if test="hobby!=null and hobby!='' ">
HOBBY = #{hobby}
</if>
</set>
WHERE ID = #{id};
</update>

trim标签

格式化输出,也可以通过trim标签设定或忽略前后缀来实现。

5. 配置关联关系

5.1 collection标签

5.2 association标签

6. 定义常量及引用

sql标签

当多种类型的查询语句的查询字段或者查询条件相同时,可以将其定义为常量,方便调用。为求<select>结构清晰也可将sql语句分解。  

<!-- 查询字段 -->
<sql id="Base_Column_List">
ID,MAJOR,BIRTHDAY,AGE,NAME,HOBBY
</sql> <!-- 查询条件 -->
<sql id="Example_Where_Clause">
where 1=1
<trim suffixOverrides=",">
<if test="id != null and id !=''">
and id = #{id}
</if>
<if test="major != null and major != ''">
and MAJOR = #{major}
</if>
<if test="birthday != null ">
and BIRTHDAY = #{birthday}
</if>
<if test="age != null ">
and AGE = #{age}
</if>
<if test="name != null and name != ''">
and NAME = #{name}
</if>
<if test="hobby != null and hobby != ''">
and HOBBY = #{hobby}
</if>
<if test="sorting != null">
order by #{sorting}
</if>
<if test="sort!= null and sort != '' ">
order by ${sort} ${order}
</if> </trim>
</sql>

include标签

用于引用定义的常量  

<!-- 查询所有,不分页 -->
<select id="selectAll" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM
student
<include refid="Example_Where_Clause" />
</select> <!-- 分页查询 -->
<select id="select" resultMap="BaseResultMap"> select * from (
select tt.*,rownum as rowno from
(
SELECT
<include refid="Base_Column_List" />
FROM
student
<include refid="Example_Where_Clause" />
) tt
<where>
<if test="pageNum != null and rows != null">
and rownum <![CDATA[<=]]>#{page}*#{rows}
</if>
</where>
) table_alias
where table_alias.rowno>#{pageNum} </select> <!-- 根据条件删除 -->
<delete id="deleteByEntity" parameterType="java.util.Map">
DELETE FROM student
<include refid="Example_Where_Clause" />
</delete>


原文地址:https://blog.csdn.net/m0_38054145/article/details/81906343

mybatis常用标签(转)的更多相关文章

  1. MyBatis - 常用标签与动态Sql

    MyBatis常用标签 ● 定义sql语句:select.insert.delete.update ● 配置JAVA对象属性与查询结构及中列明对应的关系:resultMap ● 控制动态sql拼接:i ...

  2. Mybatis 常用标签

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

  3. [Mybatis]Mybatis 常用标签及功能整理

    Mybatis中生成动态SQL的标签有四类,分别是: if choose (when, otherwise) trim (where, set) foreach 1.if 当需要动态生成where条件 ...

  4. mybatis常用标签

    1. 定义sql语句 1.1 select 标签 属性介绍: id :唯一的标识符. parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user ...

  5. Mybatis常用标签使用

    trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix:可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖, ...

  6. [刘阳Java]_MyBatis_映射文件的常用标签总结_第5讲

    MyBatis中常用标签的总结,简单给出自己的总结 MyBatis映射文件中的标签使用介绍1.<select>:用于编写查询语句用的标签 id:表示当前<select>标签的唯 ...

  7. Mybatis 常用注解

    Mybatis常用注解对应的目标和标签如表所示: 注解 目标 对应的XML标签 @CacheNamespace 类 <cache> @CacheNamespaceRef 类 <cac ...

  8. Mybatis foreach标签含义

    背景 考虑以下场景: InfoTable(信息表): Name Gender Age Score 张三 男 21 90 李四 女 20 87 王五 男 22 92 赵六 女 19 94 孙七 女 23 ...

  9. MyBatis 常用写法

    MyBatis 常用写法 1.forEach 循环   forEach 元素的属性主要有 item, idnex, collection, open, separator, close. collec ...

随机推荐

  1. JVM-垃圾收集算法基础

    目录 目录 前言 手动释放内存导致的问题 垃圾判定方法 哪些对象是垃圾? 引用计数算法 可达性分析法 垃圾收集算法 标记-清除 优点 缺点 优化 标记-复制 优点 缺点 优化 标记-整理 优点 缺点 ...

  2. 工作流中的数据持久化详解!Activiti框架中JPA的使用分析

    Activiti中JPA简介 可以使用JPA实体作为流程变量, 并进行操作: 基于流程变量更新已有的JPA实体,可以在用户任务的表单中填写或者由服务任务生成 重用已有的领域模型,不需要编写显示的服务获 ...

  3. 摄像头PVD和CVD薄膜

    摄像头PVD和CVD薄膜 在FDP 的生产中,在制作无机薄膜时,可以采用的方法有两种:PVD 和CVD (将VE 和VS 归于PVD ,而ALD 归于CVD). Physical Vapor Depo ...

  4. OpenCV读写图像文件解析

    OpenCV读写图像文件解析 imdecode 从内存中的缓冲区读取图像. C++:Mat imdecode(InputArray buf, int flags) C++:Mat imdecode(I ...

  5. HAL库|神器cubemx的正确打开方式

    前言 工欲善其事,必先利其器.HAL库的开发不一定必须使用cubemx,但是使用了cubemx,你绝对不会后悔.基于一些小伙伴对cubemx的使用还有一些疑问,本次小飞哥从新建工程到生成工程,编写应用 ...

  6. QT Dialog模态与非模态

    模态 // 创建对话框窗口 TestDialog* dlg = new TestDialog(this); // 阻塞程序的运行 dlg->exec(); 这样的话,当运行对话窗口的时候,会阻塞 ...

  7. Redis五种基础与三种高级数据结构解析

    记得点赞+关注呦. 前言 在 Redis 最重要最基础就属 它丰富的数据结构了,Redis 之所以能脱颖而出很大原因是他数据结构丰富,可以支持多种场景.并且 Redis 的数据结构实现以及应用场景在面 ...

  8. Django基础之auth模块

    内容概要 用户认证模块auth auth模块补充 auth_user表扩展字段 内容详细 auth模块 主要是用来做用户相关的功能 注册 登录 验证 修改密码 注销 ​ 访问admin需要管理员账号 ...

  9. 如何使用perf进行程序分析

    1.安装. sudo apt-get install linux-tools 如果提示没有可安装候选.请输入: sudo apt-get install linux-perf-version 其中ve ...

  10. Spring Cloud Alibaba(15)---Sleuth+Zipkin

    SpringCloudAlibaba整合Sleuth+Zipkin 有关Sleuth之前有写过两篇文章 Spring Cloud Alibaba(13)---Sleuth概述 Spring Cloud ...