How can lazy loading be implemented in MyBatis?
In MyBatis, implementing lazy loading can be achieved by setting the lazyLoadingEnabled property to true. By default, MyBatis immediately loads associated objects when retrieving an object. However, setting the lazyLoadingEnabled property to true will delay loading associated objects until they are actually needed.
In addition, lazy loading can be achieved by using the fetchType=”lazy” attribute in the select element of the Mapper.xml configuration file. For example:
<select id="getUser" resultMap="userResultMap" fetchType="lazy">
SELECT * FROM user WHERE id = #{id}
</select>
After this configuration, when calling the getUser method to obtain user information, the associated objects will not be loaded immediately, and will only load when accessing the properties of the associated objects.
In general, lazy loading can be achieved by setting the lazyLoadingEnabled attribute to true or by using the fetchType=”lazy” attribute in the Mapper.xml configuration file.