MyBatis Mapper.XML 标签使用说明
直接将值返回给对象
<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 >= now()
</otherwise>
</choose>
</select>
where 适用于从多个条件中选择所有满足条件的来构成condtions,
| 符号 | 小于 | 小于等于 | 大于 | 大于等于 | 和 | 单引号 | 双引号 |
| 原符号 | < | <= | > | >= | & | ' | " |
| 替换符号 | < | <= | > | >= | & | ' | " |
<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 >= #{startDate} <!--大于等于 开始时间-->
</if>
<if test="endDate != null">
AND end_date <= #{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 标签使用说明的更多相关文章
- MyBatis Mapper XML 详解
MyBatis Mapper XML 详解 MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JD ...
- MyBatis Mapper.xml文件中 $和#的区别
MyBatis Mapper.xml文件中 $和#的区别 网上有很多,总之,简略的写一下,作为备忘.例子中假设参数名为 paramName,类型为 VARCHAR . 1.优先使用#{paramN ...
- mybatis mapper.xml 配置文件问题(有的错误xml是不报的) 导致服务无法启动 。
转载自 开源编程 一舟mybatsi xml编译报错,tomcat启动一直循环,导致内存溢出,启动失败 mapper.xml怎么知道有没有编译错误,哪个位置有错误 这应该是mybatis的一个bug, ...
- mybatis mapper xml文件的导入方式和查询方式
mybatis mapper xml文件的导入方式和查询方式 ssm框架 Mybatis mapper与SQLSession的关系 每个基于MyBatis的应用都是以一个SqlSessionFact ...
- mybatis Mapper.xml和Mapper.java
mybatis Mapper.xml和Mapper.java 通过Mapper.xml和Mapper.java来实现mybatis.环境和入门的一样的.关键:Mapper.xml + Mapper.j ...
- 自动回复之实现随机回复与常用Mapper XML标签
[常用Mapper XML标签] 1.基本的:select.insert.update 等 2.可读性.方便拼SQL:where.set.trim 3.减少重复:sql 4.逻辑控制:if.choos ...
- mybatis mapper xml文件配置resultmap时,id行和result行有什么区别?
mybatis mapper xml文件配置resultmap时,id行和result行有什么区别? <resultMap id = "CashInvoiceMap" typ ...
- JAVA - Intellij IDEA 中去掉mybatis Mapper.xml背景色
JAVA - Intellij IDEA 中去掉mybatis Mapper.xml背景色 1:现在公司中使用mybatis的频率非常高,一般都会用MBG来生成基础的代码文件.在intellij中查看 ...
- MyBatis——Mapper XML 文件
Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...
- MyBatis—mapper.xml映射配置
SQL文件映射(mapper文件),几个顶级元素的配置: mapper元素:根节点只有一个属性namespace(命名空间)作用: 1:用于区分不同的mapper,全局唯一. 2:绑定DAO接口,即面 ...
随机推荐
- van-dialog弹窗异步关闭-校验表单
van-dialog弹窗异步关闭 有时候我们需要通过弹窗去处理表单数据,在原生微信小程序配合vant组件中有多种方式实现,其中UI美观度最高的就是通过van-dialog嵌套表单实现. 通常表单涉及到 ...
- Educational Codeforces Round 127 (Rated for Div. 2) E. Preorder
设\(f[v]\)是以结点\(v\)为根的方案数,设左子树的根为\(x\),右子树的根为\(y\),那么如果左右子树完全相同,那么我们交换左右子树对方案没有任何影响,都是: \[f[v] = f[x] ...
- WPF --- 如何以Binding方式隐藏DataGrid列
引言 如题,如何以Binding的方式动态隐藏DataGrid列? 预想方案 像这样: 先在ViewModel创建数据源 People 和控制列隐藏的 IsVisibility,这里直接以 MainW ...
- Tech Lead 要学会戴着镣铐跳舞
这不是一篇讨喜的文章,至少不会是你常常看到的例如<成为优秀 Tech Lead 的六个建议>令人欢欣鼓舞的那一类.今天我们聊聊 Tech Lead 所面临的不那么轻松的现实问题 程序员一定 ...
- Web前端工程的装机必备软件
前言 最近作者的电脑 C 盘变红了,这让我很难受(有点小强迫症),所以准备重新安装下系统,顺便把 C 盘扩大点. 注意: 操作系统是 windows 11 23H2. 所有的命令行都是使用 Windo ...
- [ABC262G] LIS with Stack
Problem Statement There is an empty sequence $X$ and an empty stack $S$. Also, you are given an inte ...
- 使用Visual Studio 2022 创建lib和dll并使用
对于一个经常写javaWeb的人来说,使用Visual Studio似乎没什么必要,但是对于使用ffi的人来说,使用c或c++编译器,似乎是必不可少的,下面我将讲述如何用Visual Studio 2 ...
- Cocos内存管理解析 CCRef/retain/release/autorelease
Cocos内存管理源码(autorelease解析) 背景 这段时间在做项目的时候,需求需要往spine动作的挂点上绑定按钮节点,由于按钮在编辑器中是加在已有节点上的,所以在往spine上添加挂点时, ...
- LeetCode227:基本计算器|| (栈、模拟)
解题思路:两个双端队列模拟,一个存放操作数 a,另一个存放操作符 op,如果找到另一个操作数b,判断操作队列队尾是否是*/,是的话执行 a(*or/)b.遍历完字符串,如果操作符队列非空,说明还有+- ...
- 江西财经大学第一届程序设计竞赛 H题- 小P的数学问题
题目链接:https://www.nowcoder.com/acm/contest/115/H 解题思路:分块打表!!! 什么是分块打表呢??? 从这道题我们知道我们要找到最多1*e9的阶乘 那循环暴 ...