Mybatis resultmap resultType初识 二 :xml 文件的使用 多参数 单参数传值

3 篇文章 0 订阅
3 篇文章 0 订阅

先了解下查询 如下sql 语句

说明 sql 的ID 要唯一!!!


<resultMap id="BaseResultMap" type="map" >
        <id column="id" property="id" jdbcType="VARCHAR" />
        <result column="userName" property="userName" jdbcType="VARCHAR" />
        <result column="passWord" property="password" jdbcType="VARCHAR" />
        <result column="disPlayName" property="disPlayName" javaType="VARCHAR"/>

    </resultMap>


//resultMap 将返回值映射到自定义到的map中
 <select id="findAll" resultMap="BaseResultMap">
        select
         id , userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser limit 1,10
   </select>

//也可以不写resultMap 直接用resultType

 <select id="findAll"resultType="map">
        select
         id , userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser limit 1,10
   </select>

也有人会把resultMap 写成如下的方式

//type 就是将返回值映射成一个pojo实体
<resultMap id="GuserResultMap" type="com.chris.apecircle.domain.Guser.Guser" >
        <id column="id" property="id" jdbcType="VARCHAR" />
        <result column="userName" property="userName" jdbcType="VARCHAR" />
        <result column="passWord" property="password" jdbcType="VARCHAR" />
        <result column="disPlayName" property="disPlayName" javaType="VARCHAR"/>

    </resultMap>

   <select id="getAll" resultMap="GuserResultMap">
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser limit 1,10
    </select>

//这里我反而觉得有点鸡肋,原因如下
//1.这里我如果只需上述的4 个参数,这样写会将实体中没有映射的属性也返回,返回值臃肿
//2.如果一定要返回的话 我们可以采用 resultType 的方式,少写了一个resultMap 代码也整洁了
//3.这种情况在有别名的状态下才能体现价值所在

resultType方式
 <select id="getAll" resultType="com.chris.apecircle.domain.Guser.Guser">
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser limit 1,10
 </select>
//MARK: resultType 和 resultMap 不能同时使用

传参数情况下

 List<Guser>  getGuserbyUserName(String userName);


//parameterType 输入的参数类型
 <select id="getGuserbyUserName" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser where userName = #{userName}
    </select>

当有两个参数的时候 就不能用parameterType 了这时用几种方法来处理,这里我只展示一种常用的方法

一:采用下标的方式
 Object getGuser(String userName,String pwd);

  <select id="getGuser" resultMap="BaseResultMap" >
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser where userName = #{arg0} and passWord = #{arg1}
    </select>

//这种对于mapper 修改的较少但是需要和参数一一对应,对于参数多时不友好

二:采用注解的方式
 Object getGuser(@Param("userName") String userName,@Param("pwd") String pwd );

  <select id="getGuser" resultMap="BaseResultMap" >
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser where userName = #{userName} and passWord = #{pwd}
    </select>
//这种方式 相较于前者 明确传值名称,顺序不需要一一对应, 缺点 对于参数多时依然不友好

三:map 封装
Object getGuser(HashMap map);

  <select id="getGuser" resultMap="BaseResultMap" >
        select
         id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
        from guser where userName = #{userName} and passWord = #{pwd}
    </select>

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值