mybatis 一对一关联对象查询
- <select id="getClassInfo" parameterType="int" resultMap="ClassResultMap">
- select * from class c,teacher t where c.teacher_id = t.t_id and c.c_id = #{id}
- </select>
- <!-- 解决字段名属性名不一致问题 -->
- <resultMap type="Classes" id="ClassResultMap">
- <id property="id" column="c_id"/>
- <result property="name" column="c_name"/>
- <!-- 一对一关联查询 -->
- <association property="teacher" column="teacher_id" javaType="Teacher">
- <id property="id" column="t_id"/>
- <result property="name" column="t_name"/>
- </association>
- </resultMap>
- <select id="getClassInfo2" parameterType="int" resultMap="ClassResultMap2" >
- select * from class where c_id = #{id}
- </select>
- <!-- 解决字段名属性名不一致问题 -->
- <resultMap type="Classes" id="ClassResultMap2">
- <id property="id" column="c_id"/>
- <result property="name" column="c_name"/>
- <association property="teacher" column="teacher_id"
- javaType="Teacher" select="getTeacher">
- </association>
- </resultMap>
- <select id="getTeacher" parameterType="int" resultType="Teacher">
- select t_id id,t_name name from teacher where t_id=#{id}
- </select>