zoukankan      html  css  js  c++  java
  • MyBatis传入多个参数的处理方法

    一、单个参数

    1. Java Mapper接口

    public List<XXBean> getXXBeanList(@param("id")String id);  

    2. XML Mapper

    <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">
        select t.* from tableName t where t.id= #{id}  
    </select>  

    二、多参数

    方案1:

    1. Java Mapper接口

    public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

    2. XML Mapper

    <select id="getXXXBeanList" resultType="XXBean">
        select t.* from tableName where id = #{0} and name = #{1}  
    </select>  

    注意:#{index},索引从0开始

    方案2:基于注解(推荐)

    1. Java Mapper接口

    public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);  

    2. XML Mapper

    <select id="getXXXBeanList" resultType="XXBean">
        select t.* from tableName where id = #{id} and name = #{code}  
    </select>  

    三、Map封装多参数

    1. Java Mapper接口

    public List<XXXBean> getXXXBeanList(HashMap map);  

    2. XML Mapper

    <select id="getXXXBeanList" parameterType="java.util.Map" resultType="XXBean">
      select 字段... from XXX where id=#{xxId} code = #{xxCode}  
    </select>  

    map中key可在#{}中直接使用

    四、List封装in

    1. Java Mapper接口

    public List<XXXBean> getXXXBeanList(List<String> list);

    2. XML Mapper

    <select id="getXXXBeanList" resultType="XXBean">
      select 字段... from XXX where id in
      <foreach collection="list" item="item" index="index" open="(" separator="," close=")">  
        #{item}  
      </foreach>  
    </select>

    foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')

    五、接口方法只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法

    1. Java代码

    List<String> list = new ArrayList<String>();
    Map<String, Object> map = new HashMap<String, Object>();
    list.add("1");
    list.add("2");
    map.put("list", list); //网址id
    map.put("siteTag", "0");//网址类型

    2. Java Mapper接口

    public List<SysWeb> getSysInfo(Map<String, Object> map2);

    3. XML Mapper

    <select id="getSysInfo" parameterType="java.util.Map" resultType="SysWeb">
        select t.sysSiteId, t.siteName, t1.mzNum as siteTagNum, t1.mzName as siteTag, t.url, t.iconPath
        from TD_WEB_SYSSITE t
        left join TD_MZ_MZDY t1 on t1.mzNum = t.siteTag and t1.mzType = 10
        WHERE t.siteTag = #{siteTag } and t.sysSiteId not in 
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/yifanSJ/p/9096018.html
Copyright © 2011-2022 走看看