【MyBatis-Debug】在SQL语句中为数据库字段取别名的重要性

问题描述:

今天使用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}
		  ]
}

问题解决。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值