当前位置: 首页 > news >正文

详谈parameterType与resultType的用法

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>

http://www.lryc.cn/news/39555.html

相关文章:

  • 【Linux】进程概念、fork() 函数 (干货满满)
  • 【动态规划】最长上升子序列、最大子数组和题解及代码实现
  • Ajax进阶篇02---跨域与JSONP
  • C 语言编程 — 线程池设计与实现
  • 并发编程要点
  • HDFS黑名单退役服务器
  • 基于stm32智能语音电梯消毒系统
  • FreeRTOS系列第1篇---为什么选择FreeRTOS?
  • 基于.NET Core内置浏览器窗体应用程序界面框架
  • 【数据结构初阶】一文带你学会归并排序(递归非递归)
  • Simulink壁咚(一)——What and How
  • 【PyTorch】Pytorch基础第0章
  • Android学习总结
  • 虚拟机ubuntu安装samba服务
  • 开发板中的内存压力测试,你了解多少?
  • MATLAB | 这些花里胡哨的热图怎么画
  • Java开发的一些编码建议
  • 【YOLOv8/YOLOv7/YOLOv5/YOLOv4/Faster-rcnn系列算法改进NO.59】引入ASPP模块
  • C++STL set/multiset容器 构造和赋值 大小和交换 插入和删除 查找和统计
  • 产品研发项目进度管理软件工具有哪些推荐?整理10款最佳进度管理软件
  • 「ML 实践篇」分类系统:图片数字识别
  • 从大专到测开,上海某字母站大厂的面试题,岗位是测开(25K*16)
  • 【面试题】Python软件工程师能力评估试题(一)
  • Java八股文(Java多线程面试题)
  • 小程序当前页面如何分享别的页面内容呢?
  • 编写Java哪个编译器好
  • 第十六章 Java为什么使用序列化
  • 28岁小公司程序员,无车无房不敢结婚,要不要转行?
  • 出道即封神的ChatGPT,现在怎么样了?
  • 【计算机视觉】CNN 可视化算法