zoukankan      html  css  js  c++  java
  • Mybatis 多参处理

    一.Mybatis 极简入门

    二.Mybatis 级联查询

    三.Mybatis 延迟加载

    四.Mybatis 缓存策略

    五.Mybatis 动态SQL

    本篇:Mybatis 多参处理

    1. 一个参数怎么处理?
    <select id="findClassedByID" parameterType="long" resultType="com.ibuyi.mybatis.entity.Classes">
            select * from classes where id=#{hello}
    </select>
    

    当只有一个参数时,#{}里面可以任意填写都能够取到传递进来的参数。

    1. 两个或多个参数怎么处理?
    import com.ibuyi.mybatis.entity.User;
    
    public interface UserDAO {
        User findUserByCityAndType(String city,int Type);
    
    }
    

    第一种使用param1,param2…或arg0,arg1…

    <select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
        select * from user where location=#{param1} and type=#{param2}
    </select>
    

    第二种使用Map,传入一个Map即可

    public interface UserDAO {
        User findUserByCityAndType(Map<String,Object> map);
    
    }
    
    
    <select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
        select * from user where location=#{location} and type=#{type}
    </select>
    

    第三种使用注解:

    public interface UserDAO {
        User findUserByCityAndType(@Param("location")  String location,@Param("type") int type);
    
    }
    
    
    <select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
        select * from user where location=#{location} and type=#{type}
    </select>
    

    如果我们使用Collection或者List或者array传递参数,应该怎么取出来呢?

    public interface UserDAO {
        User findUserByCityAndType(List<Integer> list);
    
    }
    
    <select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
        select * from user where id=#{list[0]}
    </select>
    
    public interface UserDAO {
        User findUserByCityAndType(int[] ids);
    
    }
    
    
    <select id="findUserByCityAndType" resultType="com.ibuyi.mybatis.entity.User">
        select * from user where id=#{array[0]}
    </select>
    
  • 相关阅读:
    poj 3264(RMQ或者线段树)
    LCA上的RMQ模板算法
    LCA离线算法Tarjan的模板
    poj 1330(初探LCA)
    hdu 3367(与最大生成树无关。无关。无关。重要的事情说三遍+kruskal变形)
    hdu 4496(并查集逆向添边)
    hdu 1829(继续扩展并查集)
    poj 1182 (扩展并查集)
    hdu 3038(扩展并查集)
    hdu 3371(kruskal)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13309400.html
Copyright © 2011-2022 走看看