1、Select_ResultMap
自定义结果集映射规则
1<!-- 指定主键列的封装规则 2 id:定义主键据说会有底层优化 3 colum:指定哪一列 4 property: 指定对应java的bean属性 5--> 6<resultMap type="com.demo.pojo.Employee" id="MyEmp"> 7 <id column="id" property="id"/> 8 <!-- 定义普通列封装属性 --> 9 <result column="last_name" property="lastName"/> 10</resultMap> 11<select id="" resultMap="MyEmp"> 12 select * from employee where id=#{id} 13</select>
关联查询_级联属性封装结果
1<!-- 2 场景一:(一对一) 3 查询Employee的同时查询员工对应的部门 4 --> 5<resultMap type="employee" id=""> 6 <id column="id" property="id"/> 7 <result column="did" property="dept.id"/> 8 <result column="deptName" property="dept.departmentName"/> 9</resultMap> 10<select id="" resultMap=""> 11 select * from employee e, department d where e.id = d.id 12</select>
关联查询_association定义封装对象规则
1<resultMap type="employee" id=""> 2 <id column="id" property="id"/> 3 <!-- association可以指定联合的JavaBean对象 4 property="dept":指定哪个属性是联合的对象 5 javaType:指定这个属性对象的类型[不能省略] 6 --> 7 <association property="dept" javaType="dept"> 8 <id column="id" property="id"/> 9 <result column="deptName" property="departmentName"/> 10 </association> 11</resultMap> 12<select id="" resultMap=""> 13 select * from employee e, department d where e.id = d.id 14</select>
关联查询_assocation分步查询
1<!-- 2 使用association进行分步查询 3 1、先按照员工id查询员工信息 4 2、根据查询员工信息中的id值去部门表查询出部门信息 5 3、部门设置到员工中 6--> 7<resultMap type="employee" id=""> 8 <id column="id" property="id"/> 9 <result column="email" property="email"/> 10 <result column="gender" property="gender"/> 11 <!-- 12 select:表明当前属性是调用select指定的方法查处的结果 13 column:指定将哪一列的值传给这个方法 14 --> 15 <assocation property="dept" select="com.demo.mapper.DeptMapper.getDeptById" column="d_id"> 16 </assocation> 17</resultMap> 18<select id="" resultMap=""> 19 select * from employee where id=#{id} 20</select>
关联查询__assocation_分步查询_延迟加载(懒加载/按需加载)
1<!-- 在Setting中显式指定,防止版本更替 --> 2<setting name="lazyLoadingEnable" value="true"></setting> 3<setting name="aggressiveLazyLoading" value="false"></setting>
关联查询_collection定义关联集合封装规则
1<!-- 2 场景二、(一对多) 3 查询部门的时候将部门对应的员工信息查询出来 4--> 5<resutMap type="department" id="de"> 6 <id column = "did" property="id"/> 7 <result column="deptName" property="departmentName"/> 8 <!-- collection定义关联集合类型的属性的封装规则 9 collection: 定义关联集合类型属性的封装规则 10 ofType: 指定集合里面元素的类型 11 --> 12 <collection property="emps" ofType="employee"> 13 <!-- 定义这个集合中元素的封装规则 --> 14 <id column="eid" property="id"/> 15 <result column="email" property="email"/> 16 </collection> 17</resutMap> 18<select id="" resultMap="de"> 19 select * from dept d left join employee e on d.id=e.id 20</select>
关联查询_collection分步查询
同上assocation分步查询方式
分步查询拓展
1<!-- 传递多列的值 2 将多列的值封装Map传递 3 key为需要接收参数的列,column为去传递参数的列 4 column="{key1=column1,key2=column2}" 5 fetchType="lazy":表示使用懒加载 6 -lazy:懒加载 7 -eager:立即查询 8-->
discriminator鉴别器
1<!-- 2 <discriminator javaType=""></discriminator> 3 鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为 4 5 封装Employee: 6 如果是女,则查出部门信息 7 如果是男,则将lastName赋值给email 8--> 9<resultMap type="employee" id=""> 10 <id column = "id" property="id"/> 11 <result column="lastName" property="lastName"/> 12 <result column="gender" property="gender"/> 13 <!-- 14 column: 指定要判断的列名 15 javaType: 列值对应的java类型 16 --> 17 <discriminator javaType="string" column="gender"> 18 <!-- 女生 --> 19 <case value="0" resulyType="employee"> 20 <assocation property="dept" select="com.demo.mapper.DeptMapper.getDeptById" column="d_id"></assocation> 21 </case> 22 <case value="1" resulyType="employee"> 23 <id column = "id" property="id"/> 24 <result column="lastName" property="email"/> 25 </case> 26 </discriminator> 27</resultMap>
回顾:Select_记录封装Map
多条记录封装一个map:Map<Integer,Employee>: 键是这条记录的主键,值是记录封装后的Java对象
告诉MyBatis封装这个map时使用哪个属性作为主键,使用注解标明
@MapKey("id")
------------------------------------> - 原创不易,转载请备注来源
