1. 程式人生 > >關聯關係對映

關聯關係對映

回顧:
mybatis與ehcache的整合
匯入相關依賴(ehcache、mybatis-ehcache的整合,spring-support)
spring-ehcache(cachemanagerfactory ehcache.xml cachemanager)
在spring-mybatis檔案中的sqlsessionfactory中開啟二級快取
在*Mapper.xml中開啟二級快取

		1、預設可以快取一條以及多條
		2、mybatis可以控制單個方法是否使用二級快取 usercache="false"

mybatis與redis的整合
	匯入相關依賴
	spring-redis
		需要註冊redis.properties檔案
		配置redis的連線池
		配置連線工廠
		配置redistemplete
		通過第三方類將redistemplete注入到實現了ibatis暴露在外部的cache介面的類
	在*Mapper.xml中開啟二級快取	<cache type="實現了ibatis暴露在外部的cache介面">
	
	注意點:
		1、spring不支援多檔案註冊,因該使用其他方式
		2、pom.xml中應該配置編譯redis。properties檔案
		3、將spring-redis新增到spring-Context.xml

1.將資料表匯入資料庫中

2.通過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 多對一



    <!-- 多對一的關係 -->
    <!-- property: 指的是屬性的值, javaType:指的是屬性的型別-->
    <association property="customer" javaType="Customer">
        <id column="customer_id" property="customerId"/>
        <result column="customer_name" property="customerName"/>
    </association>