zoukankan      html  css  js  c++  java
  • mybatis 传递多个查询参数

    方法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>


    #{}里面的数字代表你传入参数的顺序。

    这种方法不建议使用,sql层表达不直观,且一旦顺序调整容易出错。

    方法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>

    #{}里面的名称对应的是注解@Param括号里面修饰的名称。

    这种方法在参数不多的情况还是比较直观的,推荐使用。

    方法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>

    #{}里面的名称对应的是Map里面的key名称。

    这种方法适合传递多个参数,且参数易变能灵活传递的情况。

    方法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>

    #{}里面的名称对应的是User类里面的成员属性。

    这种方法很直观,但需要建一个实体类,扩展不容易,需要加属性

    使用map 传参 可读性差 ,导致可维护性和可扩展性差  杜绝使用 

    使用注解传参  直观明了  当参数较少一般小于5个的时候 建议使用 

    使用java bean 传参 当参数大于5个的时候使用,建议使用

  • 相关阅读:
    flask helloworld
    (16)centos7 日志文件
    (15)centos7 系统服务
    (14)centos7 进程管理
    (13)centos7 任务计划
    (12)centos7 环境变量配置
    [BZOJ2045]双亲数(莫比乌斯反演)
    bzoj2018 [Usaco2009 Nov]农场技艺大赛
    bzoj 1001 [BeiJing2006]狼抓兔子
    bzoj 5056: OI游戏 最短路树的计数
  • 原文地址:https://www.cnblogs.com/qin1993/p/11941831.html
Copyright © 2011-2022 走看看