亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

MYBATIS一對多關系取出的數據只包含子表的第一條記錄信息,而不是一個集合,怎么辦?

在MyBatis一對多的關系中,按照老師的方法配置了COMMAND表和COMMAND_CONTENT表對應的JAVA類和相應的XML文件,執行后,在控制臺的DEBUG級別的日志顯示結果為4條數據,封裝到COMMAND類對象中是一條數據,從COMMAND對象中再取它的LIST<CommandContent>鏈表,理應為4個CommandContent類的對象才合情合理,但執行結果只有一條,第一條記錄。

郁悶,不知是什么原因?請老師或者熱心人給予幫助。 也可QQ聯系我:372102668

萬分感謝。

這是我的JAVA類及相應的XML

(1)Command.java

package cn.sdut.bean;

import java.util.List;

public class Command {

private int id;

private String command;

private String description;

private List<CommandContent> contents;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getCommand() {

return command;

}

public void setCommand(String command) {

this.command = command;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public List<CommandContent> getContents() {

return contents;

}

public void setContents(List<CommandContent> contents) {

this.contents = contents;

}?

}

(2)CommandContent.java

package cn.sdut.bean;

public class CommandContent {

private int id;

private String content;

private int commandId;

public int getCommandId() {

return commandId;

}

public void setCommandId(int commandId) {

this.commandId = commandId;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

}

(3) Command.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE mapper

? ? PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

? ? "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Command">


? <resultMap type="cn.sdut.bean.Command" id="CommandResult">

? ? <id column="id" jdbcType="INTEGER" property="id"/>

? ? <result column="command" jdbcType="VARCHAR" property="command"/>

? ? <result column="description" jdbcType="VARCHAR" property="description"/>

? ? <collection property="contents" resultMap="Content.ContentResult"></collection>

? </resultMap>


? ?<select id="commandList" parameterType="cn.sdut.bean.Command" resultMap="CommandResult">?

? ? ? SELECT a.id,command,description,content?

? ? ? FROM command a,command_content b ?

? ? ??

? ? ? <where>

? ? ? ? a.id=b.command_id

? ? ? <if test="command!=null and !&quot;&quot;.equals(command.trim())">

? ? ? ? and a.command like '%' #{command} '%'

? ? ? </if> ? ?

? ? ? </where>

? ? </select>?

</mapper>

(4)CommandContent.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE mapper

? ? PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

? ? "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="Content">

? ?<resultMap type="cn.sdut.bean.CommandContent" id="ContentResult">

? ? <id column="id" jdbcType="INTEGER" property="id"/>

? ? <result column="content" jdbcType="VARCHAR" property="content"/>

? ? <result column="command_id" jdbcType="INTEGER" property="commandId"/>

? </resultMap>

</mapper>

(5) 數據庫中的2個表的定義SQL語句:

create table command(

id int primary key auto_increment,

command varchar(20) ,

description varchar(40)

)

create table command_content(

? id int primary key auto_increment,

? content varchar(2048),

? command_id int

)


正在回答

5 回答

剛解決,應該是列名一致了

0 回復 有任何疑惑可以回復我~

兩個表的id字段不要都叫id,需要不一樣?;蛘呖梢栽诓樵兊臅r候取別名

1 回復 有任何疑惑可以回復我~

同樣的問題 ,數據庫查詢沒問題?

0 回復 有任何疑惑可以回復我~

你可以先把你的sql復制一下,到數據庫中執行以下,看看可以查到幾點數據?

0 回復 有任何疑惑可以回復我~

同樣的問題。 求樓主分享

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

MYBATIS一對多關系取出的數據只包含子表的第一條記錄信息,而不是一個集合,怎么辦?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號