mybatis中的多参传递,包含实体的多参传递
来源:互联网 发布:c语言进制转换程序 编辑:程序博客网 时间:2024/06/05 04:25
最近做的项目里面有一个包含实体的多参传递,于是上网查了一些资料,发现mybatis中的多参传递的资料是比较多的,但是包含实体的多参传递就比较少了。查找资料后终于解决,写个日志总结下,方便日后使用。
多参传递一般有三种方法:
以下三种方法内容来自http://www.2cto.com/database/201409/338155.html
第一种方案
DAO层的函数方法
1
Public
User
selectUser(String name, String area);
1
2
3
<
select
id=
"selectUser"
resultMap=
"BaseResultMap"
>
select
*
from
user_user_t
where
user_name = #{0}
and
user_area=#{1}
</
select
>
其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。
第二种方案
此方法采用Map传多参数.
Dao层的函数方法
1
Public
User
selectUser(Map paramMap);
1
2
3
<
select
id=
" selectUser"
resultMap=
"BaseResultMap"
>
select
*
from
user_user_t
where
user_name = #{userName,jdbcType=
VARCHAR
}
and
user_area=#{userArea,jdbcType=
VARCHAR
}
</
select
>
Service层调用
1
2
3
4
5
Private
User
xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put(“userName”,”对应具体的参数值”);
paramMap.put(“userArea”,”对应具体的参数值”);
User
user
=xxx. selectUser(paramMap);}
个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。
第三种方案
Dao层的函数方法1
Public
User
selectUser(@param("user")User user, @param(“userName”)String name,@param(“userArea”)String area);
1
2
3
<
select
id=
" selectUser"
resultMap=
"BaseResultMap"
>
select
*
from
user_user_t
where
user_name = #{userName,jdbcType=
VARCHAR
}
and
user_area=#{userArea,jdbcType=
VARCHAR
}
</
select
>
个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案。
包含实体的多参传递:
我使用的是第三种方法解决的这个问题,也就是加@Param注解来解决,我的是时间区间的判断,具体代码实例如下:
1
Public
User
selectUser(@param("user")User user, @param(“start”)String start,@param(“end”)String end);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<
select
id=
" selectUser"
resultMap=
"BaseResultMap"
>
select
*
from
useru
<where>
<if test="user.userId != null and user.userId != ''">
u.user_id = #{user.userId}
</if>
<if test="start != null and start != ''">
and u.created >= start
</if>
<if test="end != null and end != ''">
and u.created <= end
</if>
</where>
</
select
>
其中,黑色粗体u代表数据库表user,红色粗体user代表传入的user对象,即@param("user")中的user。我使用了mybatis中的where标签和if标签,有兴趣的话请参考http://blog.csdn.net/zenson_g/article/details/10137665。记得一定不要忘记and关键字。
新手,不喜勿喷。
阅读全文
0 0
- mybatis中的多参传递,包含实体的多参传递
- Mybatis多参数及实体对象传递
- Mybatis多参数及实体对象传递
- mybatis 传递多参数(不使用实体)
- Mybatis多参传递的四种解决方案
- mybatis多参数传递
- MyBatis多参数传递
- 【Mybatis】多参数传递
- MyBatis多参数传递
- Mybatis多参数传递
- mybatis实现传递多参数的问题
- mybatis传递多个参数的问题
- MyBatis传递多个参数的写法
- Mybatis的多参数传递问题
- MyBatis传递多个参数的方法
- MyBatis传递多个参数的方法
- MyBatis中的参数传递
- mybatis多参传递方式(三种)
- 百度RSS整理
- html 基础入门
- web移动端浮层滚动带动底层元素滚动问题解决
- 如何查看端口被哪个程序应用
- Hide Delegate(隐藏“委托关系”)
- mybatis中的多参传递,包含实体的多参传递
- uva100解题报告
- 刷清橙OJ--A1095.教室排课
- mysql 5.7 this is incompatible with sql_mode=only_full_group_by 问题
- 算法概论课后习题
- 全屏、沉浸式、fitSystemWindow使用及原理分析:全方位控制“沉浸式”的实现
- 记一些常用又不好记的东西
- 51nod 1594 Gcd and Phi
- Android Listview优化详解