如何避免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
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
return employees;
}
public void setEmployees(List
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
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
return employees;
}
public void setEmployees(List
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://azwzsj.com/article/pihoig.html