使用mybatis框架对MySQL表进行关联映射查询XML配置
一对一关联映射
association元素
- 嵌套查询
<select id="id名" parameterType="id参数类型"
resultMap="外部resultMap名">
select * from 查询的主表 where id = #{id}
</select>
<resultMap type="查询表对应的实体类(路径.JOPO类)"
id="外部resultMap名">
<id property="主键属性名" column="对应表的主键名" />
<result property="其他类属性名" column="对应表的普通列名" />
···
<association property="查询表对应的实体类的对象属性" column="列名"
javaType="映射表对应的实体对象属性的类型(路径.JOPO类)"
select="引入嵌套查询的子SQL语句(路径.id名)" />
</resultMap>
<select id="被引入嵌套查询的子SQL语句 id名" parameterType="id参数类型"
resultType="查表对应的Java类">
select * from 映射表 where id = #{id}
</select>
- 嵌套结果
<select id="id名" parameterType="id参数类型"
resultMap="外部resultMap名">
select a.*,b.普通列名(对应result元素)
from 查询的主表 a,映射表 b
where a.id=b.id and a.id = #{id}
</select>
<resultMap type="查询表对应的实体类(路径.JOPO类)"
id="外部resultMap名">
<id