在使用 mybatis-plus 进行操作数据库,有一部分比较复杂的操作需要写SQL语句,这样就会涉及到传参数。下面记载一下我遇到的几种传参数情况。如果有更好的可以留言,继续完善。
自定义SQL涉及到两种类型的文件:###Mapper.java 和 ###Mapper.xml 。这两种文件都是mybatis-plus自动生成的。
例如下面的例子:
public interface TGrouponMapper extends BaseMapper {
/**
* @description: 获取用户参加的团购信息
* @author: haojg
* @date: 2020/6/18 19:48
* @return:
*/
IPage selectUserJoinGroupon(Page> page, @Param("userId") Long userId);
}
Java代码中使用QueryWrapper动态拼装SQL后,最后在马Mapper.xml文件中使用。这种情况适用于where条件比较复杂,分支比较多的情况,更多情况自己品味吧。直接上代码如下:
public interface TIdentityDocumentInfoMapper extends BaseMapper {
/**
* @Description: 根据身份证和医院患者Id获取系统患者Id等信息
* @Author: Haojg on 2019/8/7 23:37
* @return:
*/
List getPatientByIdAndPat(@Param(Constants.WRAPPER) QueryWrapper wrapper);
}
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("t3.id", IdCard);
queryWrapper.eq("t1.hospital_id", hosId);
List queryList = tIdentityDocumentInfoMapper.getPatientByIdAndPat(queryWrapper);
这种情况直接上代码。
IPage selectUserJoinGroupon(Page> page, @Param("userId") Long userId);
int updateRefundOk(@Param("subTable") String subTable, @Param("hoRefunds") HoRefunds hoRefunds);
update ho_refunds_${subTable} set
refund_request = #{hoRefunds.refundRequest},
refund_response = #{hoRefunds.refundResponse},
err_code = #{hoRefunds.errCode},
err_code_des = #{hoRefunds.errCodeDes}
where id = #{hoRefunds.id}
当entity类不满足情况的时候,可以使用Map类型的数据,具体例子如下。
public int getPushHistoryIsExsit(Map paramsMap);
Map paramsMap = new HashMap<>();
paramsMap.put("cardId", cardId);
paramsMap.put("step", step);
paramsMap.put("notifyType", notifyType);
int pushCount = hcustomHisPushMapper.getPushHistoryIsExsit(paramsMap);
我目前就使用了这么多,如果大家还有更多用法,欢迎交流!!!