网站做seo优化有什么优势,app设计欣赏,网站 建设 现状,互联网备案服务平台MyBatis的延迟加载#xff08;Lazy Loading#xff09;是一种优化技术#xff0c;用于在需要时才加载关联对象或集合#xff0c;从而提高性能和效率。以下是对MyBatis延迟加载的详细介绍#xff1a;
延迟加载的基本概念 延迟加载是指在第一次访问对象的属性时才加载该对象…MyBatis的延迟加载Lazy Loading是一种优化技术用于在需要时才加载关联对象或集合从而提高性能和效率。以下是对MyBatis延迟加载的详细介绍
延迟加载的基本概念 延迟加载是指在第一次访问对象的属性时才加载该对象的数据而不是在对象创建时就立即加载所有的数据。这样可以避免不必要的数据库访问减少系统的开销和提高性能。
mapper namespacecom.example.UserMapperresultMap iduserMap typecom.example.Userid propertyid columnid/result propertyusername columnusername/association propertyaddress columnaddress_id javaTypecom.example.Address fetchTypelazyid propertyid columnid/result propertycity columncity//association/resultMapselect idselectUser resultMapuserMapSELECT * FROM user WHERE id #{id}/select
/mapper
在上面的示例中User类中的address属性会被设置为延迟加载。
public class MyBatisExample {public static void main(String[] args) {SqlSessionFactory sqlSessionFactory MyBatisUtil.getSqlSessionFactory();try (SqlSession session sqlSessionFactory.openSession()) {UserMapper userMapper session.getMapper(UserMapper.class);User user userMapper.selectUser(1);System.out.println(user.getUsername()); // 此时不会加载addressSystem.out.println(user.getAddress().getCity()); // 访问address时才会加载address}}
} aggressiveLazyLoading属性: