• MyBatis - 参数传递


    一、单个参数

    public List getXXBeanList(@param("id")String id);  
    1. <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">
    2. select t.* from tableName t where t.id= #{id}
    3. select>
    • 方法名和ID一致
    • #{}中的参数名与方法中的参数名一致, 这里采用的是@Param这个参数,实际上@Param这个最后会被Mabatis封装为map类型的。
    • select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。

    二、多参数

    2.1 按注解(推荐)

    public List getXXXBeanList(@Param("id")String id, @Param("code")String code); 
    1. <select id="getXXXBeanList" resultType="XXBean">
    2. select t.* from tableName where id = #{id} and name = #{code}
    3. select>

    由于是多参数那么就不能使用parameterType, 这里用@Param来指定哪一个

    2.2 按序号

    public List getXXXBeanList(String xxId, String xxCode); 
    1. <select id="getXXXBeanList" resultType="XXBean">
    2. select t.* from tableName where id = #{0} and name = #{1}
    3. select>

    由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始

    2.3 Map封装

    public List<XXXBean> getXXXBeanList(HashMap map);  
    
    1. <select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">
    2. select 字段... from XXX where id=#{xxId} code = #{xxCode}
    3. select>

    其中hashmap是mybatis自己配置好的直接使用就行。

    map中key的名字是那个就在#{}使用那个

    2.4 List封装in

    注意:这里的collection是你集合的字段名。

    public List getXXXBeanList(List list);
    1. <select id="getXXXBeanList" resultType="XXBean">
    2. select 字段... from XXX where id in
    3. <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
    4. #{item}
    5. foreach>
    6. select>

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

    mybatis接受的参数分为:

    • 基本类型

    • 对象

    • List

    • 数组

    • Map

    标签的用法:

    • collection:要循环的集合
    • index:循环索引
    • item:集合中的一个元素(item和collection,按foreach循环理解)
    • open:以什么开始
    • close:以什么结束
    • separator:循环内容之间以什么分隔

    2.5 数组

    List selectByUserIdList(@Param("userIds")String[] userIds);

    注意:这里的collection是你集合的字段名。​​​​​​​

    1. <select id="selectByRoleIdList" resultMap="user" >
    2. SELECT * FROM user
    3. <if test="userIds!= null">
    4. where user_id in
    5. <foreach collection="userIds" item="userId" index="index" open="(" separator="," close=")" >
    6. #{userId,jdbcType=INTEGER}
    7. foreach>
    8. if>

    2.6 selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法

    将参数放入Map,再取出Map中的List遍历。如下:

    1. List list_3 = new ArrayList();
    2. list.add("1");
    3. list.add("2");
    4. Map map2 = new HashMap();
    5. map.put("list", list); //网址id
    6. map.put("siteTag", "0");//网址类型
    1. <select id="getSysInfo" parameterType="java.util.Map" resultType="SysWeb">
    2. 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
    3. <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
    4. #{item}
    5. foreach>
    6. select>
  • 相关阅读:
    YOLOv5训练自己的voc数据集
    生产者消费者模型
    3.前端调式
    Java架构
    CentOS 7 安装LibreOffice 7.4.0 过程
    [python]将多张图片合并为单个pdf文件
    11.15 - 每日一题 - 408
    深度学习之基础模型——长短时记忆网络LSTM
    spring boot 服务使用过程常见bug 解决
    vulnhub靶场—matrix-breakout-2-morpheus靶机
  • 原文地址:https://blog.csdn.net/hutuyaoniexi/article/details/127653514