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 !"".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
)
2017-12-13
剛解決,應該是列名一致了
2017-05-05
兩個表的id字段不要都叫id,需要不一樣?;蛘呖梢栽诓樵兊臅r候取別名
2016-09-16
同樣的問題 ,數據庫查詢沒問題?
2016-08-22
你可以先把你的sql復制一下,到數據庫中執行以下,看看可以查到幾點數據?
2016-04-25
同樣的問題。 求樓主分享