mybatis框架的xml映射文件常用查詢指南
使用mybatis框架時,那必然會有對數(shù)據(jù)庫的查詢語句的編寫,所以這篇文章希望可以幫助到你。
什么是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文件中綁定持久層接口和實體對象
<?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屬性指定的是我們持久層接口的項目路徑
- resultMap是Mybatis最強大的元素,它可以將查詢到的復(fù)雜數(shù)據(jù)(比如查詢到幾個表中數(shù)據(jù))映射到一個結(jié)果集當(dāng)中
- resultMap標(biāo)簽的type屬性是我們要映射的實體對象的項目路徑,id為resultMap的唯一標(biāo)識。
- resultMap中的result標(biāo)簽是實體和數(shù)據(jù)庫表字段的綁定,其中property屬性為實體對象的屬性名,column為數(shù)據(jù)庫的字段名,jdbcType是字段的類型。
xml映射文件的sql編寫
通過實體作為篩選條件查詢
<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ù)據(jù)以什么符號作為分隔符
- close :表示以什么結(jié)束
持久層接口抽象方法
/**
* 批量刪除主機
*
* @param hostIds 主機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>
持久層接口方法
/** * 將多個主機添加至一個主機組 * * @param request * @return Integer */Integer addHostList(HostAndGroupRequest request);
我是元素封裝在一個對象中,所以這個對象里有批量增加的元素,則直接可以傳一個對象。
總結(jié)
到此這篇關(guān)于mybatis框架的xml映射文件常用查詢指南的文章就介紹到這了,更多相關(guān)mybatis xml映射文件查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于java ssm springboot+mybatis酒莊內(nèi)部管理系統(tǒng)設(shè)計和實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了java ssm springboot+mybatis實現(xiàn)酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
FastJson踩坑:@JsonField在反序列化時失效的解決
這篇文章主要介紹了FastJson踩坑:@JsonField在反序列化時失效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Springboot項目因為kackson版本問題啟動報錯解決方案
這篇文章主要介紹了Springboot項目因為kackson版本問題啟動報錯解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

