Mybatis连3表查询数据resultMap结果映射
Mybatis实现了sql与java代码的分离,达到了解耦合的⽬的,配置sql语句时有个resultType=""的属性,⽤于定义sql查询返回结果数据类型,但它有局限性,就是当连表查询的时候,你很难说定义返回的是某⼀个类型,这时就需要⽤到⼀个标签了,那就是resultMap,结果映射.
在深⼊ResultMap标签前,我们需要了解从SQL查询结果集到JavaBean或POJO实体的过程。
1. 通过JDBC查询得到ResultSet对象
2. 遍历ResultSet对象并将每⾏数据暂存到HashMap实例中,以结果集的字段名或字段别名为键,以字段值为值
3. 根据ResultMap标签的type属性通过反射实例化领域模型
4. 根据ResultMap标签的type属性和id、result等标签信息将HashMap中的键值对,填充到领域模型实例中并返回
id属性:标识resultMap,通过它去识别.
type属性:返回值类型,类的全定向名.
autoMapping属性:值为true(默认)|false,是否⾃动映射。⾃动映射功能就是⾃动查与字段名⼩写同名的属性名,并调⽤setter⽅法。⽽设置为false后,则需要在`resultMap`内明确注明映射关系才会调⽤对应的setter⽅法。
在讲标签时先讲述下数据库中表数据的对应关系,如⼀对⼀,⼀对多,多对多等关系,具体来说,如有⼀个俱乐部表,⼀个全员表,⼀个俱乐部⾥有许多球员,⽽许多球员对应⼀个俱乐部,则俱乐部与球员之间的关系就是⼀对多与多对⼀的关系。
<collection>⼦标签:对应表格关系中的多
<association>⼦标签:对应表格关系中的⼀
⽰例是联结三表,查询结果作为⽰范,就算联结再多表也能举⼀反三。这三个表的关系如下图
sql语句联结:download.csdn/detail/sunrise_zhu/9687991
核⼼代码:
2. <resultMap type="ity.Club" id="clubBean" autoMapping="true">
3. <!--column指向数据库列名 property指向pojo对象中字段名-->
4. <result column="cid" property="cid"/>
5. <result column="cname" property="cname"/>
6. <result column="city" property="city"/>
7. <!-- property指的是在bean中字段名 ofType类的全定向名 -->
8. <collection property="players" ofType="ity.Player">
9. <result column="pid" property="pid"/>
10. <result column="pname" property="pname"/>
11. <result column="position" property="position"/>
12. <result column="cid" property="cid"/>
13. <association property="abilities" javaType="ity.Abilities">
14. <result column="aid" property="aid"/>
15. <result column="pid" property="pid"/>
position标签属性16. <result column="shoot" property="shoot"/>
17. </association>
18. </collection>
19. </resultMap>
[html]
1. <select id="joinTwo" resultMap="clubBean">
2. select c.*,p.*,a.*
3. from clubs c
4. join player p
5. on c.cid = p.cid
6. join abilities a
7. on a.pid = p.pid;
8. </select>
2. <resultMap type="ity.Player" id="playerBean">
3. <!--column指向数据库列名 property指向pojo对象中字段名 -->
4. <result column="pid" property="pid" />
5. <result column="pname" property="pname" />
6. <result column="position" property="position" />
7. <result column="cid" property="cid" />
8. <association property="club" javaType="ity.Club">
9. <result column="cid" property="cid" />
10. <result column="cname" property="cname" />
11. <result column="city" property="city" />
12. </association>
13. <association property="abilities" javaType="ity.Abilities">
14. <result column="aid" property="aid"/>
15. <result column="pid" property="pid"/>
16. <result column="shoot" property="shoot"/>
17. </association>
18. </resultMap>
[html]
1. <!-- 结果映射 -->
2. <resultMap type="ity.Abilities" id="abilitiesBean">
3. <!--column指向数据库列名 property指向pojo对象中字段名 -->
4. <result column="aid" property="aid"/>
5. <result column="pid" property="pid"/>
6. <result column="shoot" property="shoot"/>
7. <association property="player" javaType="ity.Player">
8. <result column="pid" property="pid"/>
9. <result column="pname" property="pname"/>
10. <result column="position" property="position"/>
11. <result column="cid" property="cid"/>
12. </association>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论