resultMap

表示查询结果集与java对象之间的一种关系,处理查询结果集,映射到java对象。

resultMap 是一种“查询结果集---Bean对象”属性名称映射关系,使用resultMap关系可将将查询结果集中的列一一映射到bean对象的各个属性(两者属性名可以不同,配置好映射关系即可),适用与复杂一点的查询。

(1)适用于表的连接查询(在resultMap里面可以配置连接条件,见如下程序association标签)

<!-- 订单查询关联用户的resultMap将整个查询的结果映射到cn.itcast.mybatis.po.Orders中   -->  <resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">  <!-- 配置映射的订单信息 -->  <!-- 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/>          <!-- 配置映射的关联的用户信息 --> <!-- association:用于映射关联查询单个对象的信息property:要将关联查询的用户信息映射到Orders中哪个属性 -->  <association property="user"  javaType="cn.itcast.mybatis.po.User"><!-- id:关联查询用户的唯 一标识column:指定唯 一标识用户信息的列javaType:映射到user的哪个属性--><id column="user_id" property="id"/><result column="username" property="username"/><result column="sex" property="sex"/><result column="address" property="address"/></association> </resultMap>

2)适用于表的一对多连接查询,(如,订单对应多个订单明细时,需要根据连接条件订单id匹配订单明细,并且消除重复的订单信息(订单明细中的),如下程序);

<resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersAndOrderDetailResultMap" extends="OrdersUserResultMap"><!-- 订单信息 --> <!-- 用户信息 --><!-- 使用extends继承,不用在中配置订单信息和用户信息的映射 --><!-- 订单明细信息一个订单关联查询出了多条明细,要使用collection进行映射collection:对关联查询到多条记录映射到集合对象中property:将关联查询到多条记录映射到cn.itcast.mybatis.po.Orders哪个属性 ofType:指定映射到list集合属性中pojo的类型 -->  <collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail"><!-- id:订单明细唯 一标识 property:要将订单明细的唯 一标识 映射到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> 

(3)映射的查询结果集中的列标签可以根据需要灵活变化,并且,在映射关系中,还可以通过typeHandler设置实现查询结果值的类型转换,比如布尔型与0/1的类型转换。

例如:

<resultMap type="hdu.terence.bean.Message" id="MessageResult"> <!--存放Dao值--><!--type是和数据库对应的bean类名Message--><id column="id" jdbcType="INTEGER"property=" id"/><!--主键标签--><result column="COMMAND" jdbcType="VARCHAR" property="command"/><result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/><result column="CONTENT" jdbcType="VARCHAR" property="content"/></resultMap> <select id="queryMessageList" parameterType="hdu.terence.bean.Message" resultMap="MessageResult">SELECTID,COMMAND,DESCRIPTION,CONTENT FROM message WHERE 1=1    <if test="command!=null and!&quot;&quot;.equals(command.trim())">and COMMAND=#{command}</if><if test="description!=null and!&quot;&quot;.equals(description.trim())">and DESCRIPTION like '%' #{description} '%'</if> </select>

resultType

resultType 是一种“查询结果集---Bean对象”数据类型映射关系,使用resultType关系,即可使Bean对象接收查询结果集;见名知意,该方法是通过查询结果集中每条记录(属性)的数据类型和Bean对象的数据类型作映射,若两者都相同,则表示匹配成功,Bean可以接收到查询结果。

但是本方法有局限性,要求Bean对象字段名和查询结果集的属性名相同(可以大小写不同,大小写不敏感)。因为这个局限性,可以省略调resultMap进行属性名映射。

一般适用于pojo(简单对象)类型数据,简单的单表查询。

以下是resultType的写法,将其值设置成对应的java类上即可。不需要上述resultMap的映射关系。

<select resultType="User" id="findAll">select *from user </select>
<select resultType="com.itxiaotong.pojo.User" id="findById" parameterType="int">select *from user where id = #{userId} </select>
<select resultType="com.itxiaotong.pojo.User" id="findByUsernameLike" parameterType="string">
<bind value="'%'+username+'%'" name="likeName"/>
select * from user where username like #{likeName}
</select>
<select resultType="com.itxiaotong.pojo.User" id="findPage">select * from user limit #{param1},#{param2} </select><select resultType="com.itxiaotong.pojo.User" id="findPage1">select * from user limit #{startIndex},#{pageSize} </select>
<select resultType="User" id="findPage2" parameterType="PageQuery">select * from user limit #{startIndex},#{pageSize} </select>

其中parameterType="PageQuery"的类是,下列内容

PageQuery.java

package com.itxiaotong.pojo;public class PageQuery {private int startIndex;private int pageSize;public PageQuery() {}public PageQuery(int startIndex, int pageSize) {this.startIndex = startIndex;this.pageSize = pageSize;}public int getStartIndex() {return startIndex;}public void setStartIndex(int startIndex) {this.startIndex = startIndex;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}
}
<select resultType="com.itxiaotong.pojo.User" id="findPage3" parameterType="map">select * from user limit #{startIndex},#{pageSize} </select><select resultType="int" id="findCount">select count(id) from user </select>

parameterType

在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了
parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的
输入参数类型即可。parameterType有基本数据类型和复杂的数据类型配置。
1.基本数据类型,如输入参数只有一个,其数据类型可以是基本的数据类型,也可以是
自己定的类类型。包括int,String,Integer,Date,如下:

(1)根据id进行相应的删除:

(2)添加员工:

2.复杂数据类型:包含java实体类,map。

parameterType例子(一)

现在有一个Mapper配置文件,以下是片段:

 <select id="queryCommandListByPage" resultMap="CommandResult" >select <include refid="columns"/> from command a left join command_content b on a.id=b.command_id<where><if test="command.name != null and !"".equals(command.name.trim())">and a.name=#{command.name}</if><if test="command.description != null and !"".equals(command.description.trim())">and a.description like '%' #{command.description} '%'</if></where><if test="flag==1">group by aid</if>order by id</select><sql  id="columns">a.id aid,a.name,a.description,b.content,b.id,b.command_id</sql>

下面是IService接口:

 /*** 拦截器实现分页*/public List<command> queryCommandListByPage(Map<String,Object>parameter);

parameterType例子(二)

<insert id="add" parameterType="com.itxiaotong.pojo.User">insert into user(username, sex, address)values (#{username}, #{sex}, #{address}) </insert>
<update id="update" parameterType="com.itxiaotong.pojo.User">update user set username = #{username},sex = #{sex},address=#{address} where id = #{id} </update>
<delete id="delete" parameterType="int">delete from user where id = #{id} </delete>
<insert id="add2" parameterType="com.itxiaotong.pojo.User"><!-- keyProperty:主键属性名 keyColumn:主键列名 resultType:主键类型 order:执行时机 --><selectKey resultType="int" order="AFTER" keyColumn="id" keyProperty="id">SELECT LAST_INSERT_ID(); </selectKey>
insert into user(username, sex, address)values (#{username}, #{sex}, #{address})
</insert>
<select resultType="com.itxiaotong.pojo.User" id="findPage3" parameterType="map">select * from user limit #{startIndex},#{pageSize} </select>

详谈parameterType与resultType的用法相关推荐

  1. parameterType和@Param注解用法

    在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了 parameterType的用法,parameterType为输入参数,在配置的时候,配置相应 ...

  2. c语言next函数,详谈全排列next_permutation() 函数的用法(推荐)

    这是一个c++函数,包含在头文件里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下面的代码可产生1~n的全排列 #i ...

  3. 映射文件中增删改查标签中的parameterType和resultType

    parameterType:指定输入参数类型,mybatis通过ognl从输入对象中获取参数值拼接在sql中. resultType:指定输出结果类型,mybatis将sql查询结果的一行记录数据映射 ...

  4. parameterType和resultType

    parameterType:指定输入参数类型,mybatis通过ognl从输入对象中获取参数值拼接在sql中. resultType:指定输出结果类型,mybatis将sql查询结果的一行记录数据映射 ...

  5. C语言中getchar()的用法详谈,c语言getchar的用法

    1.从缓冲区读走一个字符,相当于清除缓冲区 2.前面的scanf()在读取输入时会在缓冲区中留下一个字符'\n'(输入完s[i]的值后按回车键所致), 所以如果不在此加一个getchar()把这个回车 ...

  6. Mybatis中parameterType的用法

    在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配置相应的 ...

  7. mybatis中resultMap和resultType的详细用法

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接 表示返回类型的,而resultMap则是对外部Result ...

  8. parameterType 用法

    该参数只表示一个参数类型,但是如果有多个参数的话,就不要使用该参数了.一个参数的这种其实没有必要写parameterType,而且还有就是多个参数的时候也没有必要写parameterType,也就是说 ...

  9. resultType和parameterType的基本使用和区别

    resultType与parameterType的基本使用和区别 Mybatis的Mapper文件中的select,insert,update,delect元素中都有一个parameterType和r ...

最新文章

  1. win10 error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突
  2. 「不会开会」是个病,这本书能治吗?
  3. 企业级java b2bc商城系统开源源码二次开发(二十一)springboot集成JMS
  4. 各类电脑高效率神器使用及下载地址
  5. RxSwift之深入解析场景特征序列的使用和底层实现
  6. 云原生五大趋势预测,K8s安卓化位列其一
  7. linux运行搜狗拼音,Linux 搜狗输入法的安装(Ubuntu版)
  8. 想宅家学习但实力不允许?9本书,揭秘学霸是如何养成的​
  9. webpack4学习之问题一
  10. wap移动网页开发rem用法
  11. Java 异常处理的 20 个最佳实践,你知道几个?| CSDN 博文精选
  12. 【Foreign】字符串匹配 [KMP]
  13. 重命名DC 以及 域
  14. 软考《软件设计师教程》(第五版)
  15. 服务器基本安全策略配置
  16. jboot_jboot这些框架有意义吗
  17. 转载:电脑cmd命令怎么测试网速详细步骤
  18. 【狂神说】 mysql 自学总结 4~6章
  19. 我在b站学数据库 (七):多表操作
  20. 第 338 场周赛 (力扣周赛)

热门文章

  1. 工作五年,一年内我靠这系列 java 面试宝典从 13K 到大厂 30K
  2. 【Java----函数(function)】
  3. x7 z8750 linux,GPD Pocket 7.0英寸 口袋笔记本电脑(Atom x7-Z8750、8GB、128GB、触屏)
  4. html平板电脑打不开,苹果平板电脑浏览器打不开网页
  5. 三:Sensor SLPI层代码分析---
  6. Thread优先级之优先级别
  7. WEB安全之越权漏洞
  8. 如何使用MyBatis的plugin插件实现多租户的数据过滤?
  9. 让人心动的jQuery插件和HTML5动画
  10. java怎么给文本框加滚动条_懂Java中Swing的朋友,谁能帮我在Jtextare上加一个滚动条啊?很急!...