簡體   English   中英

使用列表參數調用存儲過程

[英]Calling Stored Procedure With List Parameter

我無法弄清楚如何使用myBatis將列表作為參數發送到SQL Server存儲過程

     call sp(List<Object>)

我在SQL Server(2012)內部有一個存儲過程,該存儲過程采用類型為list的參數。

 CREATE TypeTable of Table 
 (
   @FKId IN
@FKId INT
@FKId INT
@FKId INT
@FKId INT
@userName VARCHAR
)

我的存儲過程調用

 ALTER PROCEDURE SP(@TypeTableList Typetable READONLY ) 

  AS
  BEGIN 
  /*  My DB Operations To Enter New Records and Thier Child Records */
  END

我的地圖

<select id="mapperId" parameterType="map" statementType="CALLABLE">
     call sp(#{list})
</select>

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;
}
 }

我嘗試使用類型數組的類型處理程序,但是我總是遇到異常。

我沒有找到關於使用SQL Server為ArrayList創建自定義類型處理程序的任何資源

任何幫助將被申請

謝謝

您發布的create語句在SQL Server 2017中不起作用,因此,我將向您展示一個更簡單的示例。

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:

public class User {
  private Integer id;
  private String name;
  // getter/setter
}

映射器方法:

@Options(statementType = StatementType.CALLABLE)
@Insert("{call uspAddUsers(#{users,typeHandler=pkg.UserListTypeHandler})}")
void insertUsers(@Param("users") List<User> users);

注意typeHandler選項。
正如David Browne指出的那樣,驅動程序需要SQLServerDataType作為輸入,因此您可能需要一個類型處理程序,將列表轉換為SQLServerDataType
下面是一個簡單的類型處理程序實現。

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
}

可執行演示已通過...測試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM