如何避免ibatisN+1查詢

這篇文章將為大家詳細講解有關如何避免ibatisN+1查詢,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

目前創新互聯已為上千余家的企業提供了網站建設、域名、網站空間網站托管運營、企業網站設計、合肥網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發展。

Java代碼避免ibatisN+1查詢
多方:  
public class Employ {  
private int id;  
private String enployName;  
private int salary;  
private Department department;  

public Employ() {  
}  

public int getId() {  
return id;  
}  

public void setId(int id) {  
this.id = id;  
}  

public String getEnployName() {  
return enployName;  
}  

public void setEnployName(String enployName) {  
this.enployName = enployName;  
}  

public int getSalary() {  
return salary;  
}  

public void setSalary(int salary) {  
this.salary = salary;  
}  

public Department getDepartment() {  
return department;  
}  

public void setDepartment(Department department) {  
this.department = department;  
}  
}  

一方:  
public class Department {  
private int did;  
private String departmentName;  
private Listemployees;  


public int getDid() {  
return did;  
}  

public void setDid(int did) {  
this.did = did;  
}  

public String getDepartmentName() {  
return departmentName;  
}  

public void setDepartmentName(String departmentName) {  
this.departmentName = departmentName;  
}  

public ListgetEmployees() {  
return employees;  
}  

public void setEmployees(Listemployees) {  
this.employees = employees;  
}  

多方:
public class Employ {
private int id;
private String enployName;
private int salary;
private Department department;

public Employ() {
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getEnployName() {
return enployName;
}

public void setEnployName(String enployName) {
this.enployName = enployName;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
this.department = department;
}
}

一方:
public class Department {
private int did;
private String departmentName;
private Listemployees;


public int getDid() {
return did;
}

public void setDid(int did) {
this.did = did;
}

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}

public ListgetEmployees() {
return employees;
}

public void setEmployees(Listemployees) {
this.employees = employees;
}


如果您在映射文件的工作中想要避免ibatisN+1查詢,您可以參考如下代碼。

Xml代碼避免ibatisN+1查詢
多方:  
 
1. <?xml version="1.0" encoding="UTF-8" ?> 
2.  
3. <!DOCTYPE sqlMap       
4.     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"       
5.     "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 
6.  
7. <sqlMap namespace="Employ"> 
8.  
9.   <!-- Use type aliases to avoid typing the full classname every time. --> 
10.   <typeAlias alias="Employ" type="com.test.domain.Employ"/> 
11.  
12.   <!-- Result maps describe the mapping between the columns returned  
13.        from a query, and the class properties.  A result map isn't  
14.        necessary if the columns (or aliases) match to the properties  
15.        exactly. --> 
16.   <resultMap id="EmployResult" class="Employ"> 
17.     <result property="id" column="id"/> 
18.     <result property="enployName" column="employ_name"/> 
19.     <result property="salary" column="salary"/> 
20.     <result property="department.did" column="did"/> 
21.     <result property="department.departmentName" column="department_name"/> 
22.   </resultMap> 
23.  
24.   <!-- Select with no parameters using the result map for Account class. --> 
25.   <select id="selectAllEmploy" resultMap="EmployResult"> 
26.   <![CDATA[ 
27.   select * from employees e, departments d where e.departmentid = d.did 
28.   ]]> 
29.   </select> 
30.   <!-- A simpler select example without the result map.  Note the  
31.        aliases to match the properties of the target result class. --> 
32.     
33.   <!-- Insert example, using the Account parameter class --> 
34.   <insert id="insertEmploy" parameterClass="Employ"> 
35.   <![CDATA[ 
36.   insert into employees (employ_name, salary, departmentid) values(#enployName#, #salary#, #department.did#) 
37.   ]]> 
38.   </insert> 
39.  
40.   <!-- Update example, using the Account parameter class --> 
41.  
42.   <!-- Delete example, using an integer as the parameter class --> 
43. </sqlMap> 

一方:  
1. <?xml version="1.0" encoding="UTF-8" ?> 
2.  
3. <!DOCTYPE sqlMap       
4.     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"       
5.     "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 
6.  
7. <sqlMap namespace="Department"> 
8.  
9.   <!-- Use type aliases to avoid typing the full classname every time. --> 
10.   <typeAlias alias="Department" type="com.test.domain.Department"/> 
11.  
12.   <!-- Result maps describe the mapping between the columns returned  
13.        from a query, and the class properties.  A result map isn't  
14.        necessary if the columns (or aliases) match to the properties  
15.        exactly. --> 
16.   <resultMap id="DepartmentResult" class="Department"> 
17.     <result property="did" column="did"/> 
18.     <result property="departmentName" column="department_name"/> 
19.   </resultMap> 
20.  
21.   <!-- Select with no parameters using the result map for Account class. --> 
22.   <select id="selectDepartmentById" parameterClass="int" resultMap="DepartmentResult"> 
23.   <![CDATA[ 
24.   select * from departments where did = #did# 
25.   ]]> 
26.   </select> 
27.   <!-- A simpler select example without the result map.  Note the  
28.        aliases to match the properties of the target result class. --> 
29.     
30.   <!-- Insert example, using the Account parameter class --> 
31.   <insert id="insertDepartment" parameterClass="Department"> 
32.   <![CDATA[ 
33.   insert into departments (department_name) values(#departmentName#) 
34.   ]]> 
35.   </insert> 
36.  
37.   <!-- Update example, using the Account parameter class --> 
38.  
39.   <!-- Delete example, using an integer as the parameter class --> 
40. </sqlMap>

關于“如何避免ibatisN+1查詢”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

分享文章:如何避免ibatisN+1查詢
當前網址:http://m.kartarina.com/article6/pihoig.html

成都網站建設公司_創新互聯,為您提供手機網站建設網站維護服務器托管全網營銷推廣電子商務關鍵詞優化

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站建設網站維護公司
主站蜘蛛池模板: 激情射精爆插热吻无码视频| 免费无码又爽又高潮视频| 久久久久久无码Av成人影院| 无码一区二区三区老色鬼| 无码国产精品一区二区免费虚拟VR| 国产精品无码免费播放| 免费无码AV一区二区| 久久久无码人妻精品无码| 无码av天天av天天爽| 亚洲av无码国产精品色午夜字幕| 亚洲精品无码高潮喷水A片软| 亚洲精品无码成人片在线观看| 人妻无码视频一区二区三区| 国产网红主播无码精品 | 中文字幕丰满乱子无码视频| 少妇精品无码一区二区三区| 国产成人无码免费网站| 亚洲欧洲无码AV不卡在线| 中文字幕无码乱人伦| 狠狠久久精品中文字幕无码| 无码人妻丰满熟妇区96| 亚洲AV无码乱码在线观看富二代| 日韩精品无码Av一区二区| 亚洲AV无码资源在线观看| 人妻丰满熟妇AV无码区乱| 伊人久久综合精品无码AV专区| 一本久道中文无码字幕av| 亚洲熟妇av午夜无码不卡| 无码精品A∨在线观看中文| 中文字幕无码免费久久9一区9 | 好硬~好爽~别进去~动态图, 69式真人无码视频免 | 久久精品成人无码观看56| 久久精品无码一区二区日韩AV| 东京热HEYZO无码专区| 无码熟妇人妻在线视频| 成人免费无码视频在线网站 | 无码人妻一区二区三区在线水卜樱| 免费看成人AA片无码视频羞羞网 | 久久久无码人妻精品无码| 无码AV波多野结衣久久| 无码精品日韩中文字幕|