小编给大家分享一下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)BEGINIF flag = 0 THENSELECTB.user_business_id businessId,B.shop_name,B.total_score,B.shop_logo,B.average_consume,D.category_name,B.shop_addressFROMuser_business_commodity ALEFT JOIN user_business B ON B.user_business_id = A.user_business_idLEFT JOIN user_business_category C ON C.business_id = B.user_business_idLEFT JOIN service_category D ON D.category_id = C.category_one_idWHERE1 = 1ANDIF (categoryName IS NOT NULLAND LENGTH(TRIM(categoryName)) > 0,D.category_name = categoryName,1 = 1)ANDIF (priceBegin != 0,B.average_consume >= priceBegin,1 = 1)ANDIF (priceEnd != 0,B.average_consume <= priceEnd,1 = 1)ANDIF (commodityName IS NOT NULLAND LENGTH(TRIM(commodityName)) > 0,A. NAME LIKE concat('%', commodityName, '%'),1 = 1)AND B.is_delete = 0AND B.shop_setup_state = 1AND A.is_delete = 0AND C.is_delete = 0AND D.is_delete = 0GROUP BYA.user_business_idORDER BYCASE intelligenceSortWHEN 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;ELSESELECTB.user_business_id businessId,B.shop_name,B.total_score,B.shop_logo,B.average_consume,D.category_name,B.shop_addressFROMuser_business_commodity ALEFT JOIN user_business B ON B.user_business_id = A.user_business_idLEFT JOIN user_business_category C ON C.business_id = B.user_business_idLEFT JOIN service_category D ON D.category_id = C.category_two_idWHERE1 = 1ANDIF (categoryName IS NOT NULLAND LENGTH(TRIM(categoryName)) > 0,D.category_name = categoryName,1 = 1)ANDIF (priceBegin != 0,B.average_consume >= priceBegin,1 = 1)ANDIF (priceEnd != 0,B.average_consume <= priceEnd,1 = 1)ANDIF (commodityName IS NOT NULLAND LENGTH(TRIM(commodityName)) > 0,A. NAME LIKE concat('%', commodityName, '%'),1 = 1)AND B.is_delete = 0AND B.shop_setup_state = 1AND A.is_delete = 0AND C.is_delete = 0AND D.is_delete = 0GROUP BYA.user_business_idORDER BYCASE intelligenceSortWHEN 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存储过程查询数据的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!