0

我有一个模型类,它只保存另外两个表的外键。像这样 :

CREATE TABLE `ToolOSRelation` (


`toolType` varchar(100) NOT NULL DEFAULT '',
  `OSName` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`toolType`,`OSName`),
  KEY `OSName` (`OSName`),
  CONSTRAINT `toolosrelation_ibfk_1` FOREIGN KEY (`toolType`) REFERENCES `Tools` (`toolType`),
  CONSTRAINT `toolosrelation_ibfk_2` FOREIGN KEY (`OSName`) REFERENCES `OS` (`OSName`)
)

相应地,我有以下 Model 类作为 ToolOSRelation.java :

public class ToolOSRelation {

    private Tools toolType;
    private OS OSName;

    public ToolOSRelation() {

    }
//remaining getters and setters here

在此之后,我有以下 mybatis 映射器:

<mapper namespace="com.dexter.deviceType.ToolOSRelationDao">
    <select id="getToolsForOS" resultMap="ToolOSRelationMap" parameterType="String">
        SELECT
            t.toolType
        FROM
            ToolOSRelation t
        LEFT JOIN
            OS o
        ON
            t.OSName=o.OSName
        WHERE
            o.OSName=#{osName}
    </select>

    <resultMap id="ToolOSRelationMap" type="ToolOSRelation"> 
<association property="toolType" javaType="Tools">
     <result property="toolType" column="toolType" jdbcType="VARCHAR"/> 
</association> 
<association property="OSName" javaType="OS">
    <result property="OSName" column="OSName" jdbcType="VARCHAR" />
</association>
</mapper>

javaType="OS" 指向文件 OS.java,"Tools" 指向 Tools.java。

当我在 mysql shell 中运行它时,查询工作正常。但是,这会返回一个 ToolOSRelation 对象,其中所有值(工具和操作系统)都为空。我哪里错了?

编辑:添加工具类和操作系统类:

public class Tools {

    private String toolType;
    private List<Template> template;
    private List<ToolOSRelation> OSName;

    public Tools() {}
    }
    public Tools(String toolType) {

        this.toolType =toolType;
    }

    public String getToolType() {
        return toolType;
    }

    public void setToolType(String toolType) {
        this.toolType = toolType;
    }
    public List<Template> getTemplate() {
        return template;
    }
    public void setTemplate(List<Template> template) {
        this.template = template;
    }

    public List<ToolOSRelation> getOSName() {
        return OSName;
    }
    public void setOSName(List<ToolOSRelation> oSName) {
        OSName = oSName;
    }

}

public class OS {

    private String OSName;
    private List<DeviceType> deviceType;
    private List<ToolOSRelation> toolType;

    public OS() {
    }

    public OS(String OSName) {
        this.OSName = OSName;
    }

    public String getOSName() {
        return OSName;
    }

    public void setOSName(String oSName) {
        OSName = oSName;
    }
    public List<DeviceType> getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(List<DeviceType> deviceType) {
        this.deviceType = deviceType;
    }
    public List<ToolOSRelation> getToolType() {
        return toolType;
    }

    public void setToolType(List<ToolOSRelation> toolType) {
        this.toolType = toolType;
    }
}
4

1 回答 1

0

我想那是 Mybatis 找不到变形属性,你检查过模型类中的 setter 和 getter。

于 2016-01-21T09:57:14.410 回答