Get the App
SLTechnology News&Howtos  ›  Development  › 

How to avoid ibatisN+1 query

Shulou Source: shulou.com Published: 2022-06-02 07:42:19 09月28日 Update

This article will give you a detailed explanation on how to avoid ibatisN+1 queries. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Java code avoids ibatisN+1 queries

Multi-party:

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

}

}

One party:

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

}

}

Multi-party:

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

}

}

One party:

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

}

}

If you want to avoid ibatisN+1 queries while working on mapping files, you can refer to the following code.

Xml code avoids ibatisN+1 queries

Multi-party:

1.

two。

3.

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.

10. < typeAlias alias= "Employ" type= "com.test.domain.Employ" / >

11.

12.

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" / >

twenty-two。 < / resultMap >

23.

24.

25. < select id= "selectAllEmploy" resultMap= "EmployResult" >

twenty-six。

twenty-seven。 Select * from employees e, departments d where e.departmentid = d.did

twenty-eight。 ]] >

twenty-nine。 < / select >

thirty。

thirty-one。 Aliases to match the properties of the target result class. -- >

thirty-two。

thirty-three。

Tags: Query side code article more UTF-8 reference good practical content file article knowledge work help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn MySQL Shulou Tech Info Apple