三分钟读懂mybatis中resultMap和resultType区别
先说结论:
resultmap与resulttype的区别为:对象不同、描述不同、类型适用不同。
说人话就是,resultmap和resulttype功能差不多,但是resultmap功能更强大
resultType:
使用resultType进行输出映射时,只有查询出来的列名和pojo(简单实例对象)中的属性名一致,该列才可以映射成功。武断一点来说:一般是以下这几种类型才用resultType
1、基本类型 :resultType=基本类型(int,String等基本数据类型)
2、List类型: resultType=List中元素的类型
3、Map类型 单条记录:resultType =map
多条记录:resultType =Map中value的类型
<select id="count" resultType="int"> select count(id) from t_paper as p LEFT JOIN t_type as t ON p.type_id=t.id </select>
resultMap:
前面说过,resultMap和resultType的功能类似,但是resultMap更强大一点,resultMap可以实现将查询结果映射为复杂类型的pojo,简单来说就是,resultType解决不了的,都可以交给resultMap来解决。 在使用resultMap之前我们需要先定义一个符合当前需求的resultMap.。
<resultMap id="paperResult" type="Paper"> <!-- column:数据库字段名 property:实体的属(变量)名 --> <result column="id" property="id"/> <result column="title" property="title"/> <result column="type_id" property="typeId"/> <result column="paper_summary" property="paperSummary"/> <result column="paper_path" property="paperPath"/> </resultMap> <select id="selectPaperListByCondition" resultMap="paperResult"> SELECT p.*, t.type_name from t_paper as p LEFT JOIN t_type as t ON p.type_id=t.id WHERE title='' and type_name='' <where> <if test="title != null and title != ''"> and title like '%${title}%' </if> <if test="typeName != null and typeName != ''"> and type_name=#{typeName} </if> </where> limit #{start},#{size} </select>
总结
到此这篇关于mybatis中resultMap和resultType区别的文章就介绍到这了,更多相关mybatis resultMap和resultType区别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Eclipse 导出可执行Java工程/可执行Jar文件(包含第三方Jar包)
这篇文章主要介绍了Eclipse 导出可执行Java工程/可执行Jar文件(包含第三方Jar包)的相关资料,需要的朋友可以参考下2016-11-11详解spring cloud整合Swagger2构建RESTful服务的APIs
这篇文章主要介绍了详解spring cloud整合Swagger2构建RESTful服务的APIs,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-01-01
最新评论