<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
如果使用xml的方式进行接口映射
namespace的值必须是接口的全路径
-->
<mapper namespace="com.et.mybatis.resultMapping.ResultMapper">
<!-- 多对一的映射 -->
<resultMap type="com.et.mybatis.resultMapping.Arctice" id="arctVar">
<result column="aid" property="id"/>
<result column="title" property="title"/>
<result column="content" property="content"/>
<!--
association应用于 多对一的映射
也就是说 如果一个类中存在一个对象 如果需要将select语句查询的列 映射到该对象中 就需要使用association标签
property 指定的 arctice对象中的一个对象属性名称 javaType表示你需要指定他的数据类型
-->
<association property="user" javaType="com.et.mybatis.resultMapping.UserInf">
<result column="uid" property="id"/>
<result column="name" property="name"/>
</association>
</resultMap>
<!-- 查询的时候 如果需要进行特殊的映射 必须使用 resultMap 引用自定义的映射类型-->
<select id="selectArcticle" resultMap="arctVar">
select a.id as aid,u.id as uid,a.title,a.content,u.name from arcticle a inner join userinfo u on
a.userid=u.id;
</select>
<!-- 一对多映射 在一的一方 会存在一个集合 应该对集合的数据进行规则的匹配 -->
<resultMap type="com.et.mybatis.resultMapping.UserInf" id="userVar">
<result column="id" property="id"/>
<result column="name" property="name"/>
<!--
因为集合的数据 是多条数据返回 所以需要将当前对象产生的主键 当成 另外一个查询的条件
column 表示将查询当前对象的列的数据 当做另一个查询的条件传入
select 表示需要查询的集合的select声明
autoMapping=true表示数据库的列自动匹配到对应的属性上
-->
<collection property="arcs" column="id"
select="com.et.mybatis.resultMapping.ResultMapper.selectArcticleByUserId" autoMapping="true">
<!--select调用必须加上命名空间-->
</collection>
</resultMap>
<!-- 使用 resultMap来指定映射规则-->
<select id="selectUserInfo" resultMap="userVar">
select * from userinfo where id=#{id}
</select>
<select id="selectArcticleByUserId" resultType="com.et.mybatis.resultMapping.Arctice" >
select * from arcticle where userid=#{userid}
</select>
</mapper>
mybatis多表之间的查询resultMap
最新推荐文章于 2024-05-10 14:07:14 发布