mapper.xml中返回值类型

这篇博客详细介绍了MyBatis中resultType和resultMap的用法,resultType用于简单返回类型,而resultMap用于复杂映射。通过示例展示了如何在XML映射文件中配置,并提及了在处理list参数时的注意事项,包括默认list参数的使用和自定义参数名的处理。同时,博主分享了在实际操作中遇到的问题及解决思路。

因为两次都错在这个上面上,所以还是记录一下。。。。

resultType是直接表示返回类型的,比如Integer,String, List<Integer>也是一样,resultMap则是对外部ResultMap的引用

例如:

  <select id="selectByStatus" resultType="java.lang.Integer" parameterType="com.meihui.learning.viewmodel.admin.question.QuestionStatusVM">
    SELECT
    t_question.id
    FROM t_question LEFT JOIN t_question_status on t_question_status.question_id = t_question.id
    <where>
      and t_question.deleted = 0
      <if test="questionType != null">
        and t_question.question_type = #{questionType}
      </if>
      <if test="wrongStatus != null ">
        and t_question_status.wrong_status= #{wrongStatus}
      </if>
      <if test="subjectSecondId != null ">
        and t_question.subject_second_id= #{subjectSecondId}
      </if>
      <if test="subjectId != null ">
        and t_question.subject_id= #{subjectId}
      </if>
    </where>
  </select>
  <select id="getUserDingTaskByDdUserIdDeptId" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from t_user_ding_task
    where dd_user_id = #{ddUserId} and dept_id = #{deptId} and deleted = 0 limit 1
  </select>

 还有一种略特殊的,其实也不算特殊,之前没理解清楚,下面并没有引用外部的,所以用resultType是正常的。。 <include refid="Base_Column_List"/>这种才是外部引用。。。

  <select id="selectCountByDate"  resultType="com.meihui.learning.domain.other.KeyValue">
		SELECT create_time as name,COUNT(create_time) as value from
		        (
				  SELECT DATE_FORMAT(create_time,'%Y-%m-%d') as create_time from 
                   t_user_event_log
					WHERE  create_time  between  #{startTime}  and  #{endTime}
				) a
		GROUP BY create_time
  </select>
    List<KeyValue> selectCountByDate(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

https://blog.youkuaiyun.com/woshixuye/article/details/27521071  这位很详细

还有就是关于传递参数为list的时候的一个小问题。。

默认的list,亲测可行

    List<UserDTO> selectByDdUserIds(List<String> list);
  <select id="selectByDdUserIds" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_user_ding
    where id in
    <foreach item="ddUserId" collection="list" open="(" separator=","
             close=")">
      #{ddUserId}
    </foreach>
  </select>

这里的collection为list,所以上面的代码里不用传@Param("list")之类的

但是如果是自定义的,就得加上@Param("ddUserIds")之类的。。

    List<UserDTO> selectByDdUserIds(@Param("ddIdList") List<String> ddIdList);
  <select id="selectByDdUserIds" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_user_ding
    where id in
    <foreach item="ddUserId" collection="ddIdList" open="(" separator=","
             close=")">
      #{ddUserId}
    </foreach>
  </select>

但其实我代码里这么写还是报错 。。。清过redis缓存,clean也clean过了。。就是报错。。。但是其他的xml里的也是这写法就可以,应该还是哪里出问题了。。

https://blog.youkuaiyun.com/qq_28379809/article/details/83342196 这位也很详细。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值