resultType和resultMap的区别
来源:互联网 发布:淘宝信用度怎么看 编辑:程序博客网 时间:2024/05/16 14:48
在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解:
resultType:当使用resultType做SQL语句返回结果类型处理时,对于SQL语句查询出的字段在相应的pojo中必须有和它相同的字段对应,而resultType中的内容就是pojo在本项目中的位置。
因此对于单表查询的话用resultType是最合适的。但是,如果在写pojo时,不想用数据库表中定义的字段名称,也是可以使用resultMap进行处理对应的。多表连接查询时,若是一对一的连接查询,那么需要新建一个pojo,pojo中包括两个表中需要查询出的所有的字段,这个地方的处理方式通常为创建一个继承一个表字段的pojo,再在里面添加另外一个表内需要查询出的字段即可。若是一对多查询时,若是使用内连接查询,则很可能出现查询出的字段有重复。使用双重for循环嵌套处理即可。resultMap:当使用resultMap做SQL语句返回结果类型处理时,通常需要在mapper.xml中定义resultMap进行pojo和相应表字段的对应。
resultMap对于一对一表连接的处理方式通常为在主表的pojo中添加嵌套另一个表的pojo,然后在mapper.xml中采用association节点元素进行对另一个表的连接处理。例如:<!--订单查询关联用户的resultMap,将整个查询结果映射到cn.itcast.mybatis.po.Orders中-->
<resultMap id="OrdersUserResultMap" type="cn.itcast.mybatis.po.Orders">
<!--配置映射的订单信息-->
<!--id:指定查询列中的唯一标识,订单信息中的唯一标识,如果有多个列组成唯一标识,则配置多个id
column:订单信息的数据库存储列
property:映射到Orders中的属性-->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createTime" property="createtime"/>
<result column="note" property="note"/>
</resultMap>
若是一对多的表连接方式,比如订单表和订单明细表即为一对多连接,若是不对sql语句进行处理,由于一个订单对应多条订单明细,因此查询出的结果对于订单表数据来说将会出现重复,例如:<!--订单查询关联用户的resultMap,将整个查询的结果映射到cn.itcast.mybatis.po.Orders中-->
<resultMap id="OrdersUserResultMap type="cn.itcast.mybatis.po.Orders">
<!--配置映射的订单信息-->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createTime" property="createtime"/>
<result column="note" property="note"/>
<!--配置映射的关联的用户的信息-->
<!--association:用于映射关联查询单个对象的信息
peoperty:要将关联查询的用户信息映射到Orders中那个属性比如:在Orders类里有private User user-->
<association property="user" javaType="cn.itcast.mybatis.po.User">
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
</association>
</resultMap>
resultMap的处理方式为在订单表数据的pojo中添加一个list,list中为订单明细表的属性,在mapper.xml中采用如下的处理方式:
<!--订单及订单明细的resultMap,使用extends继承,不用再配置订单信息和用户信息的映射-->
<resultMap id="OrdersAndOrderDetailsResultMap" type="cn.itcast.mybatis.po.Orders" extends="OrdersUserResultMap>
<!--一个订单关联查询多条明细,要使用collection进行映射
collection:对关联查询到多条记录映射到集合对象中
property:将关联查询到多条记录映射到cn.itcast.mybatis.po.Orders哪个属性
ofType:指定映射到list集合属性中pojo的类型
-->
<collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail">
<id column="orderdetail_id" property="id"/>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<result column="orders_id" property="ordersId"/>
</collection>
</resultMap>
在查询时,虽然一条订单信息对应多条订单明细,由于将多条信息明细存储到了list中,因此查询后将不再出现重复数据,达到了去重的效果。
- resultType和resultMap的区别
- resultType和resultMap的区别
- resultType和resultMap区别
- mybatis中resultMap和resultType的区别
- mybatis resultType和resultMap的区别
- MyBatis中ResultMap和ResultType的区别
- MyBatis中resultType和resultMap的区别
- Mybatis: resultType和resultMap的区别
- Mybatis中的resultType和resultMap的区别
- mybatis中resulttype和resultmap的区别
- MyBatis中resultType和resultMap的区别
- resultMap跟resultType的区别
- Mybatis的resultType和resultMap
- ibatis中resultMap和resultClass(Mybatis的resultType)的区别
- MyBatis中关于resultType和resultMap的区别
- MyBatis中关于resultType和resultMap的区别
- MyBatis中关于resultType和resultMap的区别
- Mybatis中的resultType和resultMap的使用及区别
- Java 基础总结
- Canvas 用法一:绘制图形
- Mybatis获取插入记录的自增长ID
- string基本字符系列容器
- 关于多张图片在数据库中的存储问题
- resultType和resultMap的区别
- iOS-浅谈UIScrollView
- Mysql如何支持Emoji表情,修改数据库字符集为utf8mb4后依然不行
- easyui之重新加载datagrid
- jquery easyui datagrid 获取Checked选择行(勾选行)数据
- Mybatis中模糊查询
- Mybatis实现批量删除操作
- 编程之美之1.2 中国象棋将帅问题
- MyBatis传入多个参数的问题