App下載

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

猿友 2021-07-21 14:26:15 瀏覽數(shù) (2412)
反饋

SSM 框架中 MyBatis 框架在使用的時(shí)候,必不可少要和數(shù)據(jù)庫進(jìn)行交互,也就難免需要對數(shù)據(jù)庫的查詢語句進(jìn)行編寫。本篇文章將為您簡要介紹 MyBatis 框架怎么通過 xml 文件來實(shí)現(xiàn)對數(shù)據(jù)庫的查詢語句編寫。

什么是Mybatis框架?

MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動(dòng)設(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ù)(比如查詢到幾個(gè)表中數(shù)據(jù))映射到一個(gè)結(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為集合中的每一個(gè)元素
  • 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>

持久層接口方法

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

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

總結(jié)

到此這篇關(guān)于 MyBatis 框架通過 xml 映射文件實(shí)現(xiàn)數(shù)據(jù)庫插敘語句編寫的文章就介紹到這了,更多相關(guān) MyBatis 框架的其他內(nèi)容請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持!


0 人點(diǎn)贊