【MyBatis学习07】输出类型resultType及输出参数映射resultMap

本文博客地址:http://blog.csdn.net/soonfly/article/details/63687784 (转载请注明出处)

使用mybatis操作时,对于SQL语句返回结果的处理通常有两种方式,一种是resultType,另一种是resultMap。
resultType:如果要填充的pojo属性和数据库列名完全一致,可采用resultType。如果出现不一致的情况下,系统并不会报错,只是pojo的属性会填充成null。
resultMap:如果出现不一致的情况下怎么办呢?就要用到reslutMap了。

pojo类如下:

public class Product {
    Integer id;
    String productname;
    Float price2;

    public Product(){}
    public Product(Integer id, String productname, Float price, Integer cateid) {
        super();
        this.id = id;
        this.productname = productname;
        this.price2 = price;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getProductname() {
        return productname;
    }
    public void setProductname(String productname) {
        this.productname = productname;
    }
    public Float getPrice2() {
        return price2;
    }
    public void setPrice2(Float price) {
        this.price2 = price;
    }
    @Override
    public String toString() {
        return "Product [id=" + id + ", productname=" + productname
                + ", price2=" + price2 + "]";
    }
}

在SQL查询中,我们将列名故意重命名。
映射的xml文件如下:

<select id="searchByPrice" parameterType="Map" resultType="Product">
    select id p_id,productname p_name,price p_price from Product where price &gt;= #{minPrice} and price &lt;=#{maxPrice}
</select>

因为返回的列名和pojo属性不一致,所以要增加resultMap标签做一个关系转换。
且查询语句的返回要用resultMap指定。

最终XML如下:
这里写图片描述

本文博客地址:http://blog.csdn.net/soonfly/article/details/63687784 (转载请注明出处)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值