mybatis-plus 自定義SQL,XML形式,傳參的幾種方式


mybatis-plus 自定義SQL,XML形式,傳參的幾種方式
前提說明
所涉及文件
傳參類型說明
1.Java代碼中使用QueryWrapper動態拼裝SQL
2.簡單類型參數(如String,Long,Integer等),適用於固定且確定的參數
3.參數傳入類型為Class類,或mybatis-plus生成的 entity類
4.參數傳入類型為Map類型的數據
前提說明
在使用 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);
1
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);
1
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);
1
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);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM