Mybatis 多表关联 查询条件是重复字段,Mapping.xml如何解决?

xiu825561896 2016-10-12 09:37:42
现在有2张表,一个 广告表(cnt_advert),一个广告位置表(cnt_advert_location)

我建立了2个bean

public class Advert

private String id;
private String name;
private String accountType;
private String url;
private String picUrl;
private String createTime;
private String startTime;
private String endTime;
private String remark;
private Integer sort;
private String isEnable; // get/set 方法省略

public class AdLocation

private String id;
private String code;
private String name;
private String size;
private Integer num;
private String createTime;
private String isEnable; // get/set 方法省略

映射文件是:

<mapper namespace="com.advert.dao.AdvertDao">

<resultMap id="BaseResultMap" type="com.advert.bean.Advert">
<result column="id" property="id" />
<result column="name" property="name" />
<result column="account_type" property="accountType" />
<result column="url" property="url" />
<result column="pic_url" property="picUrl" />
<result column="create_time" property="createTime" />
<result column="start_time" property="startTime" />
<result column="end_time" property="endTime" />
<result column="remark" property="remark" />
<result column="sort" property="sort" />
<result column="is_enable" property="isEnable" />

</resultMap>
<select id="selectPageList" resultMap="BaseResultMap"
parameterType="com.advert.bean.Advert">
select a.name,b.name as location_name
from cnt_advert a ,cnt_advert_location b
where a.location_id=b.id
and b.name='1a'
</select>


求大神赐教,谢谢
...全文
552 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiu825561896 2016-10-13
  • 打赏
  • 举报
回复
引用 4 楼 wlwlwlwl015 的回复:
[quote=引用 2 楼 xiu825561896 的回复:] [quote=引用 1 楼 wlwlwlwl015 的回复:] 哪里重复了~ 重复起别名就行了~ 还有关联查询建议写成join的形式~ 符合SQL92标准~
两张表都是有 name 字段,我的where条件后面 有根据name 查询 <!-- 第一语句--> select a.name,b.name as location_name from cnt_advert a ,cnt_advert_location b where a.location_id=b.id and b.name='1a' <!-- 第二语句--> select a.name,b.name as location_name from cnt_advert a ,cnt_advert_location b where a.location_id=b.id and a.name='1'[/quote] 没事,加好别名就可以了,a.id=b.id,这个id和数据库字段保持一致,mybatis关注的是返回结果的字段和别名,需要和resultMap 里配置的property一致[/quote] 谢谢
小灯光环 2016-10-12
  • 打赏
  • 举报
回复
引用 2 楼 xiu825561896 的回复:
[quote=引用 1 楼 wlwlwlwl015 的回复:] 哪里重复了~ 重复起别名就行了~ 还有关联查询建议写成join的形式~ 符合SQL92标准~
两张表都是有 name 字段,我的where条件后面 有根据name 查询 <!-- 第一语句--> select a.name,b.name as location_name from cnt_advert a ,cnt_advert_location b where a.location_id=b.id and b.name='1a' <!-- 第二语句--> select a.name,b.name as location_name from cnt_advert a ,cnt_advert_location b where a.location_id=b.id and a.name='1'[/quote] 没事,加好别名就可以了,a.id=b.id,这个id和数据库字段保持一致,mybatis关注的是返回结果的字段和别名,需要和resultMap 里配置的property一致
___d 2016-10-12
  • 打赏
  • 举报
回复
起别名就行了
xiu825561896 2016-10-12
  • 打赏
  • 举报
回复
引用 1 楼 wlwlwlwl015 的回复:
哪里重复了~ 重复起别名就行了~ 还有关联查询建议写成join的形式~ 符合SQL92标准~
两张表都是有 name 字段,我的where条件后面 有根据name 查询 <!-- 第一语句--> select a.name,b.name as location_name from cnt_advert a ,cnt_advert_location b where a.location_id=b.id and b.name='1a' <!-- 第二语句--> select a.name,b.name as location_name from cnt_advert a ,cnt_advert_location b where a.location_id=b.id and a.name='1'
小灯光环 2016-10-12
  • 打赏
  • 举报
回复
哪里重复了~ 重复起别名就行了~ 还有关联查询建议写成join的形式~ 符合SQL92标准~

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧