MyBatis3-传递多个参数(Multiple Parameters)
传递多个参数一般用在查询上,比如多个条件组成的查询,有以下方式去实现:
版本信息:
MyBatis:3.4.4
1、自带方法
<select id="getUserArticlesByLimit" resultMap="resultUserArticleList">
select user.id,user.userName,user.userAddress,article.id as aid,article.title,article.content from user,article where user.id=article.userid and user.id=#{arg0} limit #{arg1},#{arg2}
</select>
public List<Article> getUserArticlesByLimit(int id,int start,int limit);
List<Article> articles=userMapper.getUserArticlesByLimit(1,0,2);
说明,arg0...也可以写成param0...
2、直接传递对象
<select id="dynamicIfTest" parameterType="Article" resultType="Article">
select * from article where 1 = 1
<if test="title != null">
and title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
limit 1
</select>
public Article dynamicIfTest(Article article);
Article inArticle = new Article();
inArticle.setTitle("test_title");
Article outArticle = userOperation.dynamicIfTest(inArticle);
3、使用@Praam标注
<select id="dynamicChooseTest" resultType="Article">
select * from article where 1 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and tile = "test_title"
</otherwise>
</choose>
</select>
public Article dynamicChooseTest(
@Param("title")
String title,
@Param("content")
String content);
Article outArticle2 = userOperation.dynamicChooseTest("test_title",null);
说明:这种方法同样可以用在一个参数的时候。
4、使用HashMap
<select id="getArticleBeanList" resultType="ArticleBean">
select * from article where id = #{id} and name = #{code}
</select>
说明:parameterType="hashmap"可以不用写。
public List<ArticleBean> getArticleBeanList(HashMap map);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id", 1);
map.put("code", "123");
List<Article> articless3 = userOperation.getArticleBeanList(map);
特殊的HashMap示例,用在foreach节点:
<select id="dynamicForeach3Test" resultType="Article">
select * from article where title like "%"#{title}"%" and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
public List<Article> dynamicForeach3Test(Map<String, Object> params);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "title");
map.put("ids", new int[]{1,3,6});
List<Article> articless3 = userOperation.dynamicForeach3Test(map);
5、List结合foreach节点一起使用
<select id="dynamicForeachTest" resultType="Article">
select * from article where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
public List<Article> dynamicForeachTest(List<Integer> ids);
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(3);
ids.add(6);
List<Article> articless = userOperation.dynamicForeachTest(ids);
6、数组结合foreach节点一起使用
<select id="dynamicForeach2Test" resultType="Article">
select * from article where id in
<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
public List<Article> dynamicForeach2Test(int[] ids);
int[] ids2 = {1,3,6};
List<Article> articless2 = userOperation.dynamicForeach2Test(ids2);
参考:
http://www.yihaomen.com/article/java/426.htm
MyBatis3-传递多个参数(Multiple Parameters)的更多相关文章
- C++ Templates(1.3 多模板参数 Multiple Template Parameters)
返回完整目录 目录 1.3 多模板参数 Multiple Template Parameters 1.3.1 为返回类型设置模板参数参数 Template Parameters for Return ...
- Mybatis(二)参数(Parameters)传递
Mybatis参数(Parameters)传递 1..单个参数 可以接受基本类型,对象类型,集合类型的值.这种情况MyBatis可直接使用这个参数,不需要经过任何处理. <!-- 根据id查询 ...
- Dao层向sql语句传递多个参数
手动封装: serviceImpl层 Map<String, Object> params = new HashMap<String, Object>(2);params.pu ...
- 如何为Windows Forms应用程序添加启动参数(Start-Up Parameters)
很多场合下,我们需要通过命令行或者快捷方式在Windows Forms程序启动时向其传递参数. 这些参数可能是用来加载某一个文档,或者是应用程序的初始化配置文件. 特别是对那些需要高度自定义配置的大程 ...
- Can't bind multiple parameters ('header' and 'parameters') to the request's content.
2019-01-23 15:46:29.012+08:00 ERROR [6]: System.InvalidOperationException: Can't bind multiple param ...
- 转 zabbix 自动发现和 zabbix自定义用户key与参数User parameters
########31 https://www.cnblogs.com/yjt1993/p/10883345.html 1.概念 在配置Iterms的过程中,有时候需要对类似的Iterms进行添加,这些 ...
- 【问题】Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数
[问题]Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数. [解决]直接对变量加引号,如: <button onclick="deleteProduct('@ ...
- C#使用结构来传递多个参数
当参数超过5个时,建议用结构来传递多个参数. 示例代码如下: public struct MyStruct { public string str; public int number; } clas ...
- 转载 C#中使用结构来传递多个参数
C#中当参数超过5个时,建议用结构来传递多个参数. 示例代码如下: public struct MyStruct { public string str; public int number; } c ...
随机推荐
- 转 phpmyadmin操作技巧:如何在phpmyadmin里面复制mysql数据库?
对于每一个站长而言,都会遇到要进行网站测试的时候.这个时候,往往需要备份数据库.如果按照一般的操作方式,都是先把数据库导出并备份到本地,然后再服务器上测试.如果一切正常还好,一旦出了问题,就又得把数据 ...
- SqlServer数据库(可疑)解决办法
-- 当数据库发生这种操作故障时,可以按如下操作步骤可解决此方法,打开数据库里的Sql 查询编辑器窗口,运行以下的命令. --1.修改数据库为紧急模式 ALTER DATABASE Zhangxing ...
- poj2289 Jamie's Contact Groups
思路: 二分+最大流.实现: #include <stdio.h> #include <stdlib.h> #include <limits.h> #include ...
- UVM基础之-------uvm report机制的使用
后面的例子我会继续补充: 1. 因为uvm默认定义的message格式比较长,非常不利于debug过程中的分析使用,一般情况下,开始使用uvm,都要利用uvm_report_server重新定义mes ...
- CentOS7上安装稻壳CMS
CentOS7上安装稻壳CMS 1, 安装用途 为了给某公司建设一个小型网站,租用了一个阿里云ECS服务器,最基础的硬件配置,因此选择了CentOS7操作系统. 稻壳CMS(docCMS)源于深喉咙C ...
- mongo 3.4分片集群系列之二:搭建分片集群--哈希分片
这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...
- Ubuntu安装配置Charles,抓取http网络请求包
http://blog.csdn.net/lylddinghffw/article/details/75322262
- grunt 全局使用
grunt 不同地方使用时需要将插件下载的当前文件夹,这是因为查找模块时是当前路径,这会造成多个工程使用时会需要下载多次,而这些东西又不应该存在于工程之中,所以应该将所有模块全局安装,然后在工程下面只 ...
- 北大ACM(POJ1019-Number Sequence)
Question:http://poj.org/problem?id=1019 问题点:打表. Memory: 392K Time: 16MS Language: C++ Result: Accept ...
- day16-常用模块I(time、datetime、random、os、sys、json、pickle)
目录 time模块 datetime模块 random模块 os模块 sys模块 json模块与pickle模块 json pickle time模块 time模块一般用于不同时间格式的转换,在使用前 ...