Mybatis无法执行查询并映射到apt Model类

时间:2016-01-21 07:34:09

标签: java spring mybatis spring-mybatis

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

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对象,其中所有值(工具和操作系统)都为null。我哪里错了?

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

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;
    }
}

1 个答案:

答案 0 :(得分:0)

我想Mybatis无法找到变形属性,你在模型类中检查了setter和getter。