mybatis传参总结
注:文章引用部分 mybatis传递参数总结文章内容
一、单个参数
1、基本数据类型
(1)直接使用
List<ChargeRuleDO> tests(long id);
<select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{id}
</select>
#{}中的参数名与方法中的参数名不一定一致,仅起到占位符作用
(2)使用注解 (最为直接不易出错)
List<ChargeRuleDO> tests(@Param("aid") long bid);
<select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{aid}
</select>
#{}中的参数名与方法中的@Param()里的参数名一致
2、复杂数据类型(这里主要是指Java实体类)
(1)直接使用
List<ChargeRuleDO> tests(TestQO testQO);
<select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{id} and t.rule_type=#{ruleType}
</select>
#{}中的参数名与方法中的参数的属性名一致
(2)使用注解
List<ChargeRuleDO> tests(@Param("atestQO") TestQO btestQO);
<select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{atestQO.id} and t.rule_type=#{atestQO.ruleType}
</select>
#{}中的参数名与方法中的@Param()里的参数对应的属性名一致,而且必须写成"#{atestQO.id}“的格式,不能简写成”#{id}".
二、多个参数
1、基本数据类型
(1)直接使用
List<ChargeRuleDO> tests(long id,String ruleType);
<select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{0} and t.rule_type=#{1}
</select>
这里改用#{index},其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。直接写参数名字或者其他进行占位会报Parameter 'XXX' not found. Available parameters are [1, 0, param1, param2]这样的错误
(2)使用注解
List<ChargeRuleDO> tests(@Param("id") long id,@Param("ruleType") String ruleType);
<select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{id} and t.rule_type=#{ruleType}
</select>
#{}中的参数名与方法中的@Param()里的参数名一致
2、复杂数据类型
(1)直接使用
尝试了两种取参数的方法都不行
(2)使用注解
List<ChargeRuleDO> tests(@Param("testQO")TestQO testQO,@Param("testQO2")TestQO2 testQO2);
<select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{testQO.id} and t.rule_type=#{testQO.ruleType} and t.user_id=#{testQO2.uesrId}
</select>
#{}中的参数名与方法中的@Param()里的参数对应的属性名一致,而且必须写成"#{testQO.id}“的格式,不能简写成”#{id}".
3、基本数据类型与复杂数据类型混合使用
因为在多参数的情况下,复杂数据类型不能直接使用,所以这里主要尝试基本数据类型直接使用并且复杂数据类型使用注解这一种情况
List<ChargeRuleDO> tests(long id,String ruleType,@Param("testQO2")TestQO2 testQO2);
<select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id = #{0} and t.rule_type=#{1} and t.user_id=#{testQO2.uesrId}
</select>
4、List参数
List<ChargeRuleDO> tests(List<long> list);
<select id="tests" resultType="com.xxx.bean.ChargeRuleDO">
select * from t_charge_rule t where t.id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>
四、if test=" "传值
在mybatis if test语句中,是使用#{}还是${} 或是其他?结论是两者都不可以在test中使用
小提一下:
#{}表示一个占位符,向占位符输入参数,mybatis自动进行java类型和jdbc类型的转换。
程序员不需要考虑参数的类型,比如:传入字符串,mybatis最终拼接好的sql就是参数两边加单引号。
#{}接收pojo数据,可以使用OGNL解析出pojo的属性值
表示sql的拼接,通过{}
表示sql的拼接,通过表示sql的拼接,通过{}接收参数,将参数的内容不加任何修饰拼接在sql中。
${}也可以接收pojo数据,可以使用OGNL解析出pojo的属性值
缺点:不能防止sql注入。
1、 String类型单个参数传递,使用_parameter
public List<Long> listAuthTeachersByTeacherName(String teacherName);
<select id="listAuthTeachersByTeacherName" parameterType="string" resultType="long">
select uid from auth_teacher where status = 20
<if test="_parameter != null and _parameter != ''">
and real_name like concat('%',#{_parameter},'%')
</if>
order by apply_last_date asc
</select>
注意:这里的参数获取只能使用"_parameter",而不能使用teacherName,否则总是抛出There is no getter for property named ‘teacherName’ in 'class java.lang.String’的异常信息。
2、多值使用注解
public List<Course> querybyNameandTime(@Param("start")String start,@Param("end") String end, @Param("coursename") String coursename, @Param("fuzzquery") String fuzzquery);
<select id="querybyNameandTime" resultMap="ListCourseMapper">
select * from course
<where>
<if test="start != null and start !=''">
and date(course_starttime) between #{start} and #{end}
</if>
<choose>
<when test="coursename != null and coursename !='' and fuzzquery != null">
and course_name like '%' #{coursename} '%'
</when>
<when test="coursename!= null and coursename !='' and fuzzquery == null ">
and course_name = #{coursename}
</when>
<otherwise>
and 1=1
</otherwise>
</choose>
</where>
</select>
3、pojo可以直接使用属性
<select id="ListByChose" resultMap="StudentMapper">
SELECT * FROM student
<where>
<choose>
<when test=" name != null and name !=''">
and sname like '%' #{name} '%'
</when>
<when test=" age != null and age !='' and age > 0">
and age > #{age}
</when>
<when test=" score != null and score !='' and score > 0">
and score > #{score}
</when>
<otherwise>
1==2
</otherwise>
</choose>
</where>
</select>
4、数组和list
数组
<select id="ListByForeachArray" resultMap="StudentMapper" >
SELECT * FROM student
<where>
<if test="array.length > 0">
and id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
list
<select id="ListByForeachList" resultMap="StudentMapper" >
SELECT * FROM student
<where>
<if test="list.size > 0">
and id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
mybatis传参总结的更多相关文章
- 问题小记(MyBatis传参出现的小问题)
问题一:在MyBatis中注解@Param和ParameterType不能一起用,会报错Parameter 'XXX' not found. Available parameters are [1, ...
- Mybatis传参方式
Mybatis传多个参数(三种解决方案) 据我目前接触到的传多个参数的方案有三种. 第一种方案 DAO层的函数方法 ? 1 Public User selectUser(String name,St ...
- mybatis传参的几种方式
1,@Param @参考文章 @Select("select s_id id,s_name name,class_id classid from student where s_name= ...
- Java Mybatis 传参方式
一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...
- mybatis 传参是 list<string> 的注意事项
<!--付款 批量 修改账单状态--><update id="editbillpayALL" parameterType="java.util.List ...
- mybatis传参问题总结
一. 传入单个参数 当传入的是单个参数时,方法中的参数名和sql语句中参数名一致即可 List<User> getUser(int id); <select id="get ...
- php sql 类似 mybatis 传参
PHP sql 处理上,没有类似于 java mybatis 的工具,导致进行一些sql 处理时,会有诸多不便, 楼主抽时间写了一个 php 类似 mybatis 的sql 工具,省去了拼装sql 的 ...
- Mybatis传参- 被逗号分割的字符串
String ids = "1,2,3,4,5,6",如ids作为参数传递,查询list返回.mybatis用foreach处理并返回. SELECT * FROM yp_popu ...
- mybatis 传参为 Integer 时 ,Mapper 文件 中判断 条件 问题。
<if test="valiStatus==null || valiStatus=='' || valiStatus==4 "> b.work_permit_card_ ...
随机推荐
- 【bzoj2748】[HAOI2012]音量调节
设F[i][j]表示在第i首歌曲结束后,音量能否刚好为j 转移:F[i][j]=F[i][j-C[i]] or F[i][j+C[i]] 初始化:F[0][beginlevel]=true 最后在所有 ...
- java7-Fork/Join
Fork/Join 框架与传统线程池的区别采用“工作窃取”模式(work-stealing):当执行新的任务时它可以将其拆分分成更小的任务执行,并将小任务加到线程队列中,然后再从一个随机线程的队列中偷 ...
- YTU 2625: B 构造函数和析构函数
2625: B 构造函数和析构函数 时间限制: 1 Sec 内存限制: 128 MB 提交: 772 解决: 513 题目描述 在建立类对象时系统自动该类的构造函数完成对象的初始化工作, 当类对象 ...
- HDU 4850 Wow! Such String!
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4850 题意:给定一个N(1 ≤ N ≤ 500000),构造一个长度为N的小写字母字符串,要求所有长度大于 ...
- ios下使用overflow scroll情况下,到达最极端的情况时会拖动整个页面的解决办法
今天开发ipad webapp时,遇到个问题就是在支持内部滚动(overflow:scroll)的页面中,在滚到到最极端(最上或者最下时),会拖动整个页面,带来不好的用户体验. 方法一,从网上找到的: ...
- 统一微信公众号、小程序、APP的用户信息
上次接手一个项目需要整合公众号.小程序以及APP的用户,查阅了微信文档以及一些作者的文章,中间踩了不少坑,在此记录一下解决的流程. 要点 实现统一信息的有以下几点: 1. 在微信开放平台绑定需要 ...
- MySQL索引使用以及优化
优化后台业主评价服务人员运行缓慢. 案发现场:后台业主评价服务人员列表页以及搜索页运行缓慢.运行时间为24074ms. 排查过程: 1.代码开头加时间,结束加时间.看运行了多少秒. 2.给评价 ...
- 乐搏讲自动化测试- Python环境搭建(7)
Python的下载和安装 Python可应用于多平台包括 Linux 和 Mac OS X.你可以通过终端窗口输入 "python" 命令来查看本地是否已经安装Python以及Py ...
- java Class.getResource和ClassLoader.getResource
http://www.cnblogs.com/wang-meng/p/5574071.html http://blog.csdn.net/earbao/article/details/50009241 ...
- JavaScript--编程练习2
制作一个跳转提示页面: 要求: 1. 如果打开该页面后,如果不做任何操作则5秒后自动跳转到一个新的地址,如慕课网主页. 2. 如果点击“返回”按钮则返回前一个页面. 效果: 注意: 在窗口中运行该程序 ...