zoukankan      html  css  js  c++  java
  • Mybatis传递多个参数的几种方式

    顺序传参法

    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层表达不直观,且一旦顺序调整容易出错。

    @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括号里面修饰的名称。

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

    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名称。

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

    PS:
    MyBatis传递map参数时,如果传递参数中没有对应的key值,在执行sql语句时默认取的是null

    例如:map中没有put “name”这个key,在sql中使用#{name}时,默认赋值null

    Java Bean传参法

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

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

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

    SELECT top 10000 tol.TuhuCost,tol.BaseCost,tol.OrderID AS OrderListOrderID,torp.ChildrenOrderId, torp.ParentOrderId,tol.pkid as OrderListID,torp.RelationshipType,tor.InstallShopID,tor.WareHouseID
    FROM dbo.tbl_OrderRelationship AS torp WITH(NOLOCK)
    LEFT JOIN dbo.tbl_OrderList AS tol WITH(NOLOCK)
    ON torp.ParentOrderId = tol.OrderID
    left JOIN vw_tbl_order as tor ON torp.ParentOrderId=tor.PKID
    WHERE torp.CreateDatetime>='2019-11-29'
    AND torp.RelationshipType = 1
    AND tor.InstallShopID IS NOT NULL
    AND tor.InstallShopID<>tor.WareHouseID
    AND tor.Status<>'7Canceled'
    AND tol.TuhuCost is not NULL
    ORDER BY torp.CreateDatetime DESC

    SELECT COUNT (1) as cnt FROM (
    SELECT DISTINCT tol.OrderID
    FROM dbo.tbl_OrderRelationship AS torp WITH(NOLOCK)
    LEFT JOIN dbo.tbl_OrderList AS tol WITH(NOLOCK)
    ON torp.ParentOrderId = tol.OrderID
    left JOIN vw_tbl_order as tor ON torp.ParentOrderId=tor.PKID
    WHERE torp.CreateDatetime>='2019-11-29'
    AND torp.RelationshipType = 1
    AND tor.InstallShopID IS NOT NULL
    AND tor.InstallShopID<>tor.WareHouseID
    AND tor.Status<>'7Canceled' AND tol.baseCost=0
    AND tol.TuhuCost is not NULL ) as t1

    SELECT COUNT(1) as cnt from tbl_orderlist as t1 with(nolock)  left join vw_tbl_order as t2 with(nolock) ON t1.orderid=t2.pkid WHERE t2.OrderDatetime>='2019-12-01'  AND t1.cost>0 AND t1.baseCost=0

  • 相关阅读:
    [bzoj3771]Triple——生成函数+容斥原理+FFT
    [bzoj3456]城市规划——分治FFT
    [uoj207]共价大爷游长沙——lct
    [bzoj2159]Crash 的文明世界——动态规划
    [luogu4707]重返现世——min-max容斥拓展+动态规划
    [bzoj4543][POI2014]Hotel加强版——长链剖分
    [bzoj1758][Wc2010]重建计划——长链剖分+线段树+分数规划
    [vijos]lxhgww的奇思妙想——长链剖分模板
    [bzoj3198][Sdoi2013]spring——容斥+哈希表
    AngularJs练习Demo15自定义服务
  • 原文地址:https://www.cnblogs.com/knowledgesea/p/11212842.html
Copyright © 2011-2022 走看看