Mybatis——基础操作、动态SQL

embedded/2025/3/16 19:17:54/

目录

一.基础操作

1.删除

2.新增

3.更新

4.查询

5.XML映射文件

二、动态SQL

1.<if>

2.<where>

3.<set>

4.<foreach>

5.<sql>

6.<include>


一.基础操作

1.删除

参数占位符:

注意: 

#{...}相比于${...}具有性能高、防止SQL注入的优势

2.新增

3.更新

4.查询

普通查询:

条件查询:

注意:#{...}不能放在' '当中,使用${..,},会发生SQL注入,解决办法是使用contact()函数

数据封装: 

5.XML映射文件

实例:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace为Mapper接口的全限定名-->
<mapper namespace="com.itheima.springbootmybatisquickstart.mapper.EmpMapper"><!--id为函数名,resultType为返回的对象的全限定名--><select id="IndistinctSelect" resultType="com.itheima.springbootmybatisquickstart.pojo.Emp"><!--执行的SQL语句-->select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where name like concat('%',#{name},'%') and gender=#{gender} and entrydate between #{begin} and #{end}</select>
</mapper>

二、动态SQL

1.<if>

用于判断条件是否成立。使用test属性进行条件判断,如果条件为true,则拼接SQL

 <!--执行的SQL语句-->select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime,update_time updateTime from emp where<if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender!=null">and gender=#{gender}</if><if test="begin!=null and end!=null">and entrydate between #{begin} and #{end}</if>

2.<where>

where元素只会在子元素有内容的情况下才插入where语句。而且会自动去除子句的开头的AND或OR

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace为Mapper接口的全限定名-->
<mapper namespace="com.itheima.springbootmybatisquickstart.mapper.EmpMapper"><!--id为函数名,resultType为返回的对象的全限定名--><select id="IndistinctSelect" resultType="com.itheima.springbootmybatisquickstart.pojo.Emp"><!--执行的SQL语句-->select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime,update_time updateTime from emp<where><if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender!=null">and gender=#{gender}</if><if test="begin!=null and end!=null">and entrydate between #{begin} and #{end}</if></where></select>
</mapper>

3.<set>

动态地在行首插入SET关键字,并会删除额外的逗号。(用在update语句中)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace为Mapper接口的全限定名-->
<mapper namespace="com.itheima.springbootmybatisquickstart.mapper.EmpMapper"><update id="update">update emp<set><if test="username!=null">username=#{username},</if><if test="name!=null">name=#{name},</if><if test="gender!=null">gender=#{gender},</if><if test="image!=null">image=#{image},</if><if test="job!=null">job=#{job},</if><if test="entrydate!=null">entrydate=#{entrydate},</if><if test="deptId!=null">dept_id=#{deptId},</if><if test="updateTime!=null">update_time=#{updateTime}</if></set>where id = #{id}</update>
</mapper>

4.<foreach>

 

5.<sql>

定义可重复的SQL片段

 <sql id="commonSelect">select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime,update_time updateTime from emp</sql>

6.<include>

 通过属性refid,指定包含的sql片段

<include refid="commonSelect"/>


http://www.ppmy.cn/embedded/173139.html

相关文章

【MyBatis Plus 逻辑删除详解】

文章目录 MyBatis Plus 逻辑删除详解前言什么是逻辑删除&#xff1f;MyBatis Plus 中的逻辑删除1. 添加逻辑删除字段2. 实体类的配置3. 配置 MyBatis Plus4. 使用逻辑删除5. 查询逻辑删除的记录 MyBatis Plus 逻辑删除详解 前言 MyBatis Plus 是一个强大的持久化框架&#xf…

前端 Webpack 面试题

1、什么是 Webpack?它有什么作用? Webpack 是一个前端资源打包工具,用于将 JavaScript、CSS、图片等项目资源进行模块化管理和打包。它能够将复杂的项目结构转化为浏览器友好的代码,提高前端项目的开发效率和性能。 模块打包:Webpack 将项目中的各个模块及依赖打包成一个…

AWK 入门教程:强大的文本处理工具

AWK 是一种强大的文本处理工具&#xff0c;广泛用于 Linux/Unix 系统中对文本文件或数据流进行操作。它能够基于条件筛选、统计字段、重新排列数据等。主要特点包括&#xff1a; 2. AWK 的基本语法 2.1 AWK 程序的结构 AWK 程序的结构: awk pattern { action } file 2.2 常…

iptables练习笔记20250315

一、简单练习环境 在VMware Workstation部署三台Ubuntu22.04虚机&#xff0c;分别为H1、H2、H3&#xff0c;其中H2已安装KVM&#xff08;可参考Ubuntu22.04 KVM安装笔记&#xff09;。 本次练习主要查看filter table的FORWARD chain的情况。 在H1发起ping 11.0.20.4&#xf…

Jenkins 集成DingDing 推送

现状分析 开发频繁发布代码&#xff0c;和测试没有及时沟通&#xff0c;导致测试返工、bug漏测等 解决方案 Jenkins 集成DingDing机器人&#xff0c;在构建时触发推送 DingDing端机器人配置 1、在钉钉电脑端建立群聊 2、点击群右上角设置&#xff0c;点击【智能群助手】 …

STM32 —— 嵌入式系统、通用计算机系统、物联网三层架构

目录 一、嵌入式系统的概念 二、通用计算机系统与嵌入式系统的比较 用途 硬件 软件 性能与功耗 开发与维护 三、嵌入式系统与物联网的关系 四、物联网的三层架构 1. 感知层&#xff08;Perception Layer&#xff09; 2. 网络层&#xff08;Network Layer&#xff09; …

游戏引擎学习第153天

仓库:https://gitee.com/mrxiao_com/2d_game_3 回顾 目前正在进行的是一个比较大的系统调整&#xff0c;原本预计今天会继续深入这个改动&#xff0c;但实际上在昨天的开发中&#xff0c;我们已经完成了大部分的代码编写&#xff0c;并且运行之后几乎一切都能正常工作&#x…

【Unity网络同步框架 - Nakama研究】

Unity网络同步框架 - Nakama研究 介绍 如果你现在被委托了一个需求&#xff0c;要求调研并且撰写一份关于Unity网络同步框架方面的报告&#xff0c;你会如何做 我知道&#xff0c;现在AI这么多&#xff0c;我马上打开DeepSeek进行光速搜索&#xff0c;那么好&#xff0c;如下…