您的位置:首页 > 其它

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

2017-03-19 19:44 761 查看
本文博客地址: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 >= #{minPrice} and price <=#{maxPrice}
</select>


因为返回的列名和pojo属性不一致,所以要增加resultMap标签做一个关系转换。

且查询语句的返回要用resultMap指定。

最终XML如下:



本文博客地址:http://blog.csdn.net/soonfly/article/details/63687784 (转载请注明出处)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: