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是一個(gè)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文件的根元素,它有一個(gè)namespace屬性用于指定對應(yīng)的Mapper接口或命名空間。
<mapper namespace="cn.tedu.tea.admin.server.content.dao.persist.mapper.TagMapper"> </mapper>
定義SQL語句和對應(yīng)的操作:
: 用于執(zhí)行查詢操作。
: 用于執(zhí)行插入操作。
: 用于執(zhí)行更新操作。
: 用于執(zhí)行刪除操作。
每個(gè)標(biāo)簽有以下主要屬性:
id: SQL語句的唯一標(biāo)識符,可以通過這個(gè)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>
這個(gè)示例中,標(biāo)簽定義了一個(gè)查詢操作,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>
這個(gè)示例中,標(biāo)簽定義了一個(gè)插入操作,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>
這個(gè)示例中,標(biāo)簽定義了一個(gè)更新操作,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>
這個(gè)示例中,標(biāo)簽定義了一個(gè)刪除操作,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)文章
java中四種生成和解析XML文檔的方法詳解(介紹+優(yōu)缺點(diǎn)比較+示例)
本篇文章主要介紹了四種生成和解析XML文檔的方法,即:DOM、SAX、JDOM和DOM4J,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-11-11springboot2.3 整合mybatis-plus 高級功能及用法詳解
這篇文章主要介紹了springboot2.3 整合mybatis-plus 高級功能,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Spring?Security實(shí)現(xiàn)HTTP認(rèn)證
本文主要介紹了Spring?Security實(shí)現(xiàn)HTTP認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-06-06SpringBoot整合Groovy腳本實(shí)現(xiàn)動態(tài)編程詳解
這篇文章主要為大家介紹了SpringBoot整合Groovy腳本實(shí)現(xiàn)動態(tài)編程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09