java mybatis 执行 sqlserver存储过程 xml 以及实现类

个人觉得用存储过程来写数据库逻辑很方便,因为项目从数据库到后端到前端都要去编写。所以把所有的都分离。

首先ssm的结构配置这里就不说了,可以看我博客ssm的2种配置。

1、Mapper.java

import java.util.List;
import java.util.Map;

public interface Mapper {
	public List> operaProcedure(Map map);
}

2、mapperSetting.xml


	
		
	

3、test.java

这里只贴了实现存储过程的主要代码

                SqlSessionFactory sf = (SqlSessionFactory) context.getBean("sqlSessionFactory");
		SqlSession sqlsession = sf.openSession();
		StringBuffer sb = new StringBuffer();
		Mapper mapper = sqlsession.getMapper(Mapper.class);
		Map map = new HashMap();
		sb.append("
"); sb.append("sp_videocall"); sb.append("delcall"); sb.append("" + userordrowid + ""); sb.append("" + roomid + ""); sb.append("
"); map.put("action", sb.toString()); List> list = mapper.operaProcedure(map); Map resultMap = new HashMap(); sqlsession.commit(); sqlsession.close();

4、test.sql

CREATE TABLE [dbo].[t_videocall](
	[flag] [int] IDENTITY(1,1) NOT NULL,
	[ordeowid] [varchar](200) NULL,
	[roomid] [varchar](200) NULL,
	[keyid] [varchar](200) NULL,
	[inline] [varchar](50) NULL,
	[waittime] [datetime] NULL,
	[userordrowid] [varchar](200) NULL
) ON [PRIMARY]
GO
CREATE proc [dbo].[sp_videocall] @str  varchar(max)
as
begin
	DECLARE @ItemMessage XML	
	SET @ItemMessage=@str
	declare @flag varchar(100)
	if @ItemMessage.value('(/main/OPTYPE/text())[1]','varchar(max)') ='delcall'
		begin
			set @flag = (select top 1 flag from dbo.t_videocall where roomid = @ItemMessage.value('(/main/ROOMID/text())[1]','varchar(max)') and userordrowid = @ItemMessage.value('(/main/USERORDROWID/text())[1]','varchar(max)') order by flag asc)
			delete dbo.t_videocall where flag = @flag
			if exists(select * from dbo.t_videocall where flag = @flag)
				select '以删除' as result,'0' as error
			else 
				select '出错,请联系管理员' as result,'1' as error
		end
end

以上

你可能感兴趣的:(java)