国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

mybatis延遲加載

2019-11-11 07:41:17
字體:
供稿:網(wǎng)友

問題出現(xiàn)

由于多表查詢相對于單表查詢是非常消耗時間的,所以就出現(xiàn)了延遲加載的方法,首先延遲加載,先從單表開始查詢,在查詢對應(yīng)多表的數(shù)據(jù),單表查詢速度比多表查詢塊,所以延遲加載是可以提升數(shù)據(jù)查詢速度。

延遲加載的實(shí)現(xiàn)

使用resultMap實(shí)現(xiàn)高級映射,也就是使用association或者collection

實(shí)現(xiàn)延遲加載需要開啟mybatis延遲加載的開關(guān),和關(guān)閉積極加載(也就是按需加載)lazyLoadingEnable:延遲加載開關(guān),默認(rèn)關(guān)閉,所以想要延遲加載必須開啟。aggressiveLazyLoading:積極加載,默認(rèn)開啟,所以需要關(guān)閉,關(guān)閉后就是按需求來進(jìn)行加載。

Mybatis配置文件加入

<settings>	<!-- 打開延遲加載開關(guān) -->	<setting name="lazyLoadingEnabled" value="true"/>	<!-- 關(guān)閉積極加載,就是按需加載 -->	<setting name="aggressiveLazyLoading" value="false"/></settings>

代碼實(shí)例

UserVo.java就是我們需要獲取的user信息和food信息,直接查詢必然會出現(xiàn)多表查詢這里我們具體實(shí)現(xiàn)就是先加載user信息,而food信息進(jìn)行延遲加載

package com.my.shiro.Entity;public class UserVo {		PRivate static final long serialVersionUID = 1L;	private String username;	private String passWord;	private String permission;		private Food food;	public String getUsername() {		return username;	}	public void setUsername(String username) {		this.username = username;	}	public String getPassword() {		return password;	}	public void setPassword(String password) {		this.password = password;	}	public String getPermission() {		return permission;	}	public void setPermission(String permission) {		this.permission = permission;	}	public Food getFood() {		return food;	}	public void setFood(Food food) {		this.food = food;	}	public static long getSerialversionuid() {		return serialVersionUID;	}	}

UserMapper.xml   resultMap實(shí)現(xiàn)延遲加載需要的配置

<resultMap id="LazyLoading" type="com.my.shiro.Entity.UserVo" >	    <id column="username" property="username" jdbcType="VARCHAR" />	    <result column="password" property="password" jdbcType="VARCHAR" />		<result column="permission" property="permission" jdbcType="VARCHAR" />				<!-- 用于延遲加載 			select中的statement的id:比如findpassword,如果不在同一個mapper中就應(yīng)該加上全限定名			colum關(guān)聯(lián)信息 -->		<association property="food" javaType="com.my.shiro.Entity.Food" 			select="com.my.shiro.Dao.FoodMapper.findFoodByUsername" 			column="username">		</association>		<!-- <collection property=""></collection> -->	</resultMap>  這里使用association實(shí)現(xiàn)延遲加載這里主要有4的屬性

property:這里的這個屬性對應(yīng)的是UserVo中的foodjavaType:延遲加載數(shù)據(jù)類型select:延遲加載需要使用的方法對應(yīng)的statement的id也就一個sql方法對應(yīng)的id,這里需要注意的是如果這個sql不再該mapper.xml中那么需要加上這個sql的namespacecolum:關(guān)聯(lián)信息,也就是我們通常多表關(guān)聯(lián)的信息

resultMap的使用

<select id="findUserAndFood" resultMap="LazyLoading">		select * from user;	</select>這里先會執(zhí)行select * from user 查詢到信息,再通過resultMap中配置的association來執(zhí)行findFoodByUsername對應(yīng)的sql,從而實(shí)現(xiàn)延遲加載findFoodByUsername

<select id="findFoodByUsername" parameterType="java.lang.String" resultType="com.my.shiro.Entity.Food">		select * 		from food 		where username = #{username}	</select>

這里的parameter可以看做resultMap中的association中的colum對應(yīng)的值,也就是關(guān)聯(lián)屬性在這里我遇到一個問題就是當(dāng)parametreType為sting的時候不可以寫成下面的格式

<where>	 <if test="username != null and username != ''">	    	and username = #{username}	 </if></where>

其實(shí)上面sql可以看作,這里避免了多表之間的查詢

select username,password,(select price from food where username = user.username)price,(select name from food where username = user.username)namefrom user至于collection使用法大體上是相似的


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新巴尔虎左旗| 厦门市| 高州市| 新余市| 曲松县| 遵义县| 普格县| 凭祥市| 温宿县| 浮梁县| 合阳县| 禹城市| 平山县| 开封县| 兰考县| 新河县| 科尔| 海原县| 宽甸| 仙桃市| 武城县| 堆龙德庆县| 龙胜| 南昌县| 长寿区| 虹口区| 青海省| 新昌县| 汉寿县| 安塞县| 临桂县| 赣州市| 岳普湖县| 绩溪县| 句容市| 龙口市| 钟山县| 嘉兴市| 云阳县| 大英县| 洪湖市|