本文主要是介绍MyBatis常用XML语法详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《MyBatis常用XML语法详解》文章介绍了MyBatis常用XML语法,包括结果映射、查询语句、插入语句、更新语句、删除语句、动态SQL标签以及ehcache.xml文件的使用,感兴趣的朋友跟随小...
1、定义结果映射
<!--方式一:一对多 按结果嵌套查询-->
<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.po编程jo.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删除语句--&gjst; <delete id="deleteUserInfo" parameterType="int"> DELETE FROM jdbctest.userinfo WHERE UserID=#{UserID} </delete>
6、动态 SQL 标签
<!--IF标签-->
<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 标签-->
<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 标签-->
<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标签-->
<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标签 万能的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>
<!--当前Mapper.xml开启二级缓存-->
<cache eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/android>
<!--自定义缓存ehcache-->
<cache type="org.myBATis.caches.ehcache.EhcacheCache"/>7、ehcache.xml文件
<?xml version="1.0" encoding="UTF8"?> <ehcache xmlns:xsi="http://www.w3php.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <!-- diskStore:为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。参数解释如下: user.home – 用户主目录 user.dir – 用户当前工作目录 java.io.tmpdir – 默认临时文件路径 --> <diskStore path="java.io.tmpdir/Tmp_EhCache"/> <!-- defaultCache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。 --> <!-- name:缓存名称。 maxElementsInMemory:缓存最大数目 maxElementsOnDisk:硬盘最大缓存个数。 eternal:对象是否永久有效,一但设置了,timeout将不起作用。 overflowToDisk:是否保存到磁盘,当系统当机时 timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual MAChine. The default value is false. diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush:内存数量最大时是否清除。 memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。 FIFO,first in first out,这个是大家最熟的,先进先出。 LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。 LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。 --> <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>
到此这篇关于MyBatis常用XML语法的文章就介绍到这了,更多相关MyBatis XML语AURLbjJ法内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!
这篇关于MyBatis常用XML语法详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!