zoukankan      html  css  js  c++  java
  • #{}多参数如何处理

    假设我们的sql需要多个参数,例如:

    <select id="login" resultType="com.zpc.mybatis.pojo.User"> 
      select * from tb_user where user_name = #{userName} and password = #{password}
    </select>

    而我们的函数如下:

    /**
     * 登录(直接使用注解指定传入参数名称)
     *
     * @param userName
     * @param password
     * @return
     */
    public User login( String userName, String password){查询,传入参数xxx}

    #{userName}和#{password}能与userName、password自动映射上吗?答案是不能

    方法一

    <select id="login" resultType="com.zpc.mybatis.pojo.User">
        select * from tb_user where user_name = #{0} and password = #{1}
    </select>

    通过参数需要来映射

    方法二

    <select id="login" resultType="com.zpc.mybatis.pojo.User"> 
      select * from tb_user where user_name = #{param1} and password = #{param2}
    </select>

    通过param1与param2来映射,与方法一类似

    显然这两种方法都缺乏灵活性

    方法三

    public User login(@Param("userName") String userName, @Param("password") String password);

    在形参处通过注解表明映射关系

  • 相关阅读:

    链表
    队列
    稀疏数组
    SQL——流程控制
    SQL——存储过程与函数
    SOA
    MVC模式
    《一线架构师实践指南》--阅读笔记三
    《一线架构师实践指南》-阅读笔记二
  • 原文地址:https://www.cnblogs.com/yanze/p/10172704.html
Copyright © 2011-2022 走看看