亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何避免ibatisN+1查詢

發布時間:2021-12-07 10:28:40 來源:億速云 閱讀:156 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關如何避免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查詢”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

湘潭县| 积石山| 临泉县| 长治市| 辽中县| 建昌县| 理塘县| 河曲县| 芷江| 克山县| 乌兰县| 霍山县| 防城港市| 桑日县| 麦盖提县| 高邮市| 揭西县| 柯坪县| 沿河| 白城市| 江津市| 三门峡市| 深水埗区| 罗城| 富顺县| 鹤峰县| 长岭县| 凌源市| 仪征市| 鄯善县| 来安县| 新河县| 防城港市| 宁海县| 筠连县| 桐乡市| 察雅县| 夏津县| 桂林市| 库尔勒市| 九江市|