避免ibatisN+1查询的方法

如果您在实体类工作的时候想要避免ibatisN+1查询,您可以参考如下代码。

从策划到设计制作,每一步都追求做到细腻,制作可持续发展的企业网站。为客户提供成都网站设计、成都网站建设、网站策划、网页设计、域名与空间、网页空间、网络营销、VI设计、 网站改版、漏洞修补等服务。为客户提供更好的一站式互联网解决方案,以客户的口碑塑造优易品牌,携手广大客户,共同发展进步。

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查询,相信能够为您的工作起到相关的帮助。

分享标题:避免ibatisN+1查询的方法
转载来于:http://www.shufengxianlan.com/qtweb/news22/556422.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联