zoukankan      html  css  js  c++  java
  • mybatis 关联查询时,从表只返回第一条记录解决办法

    如果两表联查,主表和明细表的主键都是id的话,明细表的多条只能查询出来第一条。

     

    造成以上情况可能的原因:

      1、级联查询的时候,主表和从表有一样的字段名的时候,在mysql上命令查询是没问题的。但在mybatis中主从表需要为相同字段名设置别名。设置了别名就OK了。

    例子:

    主表Standard, 从表StandEntity,均有名为id的字段

    复制代码
     1 <resultMap id="StandardAndEntityResultMap" type="whu.edu.irlab.model.Standard" extends="BaseResultMap">
    2 <collection property="standEntities" ofType="whu.edu.irlab.model.StandEntity">
    3 (依据下面的select中更名的字段id别名se_id,在此将相同的字段名改为别名)
    4 <id column="se_id" property="id" jdbcType="INTEGER" />
    5 <result column="stand_id" property="standId" jdbcType="INTEGER" />
    6 <result column="stand_name" property="standName" jdbcType="VARCHAR" />
    7 <result column="entity_name" property="entityName" jdbcType="VARCHAR" />
    8 </collection>
    9 </resultMap>
    10
    11 <select id="findAllStandardAndEntity" resultMap="StandardAndEntityResultMap"> 12 select
    13 standard.*, 14 standard_entity.id se_id,(在此将两表中相同的字段名id改为别名se_id,对应的上面collection部分也需要更改)
    15 standard_entity.stand_id, 16 standard_entity.stand_name, 17 standard_entity.entity_name 18 from
    19 standard INNER JOIN standard_entity on standard.id = standard_entity.stand_id
    20 </select>
    复制代码

     

      2、一对多不能用Association,要用Collection。

      根据经验,使用association这个元素很容易出错,建议在resultMap中先换一种写法,不要用association。修改测试一下,如果成功的话,就基本可以去顶是association的问题了,之后查一下association详细资料,应该能解决。如果不是association的问题,就调查一下配置文件等等,总能够解决的。

     

      3、resultMap配置有问题

      bean代码如下:

    复制代码
     1 public class QueryVO {
    2
    3 private AppUser appuser;
    4
    5 private Tb tb;
    6
    7 public AppUser getAppuser() {
    8 return appuser;
    9 }
    10
    11 public void setAppuser(AppUser appuser) { 12 this.appuser = appuser;
    13 }
    14
    15 public Tb getTb() {
    16 return tb;
    17 }
    18
    19 public void setTb(Tb tb) {
    20 this.tb = tb;
    21 }
    22 }
    复制代码

    mapper.xml配置:

    复制代码
     1     <select id="getItemsList" parameterType="QueryVO" resultMap="items">
    2 SELECT
    3 xpro_sys_tb.*,
    4 xpro_sys_app_user.*
    5 FROM
    6 xpro_sys_tb,
    7 xpro_sys_app_user
    8 WHERE xpro_sys_tb.author_id =
    9 xpro_sys_app_user.USER_ID 10 <include refid="query_items_where"></include> 11 </select>
    12
    13 <resultMap type="QueryVO" id="items">
    14 <association property="appuser" resultMap="appUser"></association>
    15 <association property="tb" resultMap="tb"></association>
    16 </resultMap>
    复制代码

     

    以上代码导致的结果是查询出来的总是最后一条数据,类似后面数据覆盖了之前数据的现象。

    发现问题的关键在于resultMap中如果不定义类似主键之类的能够区分每一条结果集的字段的话,会引起后面一条数据覆盖前面一条数据的现象。

    最终将resultMap中添加id,并相应在bean中添加该字段,代码如下,问题解决。

    复制代码
     1 public class QueryVO {
    2
    3 private Integer id;
    4
    5 private AppUser appuser;
    6
    7 private Tb tb;
    8
    9 public AppUser getAppuser() {
    10 return appuser;
    11 }
    12
    13 public void setAppuser(AppUser appuser) { 14 this.appuser = appuser;
    15 }
    16
    17 public Tb getTb() {
    18 return tb;
    19 }
    20
    21 public void setTb(Tb tb) {
    22 this.tb = tb;
    23 }
    24
    25 public Integer getId() {
    26 return id;
    27 }
    28
    29 public void setId(Integer id) {
    30 this.id = id;
    31 }
    32
    33 @Override
    34 public String toString() {
    35 return "QueryVO [id=" + id + ", appuser=" + appuser + ", tb=" + tb + "]";
    36 }
    37 }
    复制代码

    mapper.xml代码:

    复制代码
     1     <select id="getItemsList" parameterType="QueryVO" resultMap="items">
    2 SELECT
    3 xpro_sys_tb.*,
    4 xpro_sys_app_user.*
    5 FROM
    6 xpro_sys_tb,
    7 xpro_sys_app_user
    8 WHERE xpro_sys_tb.author_id =
    9 xpro_sys_app_user.USER_ID
    10 <include refid="query_items_where"></include> 11 </select>
    12
    13 <resultMap type="QueryVO" id="items">
    14 <id property="id" column="id"/>
    15 <association property="appuser" resultMap="appUser"></association>
    16 <association property="tb" resultMap="tb"></association>
    17 </resultMap>
  • 相关阅读:
    第2章 创建基础框架
    目录
    工厂方法模式(Factory Method)
    petshop4.0 详解之七(PetShop表示层设计)
    第1章 启动电子商务网站
    第3章 创建商品目录:第Ⅰ部分
    编写一个JAVA应用程序,用户从键盘只能输入整数,程序输出这些整数的乘积
    书上例题练习第一章
    java与C/C++的区别
    安装JDK遇到的问题
  • 原文地址:https://www.cnblogs.com/jpfss/p/9007195.html
Copyright © 2011-2022 走看看