对Mapper.xml文件进行深入的学习
1. 前言
既上次在Mapper.xml文件出现bug之后,痛改前非,决定吃透Mapper.xml映射文件。
让我们通过具体的代码段来进一步理解 MyBatis 的 Mapper XML 文件中的每个组成部分。
<?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.goblin.BIbackend.mapper.BillsMapper">
<resultMap id="BaseResultMap" type="com.goblin.BIbackend.model.entity.Bills">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="userId" column="user_id" jdbcType="BIGINT"/>
<result property="amount" column="Amount" jdbcType="FLOAT"/>
<result property="billDate" column="bill_date" jdbcType="DATE"/>
<result property="dueDate" column="due_date" jdbcType="DATE"/>
<result property="isPaid" column="is_paid" jdbcType="BIGINT"/>
</resultMap>
<sql id="Base_Column_List">
id,user_id,Amount,
bill_date,due_date,is_paid
</sql>
<select id="list" resultType="com.goblin.BIbackend.model.entity.Bills">
select * from bills
<where>
<if test="userId != null">
user_id = #{userId}
</if>
<if test="amount != null">
and Amount = #{amount}
</if>
<if test="billDate != null">
and bill_date = #{billDate}
</if>
<if test="dueDate != null">
and due_date = #{dueDate}
</if>
<if test="isPaid != null">
and is_paid = #{isPaid}
</if>
</where>
</select>
<select id="mySelectById" resultType="com.goblin.BIbackend.model.entity.Bills">
select * from bills where id = #{id}
</select>
<insert id="myInsert" parameterType="com.goblin.BIbackend.model.entity.Bills">
insert into bills (user_id,Amount,bill_date,due_date,is_paid)
values (#{userId},#{amount},#{billDate},#{dueDate},#{isPaid})
</insert>
<delete id="myDeleteById" >
delete from bills where id = #{id}
</delete>
<select id="mySelectAll" resultMap="BaseResultMap">
select * from bills
</select>
<update id="myUpdate" parameterType="com.goblin.BIbackend.model.entity.Bills">
update bills where id = #{id}
</update>
</mapper>
2. 各部分
2.1 <mapper>根元素
<mapper namespace="com.goblin.BIbackend.mapper.BillsMapper">
<!-- 其他配置 -->
</mapper>
这个根元素定义了 XML 文件的命名空间,通常对应 Java 中的 Mapper 接口的完全限定名。这有助于区分不同的 Mapper 文件,防止 SQL 语句冲突。
2.2 <resultMap>
和字段映射
<resultMap id="BaseResultMap" type="com.goblin.BIbackend.model.entity.Bills">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="userId" column="user_id" jdbcType="BIGINT"/>
<result property="amount" column="Amount" jdbcType="FLOAT"/>
<result property="billDate" column="bill_date" jdbcType="DATE"/>
<result property="dueDate" column="due_date" jdbcType="DATE"/>
<result property="isPaid" column="is_paid" jdbcType="BIGINT"/>
</resultMap>
这里定义了一个名为 BaseResultMap
的结果映射,它将数据库查询结果映射到 com.goblin.BIbackend.model.entity.Bills
类的实例。
具体映射关系如下:
1. id属性对应数据库表的id列,数据类型为BIGINT。
2. userId属性对应数据库表的user_id列,数据类型为BIGINT。
3. amount属性对应数据库表的Amount列,数据类型为FLOAT。
4. billDate属性对应数据库表的bill_date列,数据类型为DATE。
5. dueDate属性对应数据库表的due_date列,数据类型为DATE。
6. isPaid属性对应数据库表的is_paid列,数据类型为BIGINT。
前面是实体类里面定义的名称,后面是数据库里面对应的字段名。
2.3 <sql>
片段
<sql id="Base_Column_List">
id, user_id, Amount, bill_date, due_date, is_paid
</sql>
id
: 为 SQL 片段定义一个标识符,可以在<select>
或其他元素中通过<include>
引用。- 内容: 列出了查询操作中需要的列名。这是为了避免在多个
<select>
元素中重复相同的列名列表。
2.4 增删改查SQL语句
<select id="list" resultType="com.goblin.BIbackend.model.entity.Bills">
select * from bills
<where>
<if test="userId != null">
user_id = #{userId}
</if>
<if test="amount != null">
and Amount = #{amount}
</if>
<if test="billDate != null">
and bill_date = #{billDate}
</if>
<if test="dueDate != null">
and due_date = #{dueDate}
</if>
<if test="isPaid != null">
and is_paid = #{isPaid}
</if>
</where>
</select>
<select id="mySelectById" resultType="com.goblin.BIbackend.model.entity.Bills">
select * from bills where id = #{id}
</select>
<insert id="myInsert" parameterType="com.goblin.BIbackend.model.entity.Bills">
insert into bills (user_id,Amount,bill_date,due_date,is_paid)
values (#{userId},#{amount},#{billDate},#{dueDate},#{isPaid})
</insert>
<delete id="myDeleteById" >
delete from bills where id = #{id}
</delete>
<update id="myUpdate" parameterType="com.goblin.BIbackend.model.entity.Bills">
update bills where id = #{id}
</update>
parameterType
和 resultType
在 SQL 映射文件中用于指定参数和结果的 Java 类型,但它们的作用和使用场景不同。 一定不能写错,否则测试接口的时候就错乱了。
2.4.1 resultType
- 作用:
resultType
用于指定 MyBatis 查询操作返回的结果类型。这个属性告诉 MyBatis 应该如何将查询结果集的每一行映射到 Java 对象。- 使用场景:通常用在
<select>
元素中,用于定义查询结果应该如何映射到 Java 类的实例。- 示例:
<select id="selectBlog" resultType="Blog"> select * from Blog </select>
在这个例子中,
resultType
指定了查询结果应该映射到Blog
类的实例。
2.4.2 parameterType
- 作用:
parameterType
用于指定 MyBatis 操作(如<insert>
、<update>
、<delete>
或带有参数的<select>
)中传入的参数类型。这个属性告诉 MyBatis 期待的参数对象的类型,MyBatis 会根据这个类型来自动映射方法参数和 SQL 语句中的占位符。- 使用场景:通常用在需要传入参数执行数据库操作的元素中,例如插入、更新或删除操作。
- 示例:
<select id="selectById" parameterType="java.util.Map" resultType="com.example.Blog"> select * from blog where id = #{id} </select>
在这个例子中,
parameterType
指定了方法的参数类型为java.util.Map
,这意味着你可以传递一个 Map 对象作为查询参数。
2.4.3 两者区别总结
- 方向:
parameterType
定义了进入操作的参数类型,而resultType
定义了从数据库查询返回的结果类型。- 使用位置:
parameterType
通常用在需要参数的 SQL 操作中,resultType
则用在查询操作中。- 映射方式:
parameterType
映射的是方法的参数到 SQL 语句的占位符,resultType
映射的是查询结果集到 Java 对象的属性。
2.4.5 占位符#{id}
在 SQL 映射文件中编写 SQL 语句时,可以使用
#{}
来引用 Java 方法的参数。<select id="selectBlog" resultType="Blog"> SELECT * FROM blog WHERE id = #{id} </select>
在这个例子中,
#{id}
就是一个占位符,它将被 MyBatis 替换为方法参数id
的值。参数替换
MyBatis 会根据方法的参数列表自动替换
#{}
中的内容。如果方法的参数是一个简单类型或 POJO(Plain Old Java Object),MyBatis 会根据参数的名称来替换占位符。预处理语句
使用
#{}
占位符的 SQL 语句会被 MyBatis 转换为预处理语句(PreparedStatement),这样可以提高性能并防止 SQL 注入攻击。类型处理
MyBatis 会根据参数的实际类型来设置 SQL 语句中的参数。例如,如果参数是
java.sql.Date
类型,MyBatis 会使用适当的 JDBC 方法来设置日期参数。占位符总结
#{}
占位符是 MyBatis 中实现参数化查询的关键机制,它允许开发者将 Java 方法的参数值安全、灵活地传递给 SQL 语句。通过使用#{}
,MyBatis 可以自动处理参数的类型转换和预处理语句的生成,从而提高数据库操作的效率和安全性。
推荐阅读
-
深入解析Linux中的mount命令:实现对Linux系统外文件的挂载
-
对Linux系统中的lsmod、lsof、lspci、lsscsi命令和实例进行深入解析
-
如何对巨大的JS文件进行混淆和加密操作?
-
对MySQL进行my.ini文件的配置
-
对ConcurrentHashMap的原理进行深入分析
-
深入理解Linux文件系统——RH033单元3的学习指南
-
使用Java流对MP4视频文件进行加密,并在H5中播放加密的视频流
-
HHKB BT 蓝牙键盘:装逼必备利器?" 使用HHKB BT 蓝牙键盘的心得体会 在互联网的世界里,越来越多的人都追求个性化和独特性,无论是穿着打扮,还是使用的电子产品,都能看到人们的创意和想法。最近我在网上看到了一款备受推崇的机械键盘——HHKB BT 蓝牙键盘。 HHKB BT 蓝牙键盘是一款由日本知名键盘制造商 Filco 生产的产品,被誉为程序员的信仰。其最大的特点就是采用了静电容轴,按键更加灵敏且耐用。此外,HHKB BT 蓝牙键盘还具有非常紧凑的设计,仅60个键位,非常适合长时间工作或编程的人士使用。 然而,当我收到 HHKB BT 蓝牙键盘并开始使用时,我发现了一些问题。首先,数字键整体向右移动了一位,这对于习惯使用数字键的人来说是一种困扰。其次,HHKB BT 蓝牙键盘的价格较高,并且只能通过代购购买,售后服务也不完善。 尽管存在这些问题,我还是对 HHKB BT 蓝牙键盘产生了浓厚的兴趣。我尝试将数字键重新排列,使其更适合我的使用习惯,同时我也开始学习如何更有效地使用 HHKB BT 蓝牙键盘进行编程。 总的来说,HHKB BT 蓝牙键盘是一款非常专业且高效的机械键盘,如果你是一位需要长时间使用键盘的开发者或者程序员,那么它绝对值得你考虑。当然,高昂的价格和售后服务的问题也需要你权衡一下是否值得投资。
-
【摩尔线程+Colossal-AI强强联手】MusaBert登上CLUE榜单TOP10:技术细节揭秘 - 技术实力:摩尔线程凭借"软硬兼备"的技术底蕴,让MusaBert得以从底层优化到顶层。其内置多功能GPU配备AI加速和并行计算模块,提供了全面的AI与科学计算支持,为AI推理和低资源条件下的大模型训练等场景带来了高效、经济且环保的算力。 - 算法层面亮点:依托Colossal-AI AI大模型开发系统,MusaBert在训练过程中展现出了卓越的并行性能与易用性,特别在预处理阶段对DataLoader进行了优化,适应低资源环境高效处理海量数据。同时,通过精细的建模优化、领域内数据增强以及Adan优化器等手段,挖掘和展示了预训练语言模型出色的语义理解潜力。基于MusaBert,摩尔线程自主研发的MusaSim通过对比学习方法微调,结合百万对标注数据,MusaSim在多个任务如语义相似度、意图识别和情绪分析中均表现出色。 - 数据资源丰富:MusaBert除了自家高质量语义相似数据外,还融合了悟道开源200GB数据、CLUE社区80GB数据,以及浪潮公司提供的1TB高质量数据,保证模型即便在较小规模下仍具备良好性能。 当前,MusaBert已成功应用于摩尔线程的智能客服与数字人项目,并广泛服务于语义相似度、情绪识别、阅读理解与声韵识别等领域。为了降低大模型开发和应用难度,MusaBert及其相关高质量模型代码已在Colossal-AI仓库开源,可快速训练优质中文BERT模型。同时,通过摩尔线程与潞晨科技的深度合作,仅需一张多功能GPU单卡便能高效训练MusaBert或更大规模的GPT2模型,显著降低预训练成本,进一步推动双方在低资源大模型训练领域的共享目标。 MusaBert荣登CLUE榜单TOP10,象征着摩尔线程与潞晨科技联合研发团队在中文预训练研究领域的领先地位。展望未来,双方将携手探索更大规模的自然语言模型研究,充分运用上游数据资源,产出更为强大的模型并开源。持续强化在摩尔线程多功能GPU上的大模型训练能力,特别是在消费级显卡等低资源环境下,致力于降低使用大模型训练的门槛与成本,推动人工智能更加普惠。而潞晨科技作为重要合作伙伴,将继续发挥关键作用。
-
在Python中,如何根据多个条件对目录下的文件名进行排序操作