SQL映射文件
演示流程已在上一章给出,详情见 初识MyBatis-CSDN博客
1. SQL映射的XML文件
2. mapper元素
3. select元素
parameterType
resultType&resultMap
select小结
4. insert元素
5. update元素
6. delete元素
7. resultMap元素⭐含演示
association(一对一)
collection(一对多)
演示代码
assciation
实体对象中包含了另一个对象,此时用assciation
<resultMap id="baseResultMap" type="com.zb.entity.Person"><!--如果java属性和数据字段一样, 一样的配置可以不用写--><id column="id" property="id"/><result column="name" property="name"/><result column="password" property="password"/><result column="age" property="age"/><result column="address" property="address"/><result column="dept_id" property="deptId"/><!--映射部门对象中的属性--><association property="dept" javaType="com.zb.entity.Dept"><result column="dept_id" property="id"/><result column="dept_name" property="name"/></association></resultMap>
collection
实体类中包含了另一个对象的集合,此时用collection
<resultMap id="baseResultMap" type="com.zb.entity.Dept"><id column="dept_id" property="id" /><result column="dept_name" property="name" /><collection property="personList" javaType="com.zb.entity.Person"><result column="id" property="id"/><result column="name" property="name"/><result column="password" property="password"/><result column="age" property="age"/><result column="address" property="address"/><result column="dept_id" property="deptId"/></collection></resultMap>
8. 缓存
一级缓存(本地缓存)
一级缓存是 SqlSession 级别的缓存,在同一个 SqlSession 中执行的相同 SQL 查询会复用缓存结果。
- 特点:默认开启,无需配置。
- 失效场景:SqlSession 关闭或执行增删改操作。
二级缓存(全局缓存)
二级缓存是 mapper 级别的缓存,跨 SqlSession 共享。二级缓存多用与静态sql查询,静态参数一起使用
<!--设置二级缓存队列模式为FIFO,存储时长1min,大小512,只读(不许更改)--><cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>