MyBatis常用XML語法詳解
更新時間:2025年11月11日 11:04:25 作者:眾俗
文章介紹了MyBatis常用XML語法,包括結(jié)果映射、查詢語句、插入語句、更新語句、刪除語句、動態(tài)SQL標(biāo)簽以及ehcache.xml文件的使用,感興趣的朋友跟隨小編一起看看吧
1、定義結(jié)果映射
<!--方式一:一對多 按結(jié)果嵌套查詢-->
<select id="getTeacher" resultMap="TeacherStudent">
SELECT t.TID, t.TNAME,t.TDEPT,t.TJNAME,s.Pid, s.Pname
FROM jdbctest.teacher t, jdbctest.person s
where s.TID=t.TID and t.TID=#{tid}
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result column="TID" property="TID"/>
<result column="TNAME" property="TNAME"/>
<result column="TJNAME" property="TJNAME"/>
<result column="TDEPT" property="TDEPT"/>
<!--javaType 指定屬性的類型 集合中的泛型信息,使用ofType獲取-->
<collection property="personList" ofType="Person">
<result property="Pid" column="Pid"/>
<result property="Pname" column="Pname"/>
<result property="TID" column="TID"/>
</collection>
</resultMap>
<!--方式二:一對多 -->
<select id="getTeacher2" resultMap="TeacherPerson2">
SELECT * FROM jdbctest.teacher where TID=#{tid}
</select>
<resultMap id="TeacherPerson2" type="Teacher">
<result column="TID" property="TID"/>
<collection property="personList" javaType="ArrayList" ofType="Person" select="getPersonByTID" column="TID"/>
</resultMap>2、查詢語句
<!--select查詢語句-->
<select id="getUserList" resultType="UserZJ">
SELECT UserID, UserName, UserPwd FROM jdbctest.userinfo
</select>
<select id="getUserInfoByUserID" parameterType="int" resultType="com.user.pojo.UserInfo">
select * from jdbctest.userinfo where UserID=#{userId}
</select>
<select id="getUserInfoByUserNameLike" parameterType="String" resultType="com.user.pojo.UserInfo">
select * from jdbctest.userinfo where UserName like "%"#{userName}"%"
</select>3、插入語句
<!--insert插入語句,對象中的屬性可以直接取出來-->
<insert id="addUserInfo" parameterType="com.user.pojo.UserInfo">
INSERT INTO jdbctest.userinfo
(UserID, UserName, UserPwd)
VALUES(#{UserID}, #{UserName}, #{UserPwd})
</insert>
<!--Map的使用,傳遞map的key-->
<insert id="addUserInfo2" parameterType="map">
INSERT INTO jdbctest.userinfo
(UserID, UserName, UserPwd)
VALUES(#{id}, #{name}, #{pwd})
</insert>4、更新語句
<!--update修改語句-->
<update id="updateUserInfo" parameterType="com.user.pojo.UserInfo">
UPDATE jdbctest.userinfo
SET UserName=#{UserName}, UserPwd=#{UserPwd}
WHERE UserID=#{UserID};
</update>5、刪除語句
<!--delete刪除語句-->
<delete id="deleteUserInfo" parameterType="int">
DELETE FROM jdbctest.userinfo
WHERE UserID=#{UserID}
</delete>6、動態(tài) SQL 標(biāo)簽
<!--IF標(biāo)簽-->
<select id="getBlogIF" parameterType="map" resultType="blog">
SELECT Id, Name, Context
FROM jdbctest.blog where 1=1
<if test="Id !=null">
and Id = #{Id}
</if>
<if test="Name !=null">
and Name = #{Name}
</if>
<if test="Context !=null">
and Context = #{Context}
</if>
</select>
<!--sql where include 標(biāo)簽-->
<select id="getBlogWhere" parameterType="map" resultType="blog">
SELECT Id, Name, Context
FROM jdbctest.blog
<where>
<include refid="if-where-context"></include>
</where>
</select>
<sql id="if-where-context">
<if test="Name !=null">
Name = #{Name}
</if>
<if test="Context !=null">
and Context = #{Context}
</if>
</sql>
<!--choose when otherwise 標(biāo)簽-->
<select id="getBlogChoose" parameterType="map" resultType="blog">
SELECT Id, Name, Context
FROM jdbctest.blog
<where>
<choose>
<when test="Name !=null">
Name = #{Name}
</when>
<otherwise>
and Context = #{Context}
</otherwise>
</choose>
</where>
</select>
<!--set標(biāo)簽-->
<update id="updateBlog" parameterType="map">
update jdbctest.blog
<set>
<if test="Name != null">
Name= #{Name},
</if>
<if test="Context != null">
Context= #{Context}
</if>
</set>
where Id=#{Id}
</update>
<!--foreach標(biāo)簽 萬能的map,可以存一個集合-->
<select id="getBlogForeach" parameterType="map" resultType="blog">
SELECT Id, Name, Context
FROM jdbctest.blog
<where>
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>
<!--當(dāng)前Mapper.xml開啟二級緩存-->
<cache eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
<!--自定義緩存ehcache-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>7、ehcache.xml文件
<?xml version="1.0" encoding="UTF8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<!--
diskStore:為緩存路徑,ehcache分為內(nèi)存和磁盤兩級,此屬性定義磁盤的緩存位置。參數(shù)解釋如下:
user.home – 用戶主目錄
user.dir – 用戶當(dāng)前工作目錄
java.io.tmpdir – 默認臨時文件路徑
-->
<diskStore path="java.io.tmpdir/Tmp_EhCache"/>
<!--
defaultCache:默認緩存策略,當(dāng)ehcache找不到定義的緩存時,則使用這個緩存策略。只能定義一個。
-->
<!--
name:緩存名稱。
maxElementsInMemory:緩存最大數(shù)目
maxElementsOnDisk:硬盤最大緩存?zhèn)€數(shù)。
eternal:對象是否永久有效,一但設(shè)置了,timeout將不起作用。
overflowToDisk:是否保存到磁盤,當(dāng)系統(tǒng)當(dāng)機時
timeToIdleSeconds:設(shè)置對象在失效前的允許閑置時間(單位:秒)。僅當(dāng)eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大。
timeToLiveSeconds:設(shè)置對象在失效前允許存活時間(單位:秒)。最大時間介于創(chuàng)建時間和失效時間之間。僅當(dāng)eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。
diskPersistent:是否緩存虛擬機重啟期數(shù)據(jù) Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskSpoolBufferSizeMB:這個參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。默認是30MB。每個Cache都應(yīng)該有自己的一個緩沖區(qū)。
diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。
memoryStoreEvictionPolicy:當(dāng)達到maxElementsInMemory限制時,Ehcache將會根據(jù)指定的策略去清理內(nèi)存。默認策略是LRU(最近最少使用)。你可以設(shè)置為FIFO(先進先出)或是LFU(較少使用)。
clearOnFlush:內(nèi)存數(shù)量最大時是否清除。
memoryStoreEvictionPolicy:可選策略有:LRU(最近最少使用,默認策略)、FIFO(先進先出)、LFU(最少訪問次數(shù))。
FIFO,first in first out,這個是大家最熟的,先進先出。
LFU, Less Frequently Used,就是上面例子中使用的策略,直白一點就是講一直以來最少被使用的。如上面所講,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存。
LRU,Least Recently Used,最近最少使用的,緩存的元素有一個時間戳,當(dāng)緩存容量滿了,而又需要騰出地方來緩存新的元素的時候,那么現(xiàn)有緩存元素中時間戳離當(dāng)前時間最遠的元素將被清出緩存。
-->
<defaultCache
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="259200"
memoryStoreEvictionPolicy="LRU"/>
<cache name="cloud_user"
eternal="false"
maxElementsInMemory="5000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>到此這篇關(guān)于MyBatis常用XML語法的文章就介紹到這了,更多相關(guān)MyBatis XML語法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringKafka消息發(fā)布之KafkaTemplate與事務(wù)支持功能
通過本文介紹的基本用法、序列化選項、事務(wù)支持、錯誤處理和性能優(yōu)化技術(shù),開發(fā)者可以構(gòu)建高效可靠的Kafka消息發(fā)布系統(tǒng),事務(wù)支持特性尤為重要,它確保了在分布式環(huán)境中的數(shù)據(jù)一致性,感興趣的朋友一起看看吧2025-04-04
java8中parallelStream性能測試及結(jié)果分析
本篇文章給大家用代碼實例做了segmentfaultjava8中parallelStream性能測試,并對測試結(jié)果做了說明,需要的朋友學(xué)習(xí)下吧。2018-01-01
小伙熬夜用Java重現(xiàn)經(jīng)典超級馬里奧代碼實例
這篇文章主要介紹了Java重現(xiàn)經(jīng)典超級馬里奧,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
MyBatis環(huán)境資源配置實現(xiàn)代碼詳解
這篇文章主要介紹了MyBatis環(huán)境資源配置實現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
SpringBoot?整合?Spring-Session?實現(xiàn)分布式會話項目實戰(zhàn)
本文主要介紹了SpringBoot?整合?Spring-Session?實現(xiàn)分布式會話項目實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

