Mybatis之传多个参数方法总结


提出问题

Mybatis如何实现传多个参数???

解决问题

例一:把参数放到map中,看下面代码,有点多

Service层的代码:

public List<PcsTask> findByPmProjectIdAndLeaderId(String projectId) {
        String userId = SessionUtils.getCurrentUserId();
        //查询参数
        Map<String, Object> filter = new HashMap<>();
        filter.put("leaderId", userId);
        filter.put("pmProjectId", projectId);
        return pcsTaskDAO.findByPmProjectIdAndLeaderId(filter);
    }

DAO中的接口:

public List<PcsTask> findByPmProjectIdAndLeaderId(Map<String,Object> filter);

Mapper文件

<select id="findByPmProjectIdAndLeaderId" resultType="com.evada.de.projcommand.model.PcsTask" parameterType="java.util.Map">
        select * from pm_workitem pw
        where pw.pm_project_id = #{pmProjectId}
        and pw.leader_id = #{leaderId}
        and pw.workitem_status in ('1','2')
        and pw.status > '0'
        order by pw.plan_end_date asc
    </select>

例二:看下代码#{0},#{1}好像不是特别好

DAO层的函数方法

Public User selectUser(String name,String area);

Mapper文件

<select id="selectUser" resultMap="BaseResultMap">
    select  *  from user_user_t   where user_name = #{0} and user_area=#{1}
</select>

其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可

例三:最推荐的方法,DAO和Mapper文件的代码量和参数都是最优的.

DAO层的函数方法

public ... findByStatus(@Param("mainTaskId") String mainTaskId, @Param("claimStatus") String claimStatus,@Param("subTaskStatus") String subTaskStatus);

Mapper文件

<select id="findByStatus" resultMap="PcsSubTaskDTOResultMap">
        select pt.*,pw.code as task_code,pw.name as task_name
        from pm_workitem pw,pm_task pt
        where pt.status = '1'
        and pt.pm_workitem_id = pw.id
        and pt.pm_milestone_id = #{mainTaskId}
        <if test="claimStatus != null and claimStatus != ''">
            and pt.claim_status = #{claimStatus}
        </if>
        <if test="subTaskStatus != null and subTaskStatus != ''">
            and pt.task_status = #{subTaskStatus}
        </if>
        order by pt.code  asc
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值