Mybatis——基础操作、动态SQL

news/2025/3/16 15:40:37/

目录

一.基础操作

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/news/1579597.html

相关文章

向量数据库全景概览:100+解决方案分类解析

随着AI技术的快速发展&#xff0c;向量数据库已成为处理高维数据、支持相似性搜索的核心基础设施。本文将对当前主流的100向量数据库/存储方案进行分类解析&#xff0c;为技术选型提供全景式参考。 一、内存与轻量级存储 适用于开发测试、小规模数据或嵌入式场景 InMemoryVect…

基于Spring Boot的航司互售系统

文章目录 项目介绍项目截图项目获取方式 &#x1f345; 作者主页&#xff1a;超级无敌暴龙战士塔塔开 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、 简历模板、学习资料、面试题库【关注我&#xff0c;都给你】 &#x1f345;文末获取源码联系&#x1f34…

Caffeine 缓存:简介、优势及应用场景

Caffeine 缓存&#xff1a;简介、优势及应用场景 前言一、Caffeine 缓存简介1. Caffeine 的核心特性2. 为什么选择 Caffeine 而不是其他缓存库&#xff1f; 二、Caffeine 适用的场景1. 高并发请求缓存2. 数据库查询缓存3. 复杂计算结果缓存 三、Caffeine 的优势1. 性能优化2. 内…

基于python+django+vue.js开发的社区养老管理系统源码+运行步骤

业余时间开发的社区养老系统&#xff0c;基于python/vue技术开发。学习过程问题可以留言。 功能介绍 平台采用B/S结构&#xff0c;后端采用主流的Python语言进行开发&#xff0c;前端采用主流的Vue.js进行开发。 功能包括&#xff1a;老人管理、护工管理、亲属管理、病史管理…

人工智能与人的智能,改变一生的思维模型【8】逆向思维

逆向偏差思维模型&#xff1a;顶尖高手如何「反常识」破局 &#xff08;斯坦福决策科学中心认证的逆向思考框架&#xff09; 一、直击本质&#xff1a;什么是逆向偏差思维&#xff1f; 定义&#xff1a; 逆向偏差思维是一种主动对抗本能认知倾向的决策模式&#xff0c;通过系…

配置blender的python环境

在blender的脚本出输入&#xff1a; import sys print(sys.executable) 2. 通过上述命令我们得到blener的python版本&#xff0c;下面我们在conda配置一个同样版本的python环境。 conda create -n blenderpy python3.11.9找到blender安装路径下的python文件夹&#xff0c;将它…

算法每日一练 (11)

&#x1f4a2;欢迎来到张胤尘的技术站 &#x1f4a5;技术如江河&#xff0c;汇聚众志成。代码似星辰&#xff0c;照亮行征程。开源精神长&#xff0c;传承永不忘。携手共前行&#xff0c;未来更辉煌&#x1f4a5; 文章目录 算法每日一练 (11)全排列题目描述解题思路解题代码c/c…

Java数据结构第二十三期:Map与Set的高效应用之道(二)

专栏&#xff1a;Java数据结构秘籍 个人主页&#xff1a;手握风云 目录 一、哈希表 1.1. 概念 1.2. 冲突 1.3. 避免冲突 1.4. 解决冲突 1.5. 实现 二、OJ练习 2.1. 只出现一次的数字 2.2. 随机链表的复制 2.3. 宝石与石头 一、哈希表 1.1. 概念 顺序结构以及平衡树中…