广告
返回顶部
首页 > 资讯 > 后端开发 > Python >MyBatis映射文件中parameterType与resultType的用法详解
  • 406
分享到

MyBatis映射文件中parameterType与resultType的用法详解

parametertype和resulttypeparametertype和resulttype的区别 2023-05-15 11:05:09 406人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

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

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>

总结

到此这篇关于MyBatis映射文件中parameterType与resultType的用法详解的文章就介绍到这了,更多相关parameterType与resultType用法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: MyBatis映射文件中parameterType与resultType的用法详解

本文链接: https://www.lsjlt.com/news/208889.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作