zoukankan      html  css  js  c++  java
  • @Param注解在Mybatis中的使用 以及传递参数的三种方式

    第一种:

    Dao层的方法

    public User selectUser(String name,String password);

    对应的Mapper.xml

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

    第二种:

    该方法采用Map传多参数

    Dao层的方法

    public User selectUser(Map paramMap);

    对应的Mapper.xml

    <select id="selectUser" resultMap="BaseResultMap">  
       select * from t_user where user_name = #{userName,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR}  
    </select>

    Service层调用

    public User xxxSelectUser(){  
        Map paramMap=new hashMap();  
        paramMap.put("userName","对应具体的参数值");  
        paramMap.put("userPassword","对应具体的参数值");  
        User user=xxx.selectUser(paramMap);
    }

    个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。

    第三种:@Param注解

    Dao层的方法

    public User selectUser(@Param("name")String name, @Param("password")String password);

    对应的Mapper.xml

    <select id=" selectUser" resultMap="BaseResultMap">  
       select * from t_user where user_name = #{name,jdbcType=VARCHAR} and user_password=#{password,jdbcType=VARCHAR}  
    </select>

    个人觉得三种之中这种可读性最好,建议双引号中的值和变量名保持一致

    第一种占位符不够直观!

  • 相关阅读:
    省选模拟27
    省选模拟26
    省选模拟25
    省选模拟23
    PHP 各个框架的优缺点(超详细)
    windows 下安装中文版Git
    支付宝APP支付里设置应用网关和授权回调地址是不必填的
    linux服务器下安装phpstudy 如何命令行进入mysql
    js金钱转大写
    多个 (li) 标签如何获取获取选中的里面的某个特定值??
  • 原文地址:https://www.cnblogs.com/winner-0715/p/8444898.html
Copyright © 2011-2022 走看看