如果兩表聯查,主表和明細表的主鍵都是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>