举例说明 有两张表 学生和成绩 students 和 scores
忽略Java对象 z注意 students对象中有个属性是scores对象
resultMap 常用的查询方法一:
查询标签需要注意 返回类型是resultMap 这里Mymap是我们定义的resultMap的id
<select id="" resultMap="Mymap1"></select>
定义resultMap
column:指定哪一列
property:指定对应的javaBean属性
<resultMap type="students" id="Mymap1">
<id column="id" property="id"/>
<result column="User_Name" property="userName"/>
<result column="gender" property="gender"/>
<result column="age" property="age"/>
<result column="scores_id" property="scores.id"/>
<result column="chinese" property="scores.chinese"/>
<result column="mathematics" property="scores.mathematics"/>
<result column="english" property="scores.english"/>
</resultMap>
resultMap 常用的查询方法二:
<select id="" resultMap="Mymap2"></select>
<resultMap type="students" id="Mymap2">
<id column="id" property="id"/>
<result column="User_Name" property="userName"/>
<result column="gender" property="gender"/>
<result column="age" property="age"/>
<!-- association可以指定联合的javaBean对象
property="":指定哪个属性是联合的对象
javaType:指定这个属性对象的类型[不能省略]
-->
<association property="scores" javaType="全路径scores">
<id column="scores_id" property="id"/>
<result column="chinese" property="chinese"/>
<result column="mathematics" property="mathematics"/>
<result column="english" property="english"/>
</association>
</resultMap>
分步查询 单条查询
分步查询首先设置mybits的两个属性
开启懒加载模式
<setting name="lazyLoadingEnabled" value="true"/>
和按需加载
<setting name="aggressiveLazyLoading" value="false"/>
select属性填写xml的namespace和那个select标签的id
column查询传入的值
<association property="xxx"
select="xxx.ccc"
column="id">
</association>
分步查询 集合查询
<resultMap type="com.luohp.mybatis.bean.Deptno" id="result_list" extends="result_map">
<!--
collection:嵌套结果集方式,返回一个集合
ofType:指定集合元素的类型
-->
<collection property="empList" ofType="Employee">
<id column="eid" property="id"/>
<result column="lastName" property="last_name"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</collection>
</resultMap>
<select id="findDeptAndEmpList" resultMap="result_list">
select
d.id,d.dept_name ,e.id eid,e.last_name lastName,e.gender gender,e.email
from
tbl_deptno d left join tbl_employee e on d.id=e.dept_id
where
d.id=#{id}
</select>
<resultMap type="com.luohp.mybatis.bean.Deptno" id="result_list_setp" extends="result_map">
<!--
collection:嵌套结果集方式,返回一个集合
select:分步查询
column:将id列的值作为参数传给EmployeeMapper接口的findByDeptId方法,然后将返回的员工集合,赋值给empList
-->
<collection property="empList"
column="id" select="com.luohp.mybatis.dao.EmployeeMapper.findByDeptId">
</collection>
</resultMap>
<select id="findDeptAndEmpList2" resultMap="result_list_setp">
select d.id id,d.dept_name from tbl_deptno d where d.id=#{id}
</select>