溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MyBatis框架怎么通過xml映射文件實(shí)現(xiàn)查詢語句編寫

發(fā)布時(shí)間:2022-02-23 13:53:21 來源:億速云 閱讀:201 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)MyBatis框架怎么通過xml映射文件實(shí)現(xiàn)查詢語句編寫的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

什么是Mybatis框架?

MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設(shè)置參數(shù)以及獲取結(jié)果集。MyBatis 可以使用簡單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java對象)映射成數(shù)據(jù)庫中的記錄。

如何使用?

pom文件依賴

<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>2.1.3</version>
</dependency>

yml文件配置,這里匹配 resource/mapper/ 路徑下的映射文件。

mybatis:
  mapper-locations: classpath:mapper/*.xml

在xml文件中綁定持久層接口和實(shí)體對象

<?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.flamelephant.fabricmgt.dao.HostMapper">
 <resultMap type="com.flamelephant.fabricmgt.entity.po.Host" id="HostMap">
 <result property="id" column="id" jdbcType="INTEGER"/>
 <result property="hostName" column="host_name" jdbcType="VARCHAR"/>
 <result property="ip" column="ip" jdbcType="VARCHAR"/>
 <result property="userName" column="user_name" jdbcType="VARCHAR"/>
 <result property="passWord" column="pass_word" jdbcType="VARCHAR"/>
 <result property="state" column="state" jdbcType="OTHER"/>
 <result property="tag" column="tag" jdbcType="VARCHAR"/>
 <result property="gmtCreated" column="gmt_created" jdbcType="TIMESTAMP"/>
 <result property="gmtModified" column="gmt_modified" jdbcType="TIMESTAMP"/>
 </resultMap>
</mapper>
  • mapper標(biāo)簽中的namespace屬性指定的是我們持久層接口的項(xiàng)目路徑

  • resultMap是Mybatis最強(qiáng)大的元素,它可以將查詢到的復(fù)雜數(shù)據(jù)(比如查詢到幾個表中數(shù)據(jù))映射到一個結(jié)果集當(dāng)中

  • resultMap標(biāo)簽的type屬性是我們要映射的實(shí)體對象的項(xiàng)目路徑,id為resultMap的唯一標(biāo)識。

  • resultMap中的result標(biāo)簽是實(shí)體和數(shù)據(jù)庫表字段的綁定,其中property屬性為實(shí)體對象的屬性名,column為數(shù)據(jù)庫的字段名,jdbcType是字段的類型。

xml映射文件的sql編寫

通過實(shí)體作為篩選條件查詢

<select id="queryAll" resultMap="HostMap">
    select id,
           host_name,
           ip,
           user_name,
           pass_word,
           state,
           tag,
           gmt_created,
           gmt_modified
    from host
 <where>
    <if test="id != null and id != ''">
        and id = #{id}
    </if>
    <if test="hostName != null and hostName != ''">
        and host_name like CONCAT('%', #{hostName}, '%')
    </if>
    <if test="ip != null and ip != ''">
        and ip like CONCAT('%', #{ip}, '%')
    </if>
    <if test="userName != null and userName != ''">
        and user_name = #{userName}
    </if>
    <if test="passWord != null and passWord != ''">
        and pass_word = #{passWord}
    </if>
    <if test="state != null and state != ''">
        and state = #{state}
    </if>
    <if test="tag != null and tag != ''">
        and tag = #{tag}
    </if>
    <if test="gmtCreated != null">
        and gmt_created = #{gmtCreated}
    </if>
    <if test="gmtModified != null">
        and gmt_modified = #{gmtModified}
    /if>
 </where>
</select>
  • id="queryAll"為持久層接口的抽象方法名

  • resultMap="HostMap" 指定查詢結(jié)果接收的resultMap的結(jié)果集。

持久層接口綁定

/**
 * 條件查詢
 *
 * @param host 條件查詢
 * @return 對象列表
 */
List<Host> queryAll(Host host);

通過主鍵批量刪除

<!--通過主鍵批量刪除-->
<delete id="deleteHostByIds" parameterType="java.lang.Integer">
 delete
    from host
            where id in
    <if test="hostIds != null and hostIds.length > 0">
        <foreach item="id" collection="hostIds" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </if>
 </delete>

以上sql語句的原型為

delete from host where id in(1,2,3)

foreach標(biāo)簽中的屬性理解

  • collection屬性為接收的數(shù)據(jù)源

  • item為集合中的每一個元素

  • index :用于表示在迭代過程中,每次迭代到的位置

  • open :表示該語句以什么開始

  • separator :表示在迭代時(shí)數(shù)據(jù)以什么符號作為分隔符

  • close :表示以什么結(jié)束

持久層接口抽象方法

/**
 * 批量刪除主機(jī)
 *
 * @param hostIds 主機(jī)id數(shù)組
 * @return Integer
 */Integer deleteHostByIds(@Param("hostIds") Long[] hostIds);

批量新增

<!--批量增加-->
<insert id="addHostList">
 insert into host_and_group(host_group_id, host_id)
       values
    <foreach collection="hostGroupIdList" item="hostGroupId" index="index" separator=",">
            (#{hostGroupId}, #{hostId})
    </foreach>
</insert>

持久層接口方法

/**
 * 將多個主機(jī)添加至一個主機(jī)組
 *
 * @param request
 * @return Integer
 */Integer addHostList(HostAndGroupRequest request);

我是元素封裝在一個對象中,所以這個對象里有批量增加的元素,則直接可以傳一個對象。

感謝各位的閱讀!關(guān)于“MyBatis框架怎么通過xml映射文件實(shí)現(xiàn)查詢語句編寫”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI