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>

关键在与指名存储过程

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