MyBatis——特殊传参问题小结
近期在写系统报表API的时候遇到MyBatis中的一些特殊写法:
1. 传入两个参数(一般情况下我们更多的是传入一个对象或者map)
public List<MarketVehicleModel> selectVehicleByMarketAndDealer(String marketActivityId,String dealerId);
其对应的xml写法不能想当然地写成:
<select id="selectVehicleByMarketAndDealer" parameterType="java.lang.String" resultType="bz.sunlight.entity.MarketVehicleModel">
SELECT DISTINCT mvm.vehicle_model_id as vehicleModelId,mvm.vehicle_model_code as vehicleModelCode,mvm.vehicle_model_name as vehicleModelName
from vehicle v,vehicle_model vm,Market_Vehicle_Model mvm where v.vehicle_model_id = vm.id and mvm.vehicle_model_id = vm.id
and mvm.market_activity_id = #{marketActivityId} and v.dealer_id=#{dealerId}
</select>
这样写,在编译打包期间不会报什么错,但到正式运行的时候会报告匹配不到 marketActivityId 和 dealerId .
正确的写法应该是:
<select id="selectVehicleByMarketAndDealer" parameterType="java.lang.String" resultType="bz.sunlight.entity.MarketVehicleModel">
SELECT DISTINCT mvm.vehicle_model_id as vehicleModelId,mvm.vehicle_model_code as vehicleModelCode,mvm.vehicle_model_name as vehicleModelName
from vehicle v,vehicle_model vm,Market_Vehicle_Model mvm where v.vehicle_model_id = vm.id and mvm.vehicle_model_id = vm.id
and mvm.market_activity_id = #{0} and v.dealer_id=#{1}
</select>
2. 上面的修改虽然暂时解决了可行性的问题,但这种写法并不可靠,如果遇到对两个参数进行条件判断的情况,还是会有问题,比如:
<select id="getSalesVolumeRanking" parameterType="java.lang.String"
resultType="java.util.HashMap">
SELECT sale_consultant_id AS consultantId,sale_consultant_name AS consultantName,COUNT(*) AS salesVolume
FROM sales_record
<where>
<if test="0 != null">
vehicle_model_id = #{0}
</if>
<if test="1 != null">
AND dealer_id = #{1}
</if>
</where>
GROUP BY sale_consultant_id
ORDER BY salesVolume DESC
</select>
这样的写法显然是不对的,这里有两种解决方案:
(1) 将多个参数组合成map后传入
List<HashMap<String,Object>> getSalesVolumeRanking(HashMap<String, String> map);
<select id="getSalesVolumeRanking" parameterType="java.util.HashMap"
resultType="java.util.HashMap">
SELECT sale_consultant_id AS consultantId,sale_consultant_name AS consultantName,COUNT(*) AS salesVolume
FROM sales_record
<where>
<if test="vehicleModelId != null">
vehicle_model_id = #{vehicleModelId}
</if>
<if test="dealerId != null">
AND dealer_id = #{dealerId}
</if>
</where>
GROUP BY sale_consultant_id
ORDER BY salesVolume DESC
</select>
(2) 给参数加@Param注解
List<HashMap<String,Object>> getSalesVolumeRanking(@Param("vehicleModelId") String vehicleModelId,@Param("dealerId") String dealerId);
<select id="getSalesVolumeRanking" parameterType="java.lang.String"
resultType="java.util.HashMap">
SELECT sale_consultant_id AS consultantId,sale_consultant_name AS consultantName,COUNT(*) AS salesVolume
FROM sales_record
<where>
<if test="vehicleModelId != null">
vehicle_model_id = #{vehicleModelId}
</if>
<if test="dealerId != null">
AND dealer_id = #{dealerId}
</if>
</where>
GROUP BY sale_consultant_id
ORDER BY salesVolume DESC
</select>
3. 传入单个参数,但涉及到对该参数的条件判断:
List<HashMap<String,Object>> getSatisfactionDegree(String vehicleModelId);
<select id="getSatisfactionDegree" parameterType="java.lang.String"
resultType="java.util.HashMap">
SELECT sale_consultant_id AS consultantId,ROUND(AVG(score)) satisfactionDegree
FROM reward_record
<if test="vehicleModelId != null and vehicleModelId != ''">
where vehicle_model_id = #{vehicleModelId}
</if>
GROUP BY sale_consultant_id
</select>
这种写法在运行时会报告There is no getter for property named 'vehicleModelId' 这时可将vehicleModelId换成关键字_parameter:
<select id="getSatisfactionDegree" parameterType="java.lang.String"
resultType="java.util.HashMap">
SELECT sale_consultant_id AS consultantId,ROUND(AVG(score)) satisfactionDegree
FROM reward_record
<if test="_parameter != null and _parameter != ''">
where vehicle_model_id = #{_parameter}
</if>
GROUP BY sale_consultant_id
</select>
4. 自定义order/group by clause或者传入int型参数时需将#{parameterName}引用改为${parameterName} :
HashMap<String, String> parameterMap = new HashMap<String, String>();
parameterMap.put("marketActivityCode", marketActivity.getCode());
parameterMap.put("dealerId", dealer.getId());
parameterMap.put("groupByClause","level_of_intent");
List<HashMap<String,Object>> currentIntentionCustomers = intentionCustomerMapper.getIntentionCustomerNumWithVehicle(parameterMap);
<select id="getIntentionCustomerNumWithVehicle" parameterType="java.util.HashMap"
resultType="java.util.HashMap">
SELECT c.level_of_intent,c.Level_Of_Intent_First,m.vehicle_model_id,m.vehicle_model_code,m.vehicle_model_name,COUNT(*) AS num
FROM intention_customer c,intention_models m
WHERE c.id = m.intention_customer_id
AND c.dealer_id = #{dealerId}
AND m.market_activity_code = #{marketActivityCode}
<if test="groupByClause != null">
GROUP BY ${groupByClause},vehicle_model_id
</if>
</select>
如果继续使用#{groupByClause}会使得执行的sql变成GROUP BY "level_of_intent" 导致得不到正确的分组结果.
MyBatis——特殊传参问题小结的更多相关文章
- Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String'
Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String' 一.发现问题 <select ...
- MyBatis中传参时为什么要用#{}
MyBatis中传参时为什么要用#{},这个问题和MyBatis如何防止SQL注入类似.不过在解释这个问题之前,先解释一下什么是SQL注入,还有些称作注入攻击这个问题. SQL注入就是SQL 对传入参 ...
- Mybatis的传参
最近重新温习了遍Mybatis ,觉得还是汇总一下比较好,方便自己以后的快速开发 最终要的一点事,自己写的话,记忆更加深刻: 首先自己先写了个静态块,防止代码冗余: private static Sq ...
- MyBatis:传参
MyBatis从入门到放弃二:传参 前言 我们在mapper.xml写sql,如果都是一个参数,则直接配置parameterType,那实际业务开发过程中多个参数如何处理呢? 从MyBatis API ...
- 180718-jar包执行传参使用小结
jar包执行时传参的使用姿势 虽说我们现在大多不太直接使用jar包运行方式,目前比较主流的是将自己的服务丢在某个容器中(如tomcat,jetty等)运行,比如我之前所属的电商公司,就是将项目打包为w ...
- Mybatis获取传参
取自 https://blog.csdn.net/weixin_38303684/article/details/78886375 mybatis中SQL接受的参数分为:(1)基本类型(2)对象(3 ...
- mybatis 复杂传参
1基本传参数 Public User selectUserWithCon(@param(“userName”)String name,@param(“userArea”)String area); ...
- mybatis参数传参、取值处理等
单个参数:mybatis不会做特殊处理 取值方式:#{参数名} 这里参数名不必与方法的形参名称一致,可以用任意参数名来接受实参 例子:方法:update(Integer id) sql映射文件取值#{ ...
- (后端)Mybatis中#{}和${}传参的区别及#和$的区别小结(转)
原文地址:https://www.cnblogs.com/zqr99/p/8094234.html 最近在用mybatis,之前用过ibatis,总体来说差不多,不过还是遇到了不少问题,再次记录下, ...
随机推荐
- 关于NSOperationQueue,一个容易让初学者误解的问题
凡是学习NSOperationQueue的人,都会遇到setMaxConcurrentOperationCount这个函数.在网上的许多博文中,都将setMaxConcurrentOperationC ...
- windows环境安装haproxy及初步配置负载均衡使用示例
安装HaProxy 首先需要下载windows环境下需要文件,这里下载的是一个别人编译好的一个文件,这里省去了编译的过程,使用的版本是haproxy-1.7.8. 下载后直接解压到对应的目录下.示例( ...
- Linux下安装php报错:libxml2 not found. Please check your libxml2 installation
ubuntu/debian: apt-get install libxml2-dev centos/redhat: yum install libxml2-devel
- Computer Vision_33_SIFT:Speeded-Up Robust Features (SURF)——2006
此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...
- 【2017-09-04】JavaWeb内置对象
Jsp页面中引入别的页面 include命令 <%@ include file="要引入的页面路径"%> 页面中的form表单提交方式: post: 提交内容不可见, ...
- 什么是微服务 Martin Fowler的microservices
https://martinfowler.com/articles/microservices.html https://martinfowler.com/microservices/ 微服务,最早由 ...
- SQL Server CET 通用表表达式 之 精典递归
SQL2005 Common Table Expressions(CET)即通用表表达式. SQLSERVER CET递归使用案例: 1.普通案例 表结构如下: ;WITH cet_depart ...
- 通过字节码分析Java方法的静态分派与动态分派机制
在上一次[https://www.cnblogs.com/webor2006/p/9723289.html]中已经对Java方法的静态分派在字节码中的表现了,也就是方法重载其实是一种静态分派的体现,这 ...
- pyside pyqt QPushbuttion 无边框 stylesheet border:none
pyside pyqt QPushbuttion 无边框 stylesheet border:none 在 stylesheet 中添加 border:none 即可 效果是字体到边缘之间的间隙为0, ...
- jmeter接口测试-使用rsa加密解密算法
本篇介绍jmeter 使用rsa算法进行加密参数 如果测试过程中,部分接口采用了rsa加密算法,我们的jmeter 也是可以直接拿来调用的,不需要开发配合去掉加密代码! 直接上代码 import or ...