当前位置: 首页 > news >正文

mapper文件的解释

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.system.mapper.JianaiSongMapper"><resultMap type="JianaiSong" id="JianaiSongResult"><result property="songId"    column="song_id"    /><result property="title"    column="title"    /><result property="artist"    column="artist"    /><result property="album"    column="album"    /><result property="releaseDate"    column="release_date"    /><result property="genre"    column="genre"    /><result property="duration"    column="duration"    /><result property="lyrics"    column="lyrics"    /><result property="filePath"    column="file_path"    /><result property="coverPath"    column="cover_path"    /></resultMap><sql id="selectJianaiSongVo">select song_id, title, artist, album, release_date, genre, duration, lyrics, file_path, cover_path from jianai_song</sql><select id="selectJianaiSongList" parameterType="JianaiSong" resultMap="JianaiSongResult"><include refid="selectJianaiSongVo"/><where>  <if test="title != null  and title != ''"> and title = #{title}</if><if test="artist != null  and artist != ''"> and artist = #{artist}</if><if test="album != null  and album != ''"> and album = #{album}</if><if test="releaseDate != null "> and release_date = #{releaseDate}</if><if test="genre != null  and genre != ''"> and genre = #{genre}</if><if test="duration != null "> and duration = #{duration}</if><if test="lyrics != null  and lyrics != ''"> and lyrics = #{lyrics}</if><if test="filePath != null  and filePath != ''"> and file_path = #{filePath}</if><if test="coverPath != null  and coverPath != ''"> and cover_path = #{coverPath}</if></where></select><select id="selectJianaiSongBySongId" parameterType="Long" resultMap="JianaiSongResult"><include refid="selectJianaiSongVo"/>where song_id = #{songId}</select><insert id="insertJianaiSong" parameterType="JianaiSong" useGeneratedKeys="true" keyProperty="songId">insert into jianai_song<trim prefix="(" suffix=")" suffixOverrides=","><if test="title != null and title != ''">title,</if><if test="artist != null and artist != ''">artist,</if><if test="album != null">album,</if><if test="releaseDate != null">release_date,</if><if test="genre != null">genre,</if><if test="duration != null">duration,</if><if test="lyrics != null">lyrics,</if><if test="filePath != null">file_path,</if><if test="coverPath != null">cover_path,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="title != null and title != ''">#{title},</if><if test="artist != null and artist != ''">#{artist},</if><if test="album != null">#{album},</if><if test="releaseDate != null">#{releaseDate},</if><if test="genre != null">#{genre},</if><if test="duration != null">#{duration},</if><if test="lyrics != null">#{lyrics},</if><if test="filePath != null">#{filePath},</if><if test="coverPath != null">#{coverPath},</if></trim></insert><update id="updateJianaiSong" parameterType="JianaiSong">update jianai_song<trim prefix="SET" suffixOverrides=","><if test="title != null and title != ''">title = #{title},</if><if test="artist != null and artist != ''">artist = #{artist},</if><if test="album != null">album = #{album},</if><if test="releaseDate != null">release_date = #{releaseDate},</if><if test="genre != null">genre = #{genre},</if><if test="duration != null">duration = #{duration},</if><if test="lyrics != null">lyrics = #{lyrics},</if><if test="filePath != null">file_path = #{filePath},</if><if test="coverPath != null">cover_path = #{coverPath},</if></trim>where song_id = #{songId}</update><delete id="deleteJianaiSongBySongId" parameterType="Long">delete from jianai_song where song_id = #{songId}</delete><delete id="deleteJianaiSongBySongIds" parameterType="String">delete from jianai_song where song_id in <foreach item="songId" collection="array" open="(" separator="," close=")">#{songId}</foreach></delete>
</mapper>

这段代码是一个 MyBatis 的映射文件,它定义了与数据库交互的 SQL 语句和结果映射。以下是代码的逐行解释:

头部声明

<?xml version="1.0" encoding="UTF-8" ?>

这行声明了 XML 文档的版本和编码。

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

这是文档类型声明,指定了 MyBatis 映射文件的 DTD,用于 XML 的验证。

映射空间声明

<mapper namespace="com.ruoyi.system.mapper.JianaiSongMapper">

定义了一个命名空间,通常是一个完整的 Java 接口名,用于隔离不同的映射文件。

结果映射定义

<resultMap type="JianaiSong" id="JianaiSongResult"><result property="songId"    column="song_id"    /><result property="title"    column="title"    /><!-- 其他字段省略 -->
</resultMap>

定义了一个结果映射 JianaiSongResult,它将数据库查询结果的列映射到 JianaiSong 实体类的属性上。

SQL 片段定义

<sql id="selectJianaiSongVo">select song_id, title, artist, album, release_date, genre, duration, lyrics, file_path, cover_path from jianai_song
</sql>

定义了一个 SQL 片段 selectJianaiSongVo,用于选取歌曲表中的所有字段。

查询操作

<select id="selectJianaiSongList" parameterType="JianaiSong" resultMap="JianaiSongResult"><include refid="selectJianaiSongVo"/><where>  <!-- 条件判断省略 --></where>
</select>

定义了一个查询操作 selectJianaiSongList,它使用 selectJianaiSongVo SQL 片段,并根据传入的参数动态地添加查询条件。

<select id="selectJianaiSongBySongId" parameterType="Long" resultMap="JianaiSongResult"><include refid="selectJianaiSongVo"/>where song_id = #{songId}
</select>

定义了一个根据歌曲 ID 查询歌曲信息的操作 selectJianaiSongBySongId

插入操作

<insert id="insertJianaiSong" parameterType="JianaiSong" useGeneratedKeys="true" keyProperty="songId"><!-- 插入语句省略 -->
</insert>

定义了一个插入操作 insertJianaiSong,它将 JianaiSong 实体类的数据插入到数据库中,并返回生成的主键值。

更新操作

<update id="updateJianaiSong" parameterType="JianaiSong"><!-- 更新语句省略 -->
</update>

定义了一个更新操作 updateJianaiSong,它根据传入的 JianaiSong 实体类的数据更新数据库中的记录。

删除操作

<delete id="deleteJianaiSongBySongId" parameterType="Long">delete from jianai_song where song_id = #{songId}
</delete><delete id="deleteJianaiSongBySongIds" parameterType="String">delete from jianai_song where song_id in <foreach item="songId" collection="array" open="(" separator="," close=")">#{songId}</foreach>
</delete>

定义了两个删除操作 deleteJianaiSongBySongIddeleteJianaiSongBySongIds,分别用于根据单个歌曲 ID 和多个歌曲 ID 删除记录。

遍历操作

<foreach item="songId" collection="array" open="(" separator="," close=")">#{songId}
</foreach>

这是一个遍历操作,用于生成 SQL 中的 IN 语句,它将数组或集合中的每个元素遍历出来,并用逗号分隔。

整个映射文件提供了对 jianai_song 表的 CRUD 操作(创建、读取、更新、删除),以及动态 SQL 功能,使得可以根据条件构建查询语句。

这个映射文件是如何与数据库进行交互的?

这个 MyBatis 映射文件通过定义 SQL 语句和结果映射来与数据库进行交互。以下是详细的交互过程:

1. 映射文件与 MyBatis 框架集成

  • 这个 XML 映射文件需要被 MyBatis 框架解析和加载。通常,这是通过在 MyBatis 配置文件中指定映射文件的位置来完成的,或者通过注解方式直接在接口上定义映射。

2. 定义结果映射(ResultMap)

  • <resultMap> 标签定义了如何将数据库查询结果的列映射到 Java 对象的属性上。在这个例子中,JianaiSongResult 结果映射定义了 JianaiSong 实体类的属性与数据库表 jianai_song 的列之间的对应关系。

3. 定义 SQL 片段(SQL Statement)

  • <sql> 标签定义了可重用的 SQL 片段,例如 selectJianaiSongVo,它包含了查询 jianai_song 表的所有字段的 SQL 语句。

4. 定义 CRUD 操作

  • <select><insert><update><delete> 标签定义了具体的数据库操作。这些操作使用前面定义的结果映射和 SQL 片段来执行数据库查询和更新。

5. 动态 SQL

  • MyBatis 支持动态 SQL,可以根据条件动态构建 SQL 语句。例如,<if> 标签用于在 <where> 子句中动态添加条件,只有当传入的参数不为空时,相应的条件才会被包含在 SQL 语句中。

6. 执行数据库操作

  • 当应用程序调用 Mapper 接口中的方法时,MyBatis 会根据方法名找到对应的 <select><insert><update><delete> 标签,并执行其中的 SQL 语句。
  • 对于 <select> 操作,MyBatis 会将查询结果根据结果映射转换为 Java 对象列表或单个对象,并返回给应用程序。
  • 对于 <insert><update><delete> 操作,MyBatis 会执行相应的 SQL 语句,并返回受影响的行数。

7. 参数和结果处理

  • MyBatis 使用参数占位符(如 #{property})来传递方法参数到 SQL 语句中。这些参数在执行 SQL 语句之前会被预处理,以防止 SQL 注入攻击。
  • 对于查询操作,MyBatis 根据结果映射将数据库查询结果映射到 Java 对象的属性中。

8. 事务管理

  • MyBatis 可以配置事务管理,以确保数据库操作的原子性、一致性、隔离性和持久性。

通过以上步骤,MyBatis 映射文件定义了与数据库交互的具体细节,使得 Java 应用程序可以通过简单的方法调用来执行复杂的数据库操作,同时保持代码的简洁性和可维护性。

http://www.lryc.cn/news/513486.html

相关文章:

  • 常见协议的高危软件漏洞信息
  • Mediatek Android13 ROM定制
  • RedisInsight:企业级 Redis 管理与分析工具
  • c# 快捷键模块
  • 【笔记】增值税计算笔记
  • 请解释 JavaScript 中的闭包,以及它的优缺点和常见使用场景?
  • SpringBoot 集成 Caffeine 实现本地缓存
  • druid连接池参数配置
  • 【OceanBase】通过 OceanBase 的向量检索技术构建图搜图应用
  • Linux 安装运行gatk的教程
  • 什么是unit l2 norm
  • 手写顺序流程图组件
  • 适配器模式概述
  • Logo设计免费生成器:轻松设计个性化标志
  • 智能停车场车牌识别计费系统
  • 谷歌开通第三方平台OAuth登录及Java对接步骤
  • 人体:精妙绝伦的生命之躯
  • python的urllib模块和http模块
  • Java [后端] 开发日常记录(1)
  • jetbrain 安装 copilot
  • 万里数据库GreatSQL监控解析
  • OpenCV-Python实战(9)——滤波降噪
  • Pytorch | 利用DTA针对CIFAR10上的ResNet分类器进行对抗攻击
  • Linux性能测试简介
  • Kile5支持包的安装
  • 【Ubuntu 系统 之 开启远程桌面SSH登录】
  • MySQL 索引分类及区别与特点
  • 对中文乱码的理解,遇到乱码该怎么办。
  • 《机器学习》从入门到实战——逻辑回归
  • svn不能添加.a文件