mybatis-plus 自定义SQL,XML形式,传参的几种方式
前提说明
在使用 mybatis-plus 进行操作数据库,有一部分比较复杂的操作需要写SQL语句,这样就会涉及到传参数。下面记载一下我遇到的几种传参数情况。如果有更好的可以留言,继续完善。
所涉及文件
自定义SQL涉及到两种类型的文件:###Mapper.java 和 ###Mapper.xml 。这两种文件都是mybatis-plus自动生成的。
例如下面的例子:
- TGrouponMapper.java文件的内容
public interface TGrouponMapper extends BaseMapper<TGroupon> {
/**
* @description: 获取用户参加的团购信息
* @author: haojg
* @date: 2020/6/18 19:48
* @return:
*/
IPage<TGroupon> selectUserJoinGroupon(Page<?> page, @Param("userId") Long userId);
}
- TGrouponMapper.xml文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hddata.xpt.db.mapper.TGrouponMapper">
<select id="selectUserJoinGroupon" resultType="TGroupon">
SELECT g.*, ref.join_status, ref.join_name, ref.join_phone, ref.user_id as join_user_id
FROM t_groupon g, t_groupon_user_ref ref
WHERE g.id = ref.groupon_id
AND g.is_delete != 1
AND ref.user_id = #{userId}
ORDER BY g.create_time desc
</select>
</mapper>
传参类型说明
1.Java代码中使用QueryWrapper动态拼装SQL
Java代码中使用QueryWrapper动态拼装SQL后,最后在马Mapper.xml文件中使用。这种情况适用于where条件比较复杂,分支比较多的情况,更多情况自己品味吧。直接上代码如下:
- Mapper.java文件内容:
public interface TIdentityDocumentInfoMapper extends BaseMapper<TIdentityDocumentInfo> {
/**
* @Description: 根据身份证和医院患者Id获取系统患者Id等信息
* @Author: Haojg on 2019/8/7 23:37
* @return:
*/
List<TIdentityDocumentInfo> getPatientByIdAndPat(@Param(Constants.WRAPPER) QueryWrapper<TIdentityDocumentInfo> wrapper);
}
- Mapper.xml文件内容: ew.sqlSegment 是固定的写法,请注意。
<select id="getPatientByIdAndPat" resultType="TIdentityDocumentInfo">
SELECT t1.*, t3.*
FROM t_medical_card_info t1, t_identity_document_info t3
WHERE t1.identity_document_id = t3.identity_document_id
AND ${ew.sqlSegment}
</select>
- 调用接口方法:
这里通过Java直接拼接where语句。
QueryWrapper<TIdentityDocumentInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("t3.id", IdCard);
queryWrapper.eq("t1.hospital_id", hosId);
List<TIdentityDocumentInfo> queryList = tIdentityDocumentInfoMapper.getPatientByIdAndPat(queryWrapper);
2.简单类型参数(如String,Long,Integer等),适用于固定且确定的参数
这种情况直接上代码。
- Mapper.java文件内容,通过 @Param 进行参数设置。
IPage<TGroupon> selectUserJoinGroupon(Page<?> page, @Param("userId") Long userId);
- Mapper.xml文件内容:直接使用参数 userId
<select id="selectUserJoinGroupon" resultType="TGroupon">
SELECT g.*, ref.join_status, ref.join_name, ref.join_phone, ref.user_id as join_user_id
FROM t_groupon g, t_groupon_user_ref ref
WHERE g.id = ref.groupon_id
AND g.is_delete != 1
AND ref.join_status != 0
AND ref.user_id = #{userId}
ORDER BY g.create_time desc
</select>
3.参数传入类型为Class类,或mybatis-plus生成的 entity类
- Mapper.java文件内容: **HoRefunds ** 是数据库中表映射成的entity类
int updateRefundOk(@Param("subTable") String subTable, @Param("hoRefunds") HoRefunds hoRefunds);
- Mapper.xml文件内容中, 如下面的代码中: hoRefunds.refundResponse , hoRefunds 是 entity类, refundResponse 是字段名称。
<update id="updateRefundOk">
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}
</update>
4.参数传入类型为Map类型的数据
当entity类不满足情况的时候,可以使用Map类型的数据,具体例子如下。
- Mapper.java文件内容: paramsMap 为Map类型
public int getPushHistoryIsExsit(Map<String, Object> paramsMap);
- Mapper.xml文件内容中,
01,标明传入参数的类型 ** parameterType=“java.util.Map” **
02,直接使用传入参数中的Key, 如:step , cardId, notifyType
<select id="getPushHistoryIsExsit" parameterType="java.util.Map" resultType="int">
select count(t1.id) from hcustom_his_push t1
inner join hcustom_notify_config t2
on t1.notify_config_id = t2.id
and t2.notify_steps = #{step} and t2.notify_type = #{notifyType} and t1.medical_card_id = #{cardId}
</select>
- 调用实例
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("cardId", cardId);
paramsMap.put("step", step);
paramsMap.put("notifyType", notifyType);
int pushCount = hcustomHisPushMapper.getPushHistoryIsExsit(paramsMap);
我目前就使用了这么多,如果大家还有更多用法,欢迎交流!!!