您好,登錄后才能下訂單哦!
小編給大家分享一下Mybatis應用mysql存儲過程查詢數據的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
1.創建mysql存儲過程,這是個復雜查詢加上了判斷,比較復雜
CREATE PROCEDURE searchAllList ( IN tradingAreaId VARCHAR (50), IN categoryName VARCHAR (100), IN intelligenceSort TINYINT UNSIGNED, IN priceBegin DOUBLE, IN priceEnd DOUBLE, IN commodityName VARCHAR (200), IN flag TINYINT UNSIGNED ) BEGIN IF flag = 0 THEN SELECT B.user_business_id businessId, B.shop_name, B.total_score, B.shop_logo, B.average_consume, D.category_name, B.shop_address FROM user_business_commodity A LEFT JOIN user_business B ON B.user_business_id = A.user_business_id LEFT JOIN user_business_category C ON C.business_id = B.user_business_id LEFT JOIN service_category D ON D.category_id = C.category_one_id WHERE 1 = 1 AND IF ( categoryName IS NOT NULL AND LENGTH(TRIM(categoryName)) > 0, D.category_name = categoryName, 1 = 1 ) AND IF ( priceBegin != 0, B.average_consume >= priceBegin, 1 = 1 ) AND IF ( priceEnd != 0, B.average_consume <= priceEnd, 1 = 1 ) AND IF ( commodityName IS NOT NULL AND LENGTH(TRIM(commodityName)) > 0, A. NAME LIKE concat('%', commodityName, '%'), 1 = 1 ) AND B.is_delete = 0 AND B.shop_setup_state = 1 AND A.is_delete = 0 AND C.is_delete = 0 AND D.is_delete = 0 GROUP BY A.user_business_id ORDER BY CASE intelligenceSort WHEN 1 THEN 'B.total_order DESC' WHEN 2 THEN 'B.total_score DESC' WHEN 3 THEN 'B.create_time DESC' ELSE 'B.create_time ASC' END; ELSE SELECT B.user_business_id businessId, B.shop_name, B.total_score, B.shop_logo, B.average_consume, D.category_name, B.shop_address FROM user_business_commodity A LEFT JOIN user_business B ON B.user_business_id = A.user_business_id LEFT JOIN user_business_category C ON C.business_id = B.user_business_id LEFT JOIN service_category D ON D.category_id = C.category_two_id WHERE 1 = 1 AND IF ( categoryName IS NOT NULL AND LENGTH(TRIM(categoryName)) > 0, D.category_name = categoryName, 1 = 1 ) AND IF ( priceBegin != 0, B.average_consume >= priceBegin, 1 = 1 ) AND IF ( priceEnd != 0, B.average_consume <= priceEnd, 1 = 1 ) AND IF ( commodityName IS NOT NULL AND LENGTH(TRIM(commodityName)) > 0, A. NAME LIKE concat('%', commodityName, '%'), 1 = 1 ) AND B.is_delete = 0 AND B.shop_setup_state = 1 AND A.is_delete = 0 AND C.is_delete = 0 AND D.is_delete = 0 GROUP BY A.user_business_id ORDER BY CASE intelligenceSort WHEN 1 THEN 'B.total_order DESC' WHEN 2 THEN 'B.total_score DESC' WHEN 3 THEN 'B.create_time DESC' ELSE 'B.create_time ASC' END; END IF; END;
2.查看存儲過程是否創建成功:
show procedure status;
3.sqlMapper文件:
<select id="searchAllList1" parameterMap="searchAllListMap" statementType="CALLABLE" resultType="com.dongjia168.platform.vo.erp.crm.BusinessShopResp"> CALL searchAllList(#{tradingAreaId},#{categoryName},#{intelligenceSort},#{priceBegin},#{priceEnd},#{commodityName},#{flag}); </select>
<parameterMap id="searchAllListMap" type="com.dongjia168.platform.vo.erp.crm.BusinessShopReq"> <parameter property="tradingAreaId" jdbcType="VARCHAR" mode="IN"/> <parameter property="categoryName" jdbcType="VARCHAR" mode="IN"/> <parameter property="intelligenceSort" jdbcType="INTEGER" mode="IN"/> <parameter property="priceBegin" jdbcType="DOUBLE" mode="IN"/> <parameter property="priceEnd" jdbcType="DOUBLE" mode="IN"/> <parameter property="commodityName" jdbcType="VARCHAR" mode="IN"/> <parameter property="flag" jdbcType="INTEGER" mode="IN"/> </parameterMap>
其他和直接調用sql語句一樣了
以上是“Mybatis應用mysql存儲過程查詢數據的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。