ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Mybatis 多参处理

2020-02-29 12:37:37  阅读:205  来源: 互联网

标签:处理 type 多参 location Mybatis where select User


一.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>
ibuyi 发布了143 篇原创文章 · 获赞 81 · 访问量 3万+ 私信 关注

标签:处理,type,多参,location,Mybatis,where,select,User
来源: https://blog.csdn.net/weixin_43927892/article/details/104554276

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有