zoukankan      html  css  js  c++  java
  • Mybatis传递多个参数

      当Mybatis入参只有一个,且为基本类型时,需要parameterType,mapper中变量名可以随便写,#{id},#{value},使用#{value}会多些。

      当入参大于1个时,且为基本类型时,不需要parameterType,mapper直接按照#{name},#{age}的方式会报错。

      方法1:顺序传参法

    public User selectUser(String name, int deptId);
    
    <select id="selectUser" resultMap="UserResultMap">
        select * from user
        where user_name = #{0} and dept_id = #{1}
    </select>

      或者#{0},#{1}换成param1,param2。select * from user where user_name = #{param1} and dept_id =#{param2}

      方法2:@Param注解传参法

    public User selectUser(@Param("userName") String name, int @Param("deptId") deptId);
    
    <select id="selectUser" resultMap="UserResultMap">
        select * from user
        where user_name = #{userName} and dept_id = #{deptId}
    </select>

      方法3:Map传参法

    public User selectUser(Map<String, Object> params);
    
    <select id="selectUser" parameterType="java.util.Map" resultMap="UserResultMap">
        select * from user
        where user_name = #{userName} and dept_id = #{deptId}
    </select>

      方法4:Java Bean传参法

    public User selectUser(User user);
    
    <select id="selectUser" parameterType="com.test.User" resultMap="UserResultMap">
        select * from user
        where user_name = #{userName} and dept_id = #{deptId}
    </select>

      使用Java Bean的方式会更好理解些,使用@Param和JDBI的方式更像些。

      当参数为单个时,不管是基本类型,包装类型,String都没什么特殊的。当返回值为List<User>时,resultMap的值是UserResultMap。

  • 相关阅读:
    安卓 Context 和 Application的关系
    Android Intent应用
    android launchmode(四种启动模式)应用场景及实例
    返回数据给上一个活动
    Intent传参数
    安卓activity生命周期
    如何将nideshop部署到本地
    navicat链接数据库错误2013
    数据库设计三大范式
    nodejs版本升级
  • 原文地址:https://www.cnblogs.com/lnlvinso/p/14534727.html
Copyright © 2011-2022 走看看