关联关系映射

回顾:
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>

猜你喜欢

转载自blog.csdn.net/qq_43169369/article/details/85238871
今日推荐