【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类如下:
在SQL查询中,我们将列名故意重命名。
映射的xml文件如下:
因为返回的列名和pojo属性不一致,所以要增加resultMap标签做一个关系转换。
且查询语句的返回要用resultMap指定。
最终XML如下:
本文博客地址: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 (转载请注明出处)
相关文章推荐
- mybatis学习之路----输出映射之resultType resultMap
- Mybatis输入映射parameterType与输出映射ResultMap是Map类型的解决方案
- Mybatis中输出映射-resultType与resultMap的区别
- 【Mybatis框架】输出映射-resultType与resultMap
- MyBatis之输入(parameterType)与输出(resultType、resultMap)映射
- 【Mybatis框架】输出映射-resultType与resultMap
- 【Mybatis框架】输出映射-resultType与resultMap
- mybatis(六) 输入映射 输出映射 resultType:resultMap 动态sql sql片段 if判断 foreach
- Mybatis 输出映射-- resultType resultMap
- Mybatis中输出映射resultType与resultMap的区别
- 【Mybatis框架】输出映射-resultType与resultMap
- MyBatis之输入与输出(resultType、resultMap)映射
- Mybatis (五)输入映射 HashMap输出 传递pojo包装 resultType resultMap 动态sql
- 后台(40)——MyBatis输出映射resultType以及resultMap
- Mybatis学习笔记(6)--一对一的关联使用resultType输出类型
- MyBatis学习-SQL语句映射文件(1)resultMap
- mybatis resultMap映射学习笔记
- JavaEE_Mybatis_SpringMVC_Mybatis_lesson11_一对一关联映射(ResultMap/ResultType)
- MyBatis学习(二) - SQL语句映射文件(1)resultMap
- mybatis resultMap映射学习笔记