mysql創建簡單的存儲過程,mybaits使用存儲過程

在實際項目應用中,很多時候不需要存儲過程,但是當表的數據較爲分散又需要處理,往往使用sql很難解決,此時可以使用存儲過程。

如需要統計用戶表中,當天增加了多少用戶,當月增加了多少用戶,上月增加了多少用戶,佔整個用戶的百分比,此時使用一條sql將會變得很困難,若是在後臺程序計算,程序可能會寫的有點麻煩,直接使用存儲過程

給出一個曾經項目中本人寫的存儲過程,

create procedure proc_allUserTitle(in deptId int,in roleId int,in userId varchar(200),in drag varchar(1))
begin
	declare dayIncrease int default 0;
	declare upIncrease int default 0;
	declare monthIncrease int default 0;
	declare custper double default 0;
	declare total int default 0;
	declare yzCustmer int default 0;
	declare custmerPer int default 0;
	declare upMonth varchar(6);
	declare montht varchar(6);
	declare dayt varchar(10);
	select extract(year_month from curdate()) into montht;
	select substr(replace(date_sub(date_sub(date_format(now(),'%y%m%d'),interval extract( day from now())-1 day),interval 1 month),'-',''),1,6) into upMonth;
	select replace(curdate(),'-','') into dayt;
	if deptId>1&&roleId=1 then
		select count(0) from net_user where substr(user_createtime,1,8)=dayt and dep_id=deptId into dayIncrease;
		select count(0) from net_user where substr(user_createtime,1,6)=upMonth and dep_id=deptId into upIncrease;
		select count(0) from net_user where substr(user_createtime,1,6)=montht and dep_id=deptId into monthIncrease;
		select count(0) from net_user where dep_id=deptId into total;
		select count(0) from net_finance a,(select user_id as userCard,max(id)  as maxid from net_finance group by userCard) b where a.user_id=b.userCard and a.id=b.maxid and money_balance>0 and dep_id=depId into yzCustmer;
	elseif deptId=1&&roleId=1 then
		if drag='p' then
			select count(0) from net_user where substr(user_createtime,1,8)=dayt and role_id=roleId into dayIncrease;
		elseif drag='u' then
			select count(0) from net_user where substr(user_createtime,1,8)=dayt into dayIncrease;
		end if;
		select count(0) from net_user where substr(user_createtime,1,6)=upMonth and role_id=roleId into upIncrease;
		select count(0) from net_user where substr(user_createtime,1,6)=montht and role_id=roleId into monthIncrease;
		select count(0) from net_user where role_id=roleId into total;
		select count(0) from net_finance a,(select user_id as userCard,max(id)  as maxid from net_finance group by userCard) b where a.user_id=b.userCard and a.id=b.maxid and money_balance>0 into yzCustmer;
	 else
		select count(0) from net_user where substr(user_createtime,1,8)=dayt and user_id=userId into dayIncrease;
		select count(0) from net_user where substr(user_createtime,1,6)=upMonth and user_id=userId into upIncrease;
		select count(0) from net_user where substr(user_createtime,1,6)=montht and user_id=userId into monthIncrease;
		set total=0;
	end if;
	if total>0 then 
		set custmerPer=yzCustmer/total;
	end if;
	select dayIncrease,upIncrease,monthIncrease,custper;
end
本存儲過程不提供表結構,供參考

mybaits的配置文件中配置存儲過程

<mapper namespace="monitor.dao.net.FirstPageMapper">
  <select id="callFirstPage" parameterType="XXX.XXX.XX" statementType="CALLABLE" resultType="monitor.pojo.net.FirstPage">
  	call proc_firstPage(#{depId},#{roleId},#{userId});
  </select>
  
</mapper>

關鍵在與指名存儲過程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章