直接将值返回给对象

<select id="list" resultType="com.vipsoft.base.entity.UserInfo">
SELECT Id,Title FROM User
</select>

如果字段和属性名不一致时,通过 resultMap 做映射

<resultMap id="StudentResult" type="com.mybatis3.domain.Student">
<id column="stud_id" property="studId" jdbcType="VARCHAR"/>
<result column="user_name" property="name" jdbcType="VARCHAR"/>
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap> <select id="findAllStudents" resultMap="StudentResult" >
SELECT * FROM STUDENTS
</select>

实体引用另一个实体的查询结果返回

<resultMap type="Course" id="CourseResult">
<id column="course_id" property="courseId"/>
<result column="name" property="name"/>
<result column="description" property="description"/>
<result column="start_date" property="startDate"/>
<result column="end_date" property="endDate"/>
</resultMap> <resultMap type="Tutor" id="TutorResult">
<id column="tutor_id" property="tutorId"/>
<result column="tutor_name" property="name"/>
<result column="email" property="email"/>
<collection property="courses" resultMap="CourseResult"/>
</resultMap> <select id="findTutorById" parameterType="int" resultMap="TutorResult">
SELECT T.TUTOR_ID, T.NAME AS TUTOR_NAME, EMAIL, C.COURSE_ID, C.NAME, DESCRIPTION, START_DATE, END_DATE
FROM TUTORS T LEFT OUTER JOIN ADDRESSES A ON T.ADDR_ID=A.ADDR_ID
LEFT OUTER JOIN COURSES C ON T.TUTOR_ID=C.TUTOR_ID
WHERE T.TUTOR_ID=#{tutorId}
</select>

choose 该方式适用于多个条件中选择一个满足条件的来生成sql

<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
SELECT * FROM COURSES
<choose>
<when test="searchBy == 'Tutor'">
WHERE TUTOR_ID= #{tutorId}
</when>
<when test="searchBy == 'CourseName'">
WHERE name like #{courseName}
</when>
<otherwise>
WHERE TUTOR start_date &gt;= now()
</otherwise>
</choose>
</select>

where 适用于从多个条件中选择所有满足条件的来构成condtions,

符号 小于 小于等于 大于 大于等于 单引号 双引号
原符号 < <= > >= & ' "
替换符号 &lt; &lt;= &gt; &gt;= &amp; &apos; &quot;
<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
SELECT * FROM COURSES
<where>
<if test=" tutorId != null ">
TUTOR_ID= #{tutorId}
</if>
<if test="courseName != null">
AND name like #{courseName}
</if>
<if test="startDate != null">
AND start_date &gt;= #{startDate} <!--大于等于 开始时间-->
</if>
<if test="endDate != null">
AND end_date &lt;= #{endDate} <!--小于等于 结束时间-->
</if>
</where>
</select>

foreach

<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
<where>
<!--
collection:指定输入对象中的集合属性.该属性的值有三种:list,array,map,根据传入的集合类型而设定该值。
item:每次遍历生成的对象
index:当前迭代的次数
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串
select * from user where 1=1 and id in (1,2,3)
-->
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</where>
</select>

select * from user where id=1 or id=2 or id=3

<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
<where>
<foreach collection="list" item="id" open="and (" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>

< sql >标签
该标签主要定义复用的sql语句片段,在执行的sql语句标签直接引用即可。可以提高编码效率、简化代码和提高可读性。

需要配置id熟悉,表示该sql片段的唯一标识。

引用:通过<include refid=" " / >标签引用,refid的值就是< sql>的id属性的值。

<sql id="Base_Column_List">
id, question, answer
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from java
where id = #{id,jdbcType=BIGINT}
</select>

<set> : 主要用来替换sql语句中的set字段,一般在update中使用。

<update>
update user
<set>
<if test="name != null and name.length()>0">name = #{name},</if>
<if test="age != null and age .length()>0">age = #{age },</if>
</set>
where id = #{id}
</update> 

批量更新,一次执行多条SQL

<update>
<foreach collection="list" item="item" index="index" separator=";">
update user
<set>
<if test="name != null and name.length()>0">name = #{name},</if>
<if test="age != null and age .length()>0">age = #{age },</if>
</set>
<where>
<if test="id != null and id != ''">
AND id = #{id,jdbcType=VARCHAR}
</if>
<if test="userName != null and userName != ''">
<bind name="bindUserName" value="'%' + userName + '%'" />
AND UserName like #{bindUserName}
</if>
<if test="status != null">
AND Status = #{status,jdbcType=INTEGER}
</if>
</where>
</foreach>
</update>

<trim> : 是一个格式化的标记,可以完成set或者是where标记的功能。

select * from user
<trim prefix="WHERE" prefixoverride="AND |OR">
<if test="name != null and name.length()>0"> AND name=#{name}</if>
<if test="age != null and age.length()>0"> AND age=#{age}</if>
</trim> 假如说name和age的值都不为null的话打印的SQL为:select * from user where name = ‘xx’ and age = ‘xx’
在where的后面是不存在第一个and的,上面两个属性的意思如下:
  prefix:前缀      
  prefixoverride:去掉第一个and或者是or
update user
<trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
<if test="name != null and name.length()>0"> name=#{name} , </if>
<if test="age!= null and age.length()>0"> age=#{age} , </if>
</trim>
假如说name和age的值都不为null的话打印的SQL为:update user set name=‘xx’ , age=‘xx’ where id=‘x’ 在age='xx’的后面不存在逗号,而且自动加了一个set前缀和where后缀,上面三个属性的意义如下,其中prefix意义如上:
   suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
   suffix:后缀

choose & foreach list 有一个值,条件=,有多个值 in

<update id="updateStatus" parameterType="java.util.ArrayList" >
update User
set Status = 1,UpdateTime=(select GETDATE())
<choose>
<when test="idList != null and idList.size==1">
WHERE Id= #{id,jdbcType=VARCHAR}
</when>
<when test="idList != null and idList.size>1">
WHERE Id in
<foreach collection="idList" item="id" open="(" close=")" separator=",">
<if test="id != null and id != '' ">
#{id,jdbcType=VARCHAR}
</if>
</foreach>
</when>
<otherwise>
WHERE 1=0
</otherwise>
</choose>
</update>

MyBatis Mapper.XML 标签使用说明的更多相关文章

  1. MyBatis Mapper XML 详解

    MyBatis Mapper XML 详解 MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JD ...

  2. MyBatis Mapper.xml文件中 $和#的区别

    MyBatis Mapper.xml文件中 $和#的区别   网上有很多,总之,简略的写一下,作为备忘.例子中假设参数名为 paramName,类型为 VARCHAR . 1.优先使用#{paramN ...

  3. mybatis mapper.xml 配置文件问题(有的错误xml是不报的) 导致服务无法启动 。

    转载自 开源编程 一舟mybatsi xml编译报错,tomcat启动一直循环,导致内存溢出,启动失败 mapper.xml怎么知道有没有编译错误,哪个位置有错误 这应该是mybatis的一个bug, ...

  4. mybatis mapper xml文件的导入方式和查询方式

    mybatis mapper xml文件的导入方式和查询方式 ssm框架 Mybatis  mapper与SQLSession的关系 每个基于MyBatis的应用都是以一个SqlSessionFact ...

  5. mybatis Mapper.xml和Mapper.java

    mybatis Mapper.xml和Mapper.java 通过Mapper.xml和Mapper.java来实现mybatis.环境和入门的一样的.关键:Mapper.xml + Mapper.j ...

  6. 自动回复之实现随机回复与常用Mapper XML标签

    [常用Mapper XML标签] 1.基本的:select.insert.update 等 2.可读性.方便拼SQL:where.set.trim 3.减少重复:sql 4.逻辑控制:if.choos ...

  7. mybatis mapper xml文件配置resultmap时,id行和result行有什么区别?

    mybatis mapper xml文件配置resultmap时,id行和result行有什么区别? <resultMap id = "CashInvoiceMap" typ ...

  8. JAVA - Intellij IDEA 中去掉mybatis Mapper.xml背景色

    JAVA - Intellij IDEA 中去掉mybatis Mapper.xml背景色 1:现在公司中使用mybatis的频率非常高,一般都会用MBG来生成基础的代码文件.在intellij中查看 ...

  9. MyBatis——Mapper XML 文件

    Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...

  10. MyBatis—mapper.xml映射配置

    SQL文件映射(mapper文件),几个顶级元素的配置: mapper元素:根节点只有一个属性namespace(命名空间)作用: 1:用于区分不同的mapper,全局唯一. 2:绑定DAO接口,即面 ...

随机推荐

  1. JUC并发编程学习笔记(十四)异步回调

    异步回调 Future设计的初衷:对将来的某个事件的结果进行建模 在Future类的子类中可以找到CompletableFuture,在介绍中可以看到这是为非异步的请求使用一些异步的方法来处理 点进具 ...

  2. Python 利用pandas和matplotlib绘制双柱状图

    在数据分析和可视化中,常用的一种图形类型是柱状图.柱状图能够清晰地展示不同分类变量的数值,并支持多组数据进行对比.本篇文章将介绍如何使用Python绘制双柱状图. 准备工作 在开始绘制柱状图之前,需要 ...

  3. SQL改写案例4(开窗函数取中位数案例)

    周总找我问个报表SQL实现逻辑的案例,废话不说给他看看. 原SQL: SELECT d.tname 姓名, d.spname 岗位, d.sum_cnt 报单单量, d.min_cnt 放款单量, d ...

  4. Wampserver搭建DVWA和sqli-labs问题总结

    Wampserver 搭建 DVWA 和 sqli-labs 问题总结 遇到问题解决的思路方法 百度,博客去搜索相关的问题,人工智能 chatgpt 查看官方文档,查看注释. 本次解决方法就是在文档的 ...

  5. Sealos 云操作系统一键集成 runwasi,解锁 Wasm 的无限潜力

    WebAssembly (通常缩写为 Wasm) 是一种为网络浏览器设计的低级编程语言.它旨在提供一种比传统的 JavaScript 更快.更高效的方式来执行代码,以弥补 JavaScript 在性能 ...

  6. MySQL安装、卸载与初始化

    一.MySQL简介 1.MySQL是什么 MySQL 是一款安全.跨平台.高效的,并与 PHP.Java等主流编程语言紧密结合的关系型数据库管理系统.MySQL 的象征符号是一只名为 Sakila 的 ...

  7. CTA策略介绍

    CTA策略更多的时候是一种投资方法,更准确的说,主要投资于衍生品的.比较系统化规则化的投资方法都可以称作CTA投资,它并不拘泥于量化或是主动,其具有相当的生命力,会长期存在. CTA策略的收入来源是多 ...

  8. C/C++ 通过SQLiteSDK增删改查

    SQLite,作为一款嵌入式关系型数据库管理系统,一直以其轻量级.零配置以及跨平台等特性而备受青睐.不同于传统的数据库系统,SQLite是一个库,直接与应用程序一同编译和链接,无需单独的数据库服务器进 ...

  9. STL常用函数

    STL简介 \(STL\)是\(Standard\) \(Template\) \(Library\)的简称,中文名称为标准模板库,从根本上讲, 就是各种\(STL\)容器的集合,容器可以理解为能够实 ...

  10. C++学习笔记八:极限和数学运算<limits><cmath>

    1) <limits>库: 1.1 源文档: https://en.cppreference.com/w/cpp/types/numeric_limits #include <lim ...