[英]Calling Stored Procedure With List Parameter
I am unable to figure out how to send a list as a parameter to SQL Server Stored Procedure using myBatis 我无法弄清楚如何使用myBatis将列表作为参数发送到SQL Server存储过程
call sp(List<Object>)
I have a stored procedure inside a SQL Server(2012) which takes a parameter of type list. 我在SQL Server(2012)内部有一个存储过程,该存储过程采用类型为list的参数。
CREATE TypeTable of Table
(
@FKId IN
@FKId INT
@FKId INT
@FKId INT
@FKId INT
@userName VARCHAR
)
My Stored Procedure call 我的存储过程调用
ALTER PROCEDURE SP(@TypeTableList Typetable READONLY )
AS
BEGIN
/* My DB Operations To Enter New Records and Thier Child Records */
END
MyMapper 我的地图
<select id="mapperId" parameterType="map" statementType="CALLABLE">
call sp(#{list})
</select>
POJO POJO
public class ListClass {
private Long fk1;
private Long fk2;
private Long fk3;
private Long fk4;
private Long fk5;
private String userName;
public ListClass() {
super();
}
public Long getFk1() {
return fk1;
}
public void setFk1(Long fk1) {
this.fk1 = fk1;
}
public Long getFk2() {
return fk2;
}
public void setFk2(Long fk2) {
this.fk2 = fk2;
}
public Long getFk3() {
return fk3;
}
public void setFk3(Long fk3) {
this.fk3 = fk3;
}
public Long getFk4() {
return fk4;
}
public void setFk4(Long fk4) {
this.fk4 = fk4;
}
public Long getFk5() {
return fk5;
}
public void setFk5(Long fk5) {
this.fk5 = fk5;
}
public String getuserName() {
return userName;
}
public void setuserName(String userName) {
this.userName = userName;
}
}
I have tried using type handler of type array but i always get a exception. 我尝试使用类型数组的类型处理程序,但是我总是遇到异常。
I have not found any resources on creating a custom type handler for ArrayList With SQL Server 我没有找到关于使用SQL Server为ArrayList创建自定义类型处理程序的任何资源
Any Help would be appriciated 任何帮助将被申请
Thankyou 谢谢
The create
statement you posted didn't work in SQL Server 2017, so I'll show you a simpler example. 您发布的create
语句在SQL Server 2017中不起作用,因此,我将向您展示一个更简单的示例。
DDLs: DDL:
create table Users (
id int,
name varchar(20)
);
create type UserTableType as table (
id int,
name varchar(20)
);
create procedure uspAddUsers
@UserTable UserTableType READONLY
as
begin
insert into Users (id, name)
select * from @UserTable
end;
POJO: POJO:
public class User {
private Integer id;
private String name;
// getter/setter
}
Mapper method: 映射器方法:
@Options(statementType = StatementType.CALLABLE)
@Insert("{call uspAddUsers(#{users,typeHandler=pkg.UserListTypeHandler})}")
void insertUsers(@Param("users") List<User> users);
Note the typeHandler
option. 注意typeHandler
选项。
As David Browne pointed out, the driver requires SQLServerDataType
as the input, so you may need a type handler that converts the list into a SQLServerDataType
. 正如David Browne指出的那样,驱动程序需要SQLServerDataType
作为输入,因此您可能需要一个类型处理程序,将列表转换为SQLServerDataType
。
The below is a simple type handler implementation. 下面是一个简单的类型处理程序实现。
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.microsoft.sqlserver.jdbc.SQLServerDataTable;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
public class UserListTypeHandler extends BaseTypeHandler<List<User>>{
@Override
public void setNonNullParameter(PreparedStatement ps,
int i, List<User> parameter, JdbcType jdbcType)
throws SQLException {
SQLServerDataTable dataTable = new SQLServerDataTable();
dataTable.addColumnMetadata("id", java.sql.Types.INTEGER);
dataTable.addColumnMetadata("name", java.sql.Types.VARCHAR);
for (User user : parameter) {
dataTable.addRow(user.getId(), user.getName());
}
ps.unwrap(SQLServerPreparedStatement.class)
.setStructured(i, "UserTableType", dataTable);
}
// getNullableResult() won't be used
}
An executable demo tested with... 可执行演示已通过...测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.