成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

避免ibatisN+1查詢的方法

開發(fā) 后端
我們在使用ibatis的時候經(jīng)常會遇到IbatisN+1查詢的情況,為了讓我們的工作更有效率,很多人希望能夠盡可能的避免ibatisN+1查詢。這篇文章就來為您講述如何避免ibatisN+1查詢。您再遇到ibatisN+1查詢的時候可以參考這些方法來避免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 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>

通過以上的代碼,您可以有效地避免ibatisN+1查詢,相信能夠為您的工作起到相關(guān)的幫助。

 

【編輯推薦】

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

2009-01-20 10:51:00

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

2020-09-01 09:56:26

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

2022-12-07 11:24:51

首席信息官IT

2015-03-10 13:50:42

smartycss語法

2024-06-24 08:33:06

2020-12-04 07:51:24

CQRS模型查詢

2023-04-06 09:41:00

React 組件重渲染

2022-12-29 08:46:15

IT采購投資

2023-11-30 22:25:40

云計算云原生

2011-09-16 14:53:55

WLAN無線干擾

2024-10-31 16:31:16

2019-12-03 18:51:36

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

2024-09-23 20:55:04

2019-07-28 20:49:37

回表查詢索引覆蓋MySQL

2016-02-23 09:23:50

swift陷阱解決方法

2022-04-25 17:49:05

云計算云安全安全

2013-08-15 09:47:07

云遷移云技術(shù)

2020-10-10 11:02:09

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

2024-03-01 19:47:27

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

2017-02-24 15:28:33

Android內(nèi)存溢出方法總結(jié)
點贊
收藏

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

主站蜘蛛池模板: 午夜精品久久久久久久久久久久 | 九九国产在线观看 | 欧美精品一区二区蜜桃 | 国产一区亚洲 | 婷婷开心激情综合五月天 | 97视频免费 | 成人福利视频 | 国产一区二区三区在线 | 国产一区二区三区久久 | 欧美一级免费看 | 久久99深爱久久99精品 | 高清黄色| 91久久北条麻妃一区二区三区 | 欧美成人精品一区二区三区 | 国产高清在线精品 | 69性欧美高清影院 | 亚洲精品视频在线看 | 国产激情小视频 | 成人午夜av | 久久精品成人热国产成 | 天堂一区| 天天欧美 | 欧美极品在线观看 | 日日摸日日碰夜夜爽2015电影 | 久久久无码精品亚洲日韩按摩 | 国产高清在线视频 | 欧美lesbianxxxxhd视频社区 | 亚洲精品久久久久久久久久久久久 | 国产精品免费一区二区三区 | 中文字幕电影在线观看 | 久久久久亚洲 | 国产一区二区三区 | 精品国产一区二区国模嫣然 | 国产成人99久久亚洲综合精品 | 国产精品欧美一区二区三区不卡 | 狠狠综合久久av一区二区老牛 | 日本在线播放 | 中文成人在线 | 国产黄色精品在线观看 | 欧美视频精品 | 超碰在线97国产 |