首页 > 代码库 > mybatis处理一对一查询

mybatis处理一对一查询

1、使用嵌套结果方式

sql语句:select c.* ,t.t_name from class c,teacher t where c.teacher_id = t.t_id where c.c_id = #{id}  

<resultMap>

<id property="" column=""/>

<result property="" column=""/>

<association property="teacher" javaType="Teacher">

<association>

</resultMap>

2、使用嵌套查询方式

select * from class where c_id = #{id}

select * from teacher where t_id = #{teacher_id} //使用上一个查询的结果teacher_id

 

<resultMap>

<id property="" column=""/>

<result property="" column=""/>

<association property="teacher" column="teacher_id"  select="getTeacher">

<association>

</resultMap>

 

mybatis处理一对一查询