简体   繁体   English

使用列表参数调用存储过程

[英]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... 可执行演示已通过...测试

  • Microsoft SQL Server 2017 (RTM-CU15) (KB4498951) - 14.0.3162.1 (X64) Microsoft SQL Server 2017(RTM-CU15)(KB4498951)-14.0.3162.1(X64)
  • mssql-jdbc 7.3.1.jre8-preview mssql-jdbc 7.3.1.jre8-preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以数组为参数调用存储过程 - Calling a stored procedure with an array as parameter 使用hibernate / jdbc以值/对象列表作为参数从Java代码调用存储过程 - Calling stored procedure from java code using hibernate/jdbc with list of values/objects as parameter 通过CallableStatement调用存储过程时出现“参数不是OUT参数”错误 - “Parameter is not an OUT parameter” error while calling stored procedure via CallableStatement 从java中的输入参数使用数组调用sybase中的存储过程 - calling a stored procedure in sybase with an array from input parameter in java 使用jpa 2.0调用具有out参数的存储过程 - calling a stored procedure having out parameter using jpa 2.0 从存储过程中获取 Output 参数而不调用 execute() - Get Output parameter from stored procedure without calling execute() 在SQL Server 2008中使用表类型输入参数调用存储过程 - Calling stored procedure with table type input parameter in sql server 2008 从Java调用存储类型为Record的参数的存储过程 - Calling a stored procedure from java that accepts parameter of type Record 从Java(Eclipse)调用MySQL存储过程中的OUT参数错误 - OUT Parameter Error in Calling MySQL Stored Procedure from Java (Eclipse) 使用Spring +没有为参数指定值来调用Postgres存储过程 - Calling a Postgres stored Procedure using Spring + No value specified for parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM