问题描述:
今天使用MyBatis的xml配置文件中进行多表查询时,遇到了一个问题:A实体类中的 B实体类字段 查询出错。
首先A实体类(Blog类) 和B实体类(Type类)是多对多关系,它们由一个中间表(Blog_Type)维护
Blog 实体类代码如下:
public class Blog {
private Long id;
private String title;
private List<Type> types;
}
Type 实体类代码如下:
public class Type {
private int id;
private String name;
}
这个案例需求是:查出Blog实体类对应数据库字段的所有数据。期望结果如下:
BlogQuery{
id=1,
title='简单介绍RESTful风格',
types=[
Type{id=1, name='前端', blogs=null},
Type{id=2, name='Debug专题', blogs=null}
]
}
但是出错了,我查出来的结果如下:
BlogQuery{
id=1,
title='简单介绍RESTful风格',
types=[
Type{id=0, name='前端', blogs=null},
Type{id=0, name='Debug专题', blogs=null}
]
}
注意看,问题在于Blog实体类中的 Type实体类字段 查询出错。
原因分析:
出现这种情况,我首先想到的是结果集映射(resultMap)出现了问题。
于是我打开了MyBatis的xml配置文件,看了看里面的代码:
<select id="queryAllBlog" resultMap="blogquery">
SELECT
b.id,b.title,t.id,t.name
FROM
t_blog b
INNER JOIN
blog_type bt
ON
b.`id`=bt.`bid`
INNER JOIN
t_type t
ON
bt.`tid`=t.`id`
order by b.id
</select>
<resultMap id="blogquery" type="com.lubenwei.vo.BlogQuery">
<id property="id" column="id"/>
<result property="title" column="title"/>
<collection property="types" ofType="Type">
<result property="id" column="id" />
<result property="name" column="name" />
</collection>
</resultMap>
显然,根据上面的代码,resultMap这个结果集不认识Type的id字段。
原因是因为type表的 id 和 blog表的id 冲突,这样的话resultMap结果集映射会因为这个冲突出bug。
解决方案:
既然这两个表的id字段会起冲突,那么就应该在SQL语句中为它们配置别名了,并根据别名重新配置结果集映射:
<!--
注意看第一条sql语句: SELECT b.id,b.title,t.id tid,t.name tname
这里我为什么给t.id起了别名tid
原因是因为t表的 id 和 b表的id 冲突
这样的话resultMap结果集映射会因为这个冲突出bug
-->
<select id="queryAllBlog" resultMap="blogquery">
SELECT
b.id,b.title,t.id tid,t.name tname
FROM
t_blog b
INNER JOIN
blog_type bt
ON
b.`id`=bt.`bid`
INNER JOIN
t_type t
ON
bt.`tid`=t.`id`
order by b.id
</select>
<resultMap id="blogquery" type="com.lubenwei.vo.BlogQuery">
<id property="id" column="id"/>
<result property="title" column="title"/>
<!--既然上面的sql语句变了,下面的column字段也要改成别名的形式-->
<collection property="types" ofType="Type">
<result property="id" column="tid" />
<result property="name" column="tname" />
</collection>
</resultMap>
经过这一番配置,终于不起冲突了,最终查询结果如下:
BlogQuery{
id=1,
title='简单介绍RESTful风格',
types=[
Type{id=1, name='前端', blogs=null},
Type{id=2, name='Debug专题', blogs=null}
]
}
问题解决。