Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement lazy loading of MyBatis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to achieve MyBatis lazy loading". In daily operation, I believe many people have doubts about how to achieve MyBatis lazy loading. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to achieve MyBatis lazy loading". Next, please follow the editor to study!

Lazy loading, also known as nested query

When you need to query the related information, using the Mybatis lazy loading feature can effectively reduce the pressure on the database. The first query only queries the main table information, and the related table information is loaded when the user gets it.

Lazy loading can be achieved by Mybatis one-to-one associated association and one-to-many collection. Use resultMap when lazy loading, not resultType.

Here we take the employee table and the department table as examples.

Associate with department table id through deptId

We first need to turn on a setting here.

Lazy loading is enabled by default, but here we also need to set this property. If it is not set, the delayed loading function will not be triggered.

Employee selectOneEmployee (int id)

Let's take querying individual employees as an example. ResultMap and sql are as follows

Select id,name,age,deptId from employee where id=# {id} select name from dept where id=# {deptId}

Here one-to-one, we use

Java Test:

Public static void main (String [] args) {SqlSession sqlSession= MybatisUtil.getSqlSession (); EmployeeDao mapper=sqlSession.getMapper (EmployeeDao.class); Employee employee = mapper.selectOneEmployee (3); System.out.println (employee); System.out.println (employee.getDept ()); sqlSession.commit (); / / commit transaction sqlSession.close (); / / close

Query results:

From the results, we can see that when we first output this employee object, the department is not queried, but when we need to use the department's information, the query will be triggered.

Query the department resultMap and sql as follows:

SELECT id,name FROM dept where id=# {id} select name from employee where deptId=# {id}

One-to-many, we use

That's all for lazy loading. Thank you for reading.

At this point, the study on "how to achieve MyBatis lazy loading" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report