避免ibatisN+1查询的方法

开发 后端
我们在使用ibatis的时候经常会遇到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查询,相信能够为您的工作起到相关的帮助。

 

【编辑推荐】

  1. 实例说明ibatis动态查询
  2. ibatis标签详解
  3. ibatis插件的安装方式
  4. ibatis下加入c3p0连接池的方法
  5. ibatis也能用proxool连接池
责任编辑:桑丘 来源: java developer小亭的blog
相关推荐

2009-01-20 10:51:00

局域网IP地址分配

2015-03-10 13:50:42

smartycss语法

2022-12-07 11:24:51

首席信息官IT

2020-09-01 09:56:26

云端云计算云服务

2020-12-04 07:51:24

CQRS模型查询

2022-12-29 08:46:15

IT采购投资

2023-04-06 09:41:00

React 组件重渲染

2024-06-24 08:33:06

2019-12-03 18:51:36

SQL数据库MySQL

2024-10-31 16:31:16

2011-09-16 14:53:55

WLAN无线干扰

2023-11-30 22:25:40

云计算云原生

2019-07-28 20:49:37

回表查询索引覆盖MySQL

2024-09-23 20:55:04

2016-02-23 09:23:50

swift陷阱解决方法

2022-04-25 17:49:05

云计算云安全安全

2013-08-15 09:47:07

云迁移云技术

2020-10-10 11:02:09

Linux 系统 数据

2024-03-01 19:47:27

SQL数据库

2020-08-03 10:13:29

CIO项目管理技术
点赞
收藏

51CTO技术栈公众号