自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

避免ibatisN+1查詢的方法

開(kāi)發(fā) 后端
我們?cè)谑褂胕batis的時(shí)候經(jīng)常會(huì)遇到IbatisN+1查詢的情況,為了讓我們的工作更有效率,很多人希望能夠盡可能的避免ibatisN+1查詢。這篇文章就來(lái)為您講述如何避免ibatisN+1查詢。您再遇到ibatisN+1查詢的時(shí)候可以參考這些方法來(lái)避免ibatisN+1查詢。

如果您在實(shí)體類工作的時(shí)候想要避免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 List employees;  
 
 
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 List getEmployees() {  
return employees;  
}  
 
public void setEmployees(List employees) {  
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 List employees;


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 List getEmployees() {
return employees;
}

public void setEmployees(List employees) {
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>

通過(guò)以上的代碼,您可以有效地避免ibatisN+1查詢,相信能夠?yàn)槟墓ぷ髌鸬较嚓P(guān)的幫助。

 

【編輯推薦】

  1. 實(shí)例說(shuō)明ibatis動(dòng)態(tài)查詢
  2. ibatis標(biāo)簽詳解
  3. ibatis插件的安裝方式
  4. ibatis下加入c3p0連接池的方法
  5. ibatis也能用proxool連接池
責(zé)任編輯:桑丘 來(lái)源: java developer小亭的blog
相關(guān)推薦

2009-01-20 10:51:00

局域網(wǎng)IP地址分配

2015-03-10 13:50:42

smartycss語(yǔ)法

2022-12-07 11:24:51

首席信息官IT

2020-09-01 09:56:26

云端云計(jì)算云服務(wù)

2022-12-29 08:46:15

IT采購(gòu)投資

2023-04-06 09:41:00

React 組件重渲染

2020-12-04 07:51:24

CQRS模型查詢

2024-06-24 08:33:06

2023-11-30 22:25:40

云計(jì)算云原生

2011-09-16 14:53:55

WLAN無(wú)線干擾

2024-10-31 16:31:16

2019-12-03 18:51:36

SQL數(shù)據(jù)庫(kù)MySQL

2019-07-28 20:49:37

回表查詢索引覆蓋MySQL

2024-09-23 20:55:04

2016-02-23 09:23:50

swift陷阱解決方法

2013-08-15 09:47:07

云遷移云技術(shù)

2020-10-10 11:02:09

Linux 系統(tǒng) 數(shù)據(jù)

2022-04-25 17:49:05

云計(jì)算云安全安全

2024-03-01 19:47:27

SQL數(shù)據(jù)庫(kù)

2020-08-03 10:13:29

CIO項(xiàng)目管理技術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)