java-maven-mybatis 调用sqlserver存储过程

作者在 2019-05-09 17:28:58 发布以下内容

最近做了一个项目,开发初期决定将业务逻辑分离出一部分在数据库中实现,而对于数据库可以做逻辑处理的地方就是存储过程,这样就涉及mybatis调用存储过程的问题,这里用的数据库是sqlserver,操作很简单,下面直接贴代码

一、dao.xml


<select id="selectProCheck" parameterType="com.nssolsh.boot.modular.system.model.ResultMessage" resultMap="BaseResultMapHead">
	exec dbo.execl_body_data_check @file_id =#{file_id},@after_month = #{after_month}
</select>



二、数据库存储过程


ALTER PROCEDURE [dbo].[execl_body_data_check]
  @file_id AS nvarchar,@after_month as nvarchar AS
--创建时间的游标,每次上传的数据的前一个月和当前月
declare date_cursor cursor for 
select concat(b.year_month,'#',c.year_month) year_month  from (
	select ROW_NUMBER() over(order by a.year_month) as id,a.year_month 
	from (
		select distinct year_month 
		from dbo.psi_item_detail_temp_info 
		where file_id = @file_id) a) b 
left join 
(select ROW_NUMBER() over(order by a.year_month) as id,a.year_month 
	from (
		select distinct year_month 
		from dbo.psi_item_detail_temp_info 
		where file_id = @file_id) a) c on b.id+1=c.id
		where c.year_month is not null
		
BEGIN


OPEN date_cursor    --打开游标
         FETCH date_cursor INTO @month_c --将游标里的,month_c赋给变量
---------------该部分是缺失的部分----------
		select * from (  select sum(qty) t1_qty,item_cd,file_id  from data_test   where   year_month = left(@month_c,7) and file_id = @file_id  group by item_cd,file_id
) t1 
left join (  select sum(qty) t2_qty,item_cd,file_id  from data_test   where   year_month = right(@month_c,7) and file_id = @file_id  group by item_cd,file_id
) t2 on t1.item_cd = t2.item_cd and t1.file_id = t2.file_id
------------------------
		IF (@@ERROR <> 0)
	 WHILE (@@fetch_status=0)
	 --关闭游标 
	CLOSE date_cursor 
	--释放资源 
	DEALLOCATE date_cursor 
END

----------具体的存储过程逻辑忽略,存储过程中保留一个游标语法--------------

----------逻辑中涉及一个和上月对比的逻辑,保留了对时间的处理--------------

----------每次调用改存储过程只会查出对应的fileId的各个月的数据------------

---------sql简化时有有错,现在已经加上了缺失的部分有建议欢迎提出-------


java | 阅读 3039 次
文章评论,共2条
黄边简进(游客)
2019-06-03 12:28
1
写的好模糊
mooncharmzx(作者)
2019-06-03 15:40
2
以下是引用黄边简进在2019-06-03 12:28的发言1

写的好模糊
简化sql时游标中的部分不没有加上,现在修改了
游客请输入验证码