Mybatis映射文件詳解之mapper.xml文件
在Mybatis中,Mapper XML文件是用于定義SQL語句和Java方法之間映射關(guān)系的核心配置文件。通過這些文件,開發(fā)者可以將數(shù)據(jù)庫中的表與Java對象進(jìn)行映射,實(shí)現(xiàn)數(shù)據(jù)的持久化操作。本文將詳細(xì)介紹Mybatis映射文件的相關(guān)知識,包括其結(jié)構(gòu)、標(biāo)簽以及如何編寫和使用。
一、Mybatis映射文件概述
Mybatis是一個Java持久層框架,它提供了一種簡單易用的方式來訪問和操作數(shù)據(jù)庫。在Mybatis中,映射文件(Mapper XML)起到了至關(guān)重要的作用,它們定義了SQL語句與Java方法之間的映射關(guān)系。
二、映射文件的結(jié)構(gòu)
Mapper XML文件通常用于定義與數(shù)據(jù)庫交互的SQL語句和操作。它的基本結(jié)構(gòu)如下所示:
<?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="cn.tedu.tea.admin.server.content.dao.persist.mapper.TagMapper">
<!-- TagStandardVO getStandardById(Long id);-->
<select id="getStandardById" resultMap="StandardResultMap">
SELECT id,name,parent_id,enable,sort
FROM content_tag
WHERE id=#{id}
</select>
<resultMap id="StandardResultMap" type="cn.tedu.tea.admin.server.content.pojo.vo.TagStandardVO">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="parent_id" property="parentId"></result>
<result column="enable" property="enable"></result>
<result column="sort" property="sort"></result>
</resultMap>
</mapper>Mapper XML文件詳解
mapper元素是Mapper XML文件的根元素,它有一個namespace屬性用于指定對應(yīng)的Mapper接口或命名空間。
<mapper namespace="cn.tedu.tea.admin.server.content.dao.persist.mapper.TagMapper"> </mapper>
定義SQL語句和對應(yīng)的操作:
: 用于執(zhí)行查詢操作。
: 用于執(zhí)行插入操作。
: 用于執(zhí)行更新操作。
: 用于執(zhí)行刪除操作。
每個標(biāo)簽有以下主要屬性:
id: SQL語句的唯一標(biāo)識符,可以通過這個id在Java代碼中調(diào)用對應(yīng)的SQL語句。
parameterType: SQL語句的參數(shù)類型,指定了傳入SQL語句的參數(shù)類型。
resultType 或 resultMap: 如果是查詢操作,可以通過resultType指定返回結(jié)果的類型,或者使用resultMap自定義結(jié)果映射規(guī)則。
查詢操作
<select id="selectByType"
resultType="cn.tedu.baking.pojo.vo.ContentManagementVO">
SELECT c.id,
c.title,
c.img_url,
c.brief,
c.type,
cat.name categoryName,
c.view_count,
c.comment_count,
c.create_time
FROM t_content c
JOIN t_category cat ON c.category_id = cat.id
WHERE c.type = #{type}
AND c.create_by = #{id}
</select>這個示例中,標(biāo)簽定義了一個查詢操作,id為selectUserById,返回結(jié)果類型為User,SQL語句為SELECT * FROM users WHERE id = #{id}。#{id}是占位符,表示動態(tài)傳入的參數(shù)。
插入操作
<insert id="insert">
INSERT INTO t_content
VALUES (NULL, #{title}, #{imgUrl}, #{videoUrl},
#{content}, #{type}, 0, 0, #{createBy},
#{createTime}, null, null,
#{brief}, #{categoryId})
</insert>這個示例中,標(biāo)簽定義了一個插入操作,id為insertUser,參數(shù)類型為User,SQL語句為INSERT INTO users (username, password) VALUES (#{username}, #{password})。#{username}和#{password}分別表示User對象中的屬性。
更新操作
<update id="updateUser" parameterType="User">
UPDATE users SET username = #{username}, password = #{password} WHERE id = #{id}
</update>這個示例中,標(biāo)簽定義了一個更新操作,id為updateUser,參數(shù)類型為User,SQL語句為UPDATE users SET username = #{username}, password = #{password} WHERE id = #{id}。這里的#{id}、#{username}和#{password}都是User對象的屬性。
刪除操作
<delete id="deleteById">
DELETE
FROM t_content
WHERE id = #{id}
</delete>這個示例中,標(biāo)簽定義了一個刪除操作,id為deleteUser,SQL語句為DELETE FROM users WHERE id = #{id},其中#{id}是動態(tài)傳入的參數(shù)。
動態(tài)SQL
MyBatis支持使用、、、等標(biāo)簽來構(gòu)建動態(tài)SQL語句,根據(jù)條件動態(tài)生成SQL片段,提高SQL語句的靈活性和可重用性。
<update id="update">
UPDATE t_content
<set>
<if test="title!=null">title=#{title},</if>
<if test="imgUrl!=null">img_url=#{imgUrl},</if>
<if test="brief!=null">brief=#{brief},</if>
<if test="videoUrl!=null">video_url=#{videoUrl},</if>
<if test="type!=null">type=#{type},</if>
<if test="categoryId!=null">category_id=#{categoryId},</if>
<if test="viewCount!=null">view_count=#{viewCount},</if>
<if test="commentCount!=null">comment_count=#{commentCount},</if>
<if test="updateBy!=null">update_by=#{updateBy},</if>
<if test="updateTime!=null">update_time=#{updateTime},</if>
<if test="content!=null">content=#{content}</if>
</set>
WHERE id=#{id}
</update>結(jié)果映射
除了簡單的resultType屬性外,還可以使用標(biāo)簽自定義復(fù)雜的結(jié)果映射關(guān)系,將數(shù)據(jù)庫中的查詢結(jié)果映射到Java對象的屬性中。
<select id="getStandardById" resultMap="StandardResultMap">
SELECT id,name,parent_id,enable,sort
FROM content_tag
WHERE id=#{id}
</select>
<resultMap id="StandardResultMap" type="cn.tedu.tea.admin.server.content.pojo.vo.TagStandardVO">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="parent_id" property="parentId"></result>
<result column="enable" property="enable"></result>
<result column="sort" property="sort"></result>
</resultMap>到此這篇關(guān)于Mybatis映射文件詳解-mapper.xml文件的文章就介紹到這了,更多相關(guān)Mybatis mapper.xml文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java?Mybatis的初始化之Mapper.xml映射文件的詳解
- 解決Mybatis映射文件mapper.xml中的注釋問題
- mybatis中映射文件(mapper)中的使用規(guī)則
- mybatis整合spring實(shí)現(xiàn)開啟mapper.xml映射文件掃描
- myBatis的mapper映射文件之批量處理方式
- mybatis映射文件mapper.xml的具體寫法
- 解決Mybatis在IDEA中找不到mapper映射文件的問題
- Mybatis中Mapper映射文件使用詳解
- 詳解mybatis通過mapper接口加載映射文件
- MyBatis Mapper映射文件配置的實(shí)現(xiàn)
相關(guān)文章
SpringMVC異步處理操作(Callable和DeferredResult)
這篇文章主要介紹了SpringMVC異步處理操作(Callable和DeferredResult),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
springboot啟動報(bào)錯Failed to load class [java
這篇文章主要介紹了springboot啟動報(bào)錯Failed to load class [javax.servlet.Filter]的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2026-03-03
基于SpringBoot+FastExcel的百萬級數(shù)據(jù)導(dǎo)入導(dǎo)出完整方案
本文詳細(xì)介紹了在SpringBoot3.5.11+JDK17+MySQL環(huán)境下,使用FastExcel實(shí)現(xiàn)百萬級數(shù)據(jù)的導(dǎo)入和導(dǎo)出,內(nèi)容包括數(shù)據(jù)庫表設(shè)計(jì)、百萬測試數(shù)據(jù)生成、實(shí)體類與Mapper、配置文件、百萬數(shù)據(jù)導(dǎo)入、百萬數(shù)據(jù)導(dǎo)出、線程池、阻塞隊(duì)列、CountDownLatch的設(shè)計(jì)思路與原理,以及接口測試方法2026-03-03

