您的位置:首页 > 其它

mybatis 中resultType与resultMap区别

2016-05-29 17:05 330 查看
mybatis中要返回如下对象的List集合:

public class Host {

private String hostType;

private String hostAddr;

public String getHostType() {
return hostType;
}

public void setHostType(String hostType) {
this.hostType = hostType;
}

public String getHostAddr() {
return hostAddr;
}

public void setHostAddr(String hostAddr) {
this.hostAddr = hostAddr;
}

}

如果对应的表结构是:
CREATE TABLE `host` (
`host_type` varchar(32) NOT NULL COMMENT '主机类型',
`host_addr` varchar(128) NOT NULL COMMENT '主机地址',
)


用resultMap返回xml中应当这样写:
<resultMap type="net.flyingfat.common.biz.domain.Host" id="hostResult">
<result column="host_type" property="hostType"/>
<result column="host_addr" property="hostAddr"/>
</resultMap>

<select id="getHost" resultMap="hostResult">
SELECT * FROM host
</select>用resultType返回,xml中仅仅改为
<select id="getHost" resultType="net.flyingfat.common.biz.domain.Host">
SELECT * FROM host
</select>

是不够的,表结构也要改为
CREATE TABLE `host` (
`hostType` varchar(32) NOT NULL COMMENT '主机类型',
`hostAddr` varchar(128) NOT NULL COMMENT '主机地址'
)

结论:如果数据表中的字段包含下划线这种写法,最好还是用resultMap去返回;resultType返回必须保证数据库字段跟java对象的字段大小写一致。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: