Mybatis的 关联关系映射

重点:
0、上期回顾;
1、配置mybatis关联映射

重点解析:

0、回顾:

一、mybatis与ehcache的整合

1、导入相关依赖(ehcache、mybatis-ehcache的整合,spring-support)
2、spring-ehcache(cachemanagerfactory ehcache.xml cachemanager)
3、在spring-mybatis文件中的sqlsessionfactory中开启二级缓存
4、在*Mapper.xml中开启二级缓存

  • 4.1、默认可以缓存一条以及多条
  • 4.2、mybatis可以控制单个方法是否使用二级缓存 usercache=“false”

二、mybatis与redis的整合
1、导入相关依赖
2、spring-redis
2.1、需要注册redis.properties文件
2.2、配置redis的连接池
2.3、配置连接工厂
2.4、配置redistemplete
2.4.1、通过第三方类将redistemplete注入到实现了ibatis暴露在外部的cache接口的类
2.4.2、在*Mapper.xml中开启二级缓存

ps:注意点:
			1、spring不支持多文件注册,因该使用其他方式
			2、pom.xml中应该配置编译redis。properties文件
			3、将spring-redis添加到spring-Context.xml

1、配置mybatis关联映射

1、通过mybatis-generator插件生成dao、mapper、model

1)配置mybatis-generator插件生成文件位置
2)修改generatorConfig.xml配置文件的生成目录(mapper和model)及对应生成关系

3.修改Customer、Order实体类
1)实现序列化接口
2)建立实体映射关联关系(一对多、多对一)
#一对多:一个客户对应多个订单
private List orders=new ArrayList();

#多对一:多个订单对应一个客户(一个订单对应一个客户)
private Customer customer;

4.配置mybatis关联映射

4.1 一对多

 
        
        
        
        
        
            
            
        
    

   注意事项,使用左外连接而非内连接!!!

4.2 多对一

 
      
      
      
      
      
    
      
          
          
      
 

你可能感兴趣的:(IntelliJ,IDEA)