How should I call the following stored procedure(sqlserver 2008 r2 with User-Defined Table Type) in springboot, mybatis with annotation

180 views
Skip to first unread message

Ricky Paranoid

unread,
Dec 20, 2019, 10:56:28 PM12/20/19
to mybatis-user
How should I call the following stored procedure(sqlserver 2008 r2) in springboot, mybatis with annotation

    declare @p1 TbType_Order
    insert
into @p1 values('00005',20.01)
    insert
into @p1 values('00006',15.99)
   
exec Gp_UpdateOrderBillDT @tb=@p1,@intFlag=0,@billNumberId=16,@billType=301



the "TbType_Order" is a User-Defined Table Type

   
    CREATE TYPE [dbo].[TbType_Order] AS TABLE(
       
[No] [varchar](50) NULL,
       
[Price] [numeric](24, 10) NULL
   
)
    GO



I try to call using this way:

 
   public interface BillProcedureMapper {
       
@Select({
           
"<script>" +
           
" declare @p1 TbType_Order " +
           
" <foreach item=\"item\" collection=\"details\"> ",
           
" insert into @p1 values( #{item.no},#{item.price} )" +
           
" </foreach> ",
           
" exec Gp_UpdateOrderBillDT " +
               
"@tb=@p1," +
               
"@intFlag=0," +
               
"@billNumberId=#{billNumberId}," +
               
"@billType=301; " +
           
"</script>"
       
})
       
@Options(statementType = StatementType.CALLABLE)
       
void savePurchaseOrderDetail(
           
@Param("billNumberId") Long billNumberId,
           
@Param("details") List<DetailDTO> details
       
);
   
}



but jdbc say "variable @p1 already declared"
and I try use type handler :

    public class TableTypeOrderHandler extends BaseTypeHandler {
       
@Override
       
public void setNonNullParameter(
           
PreparedStatement ps, int i, Object parameter, JdbcType jdbcType
       
) throws SQLException {

           
SQLServerDataTable sourceDataTable = new SQLServerDataTable();

            sourceDataTable
.addColumnMetadata("No", Types.VARCHAR);

            sourceDataTable
.addColumnMetadata("Price", Types.DECIMAL);

           
List<DetailDTO> dataList = (List<DetailDTO>) parameter;

           
for (DetailDTO o : dataList) {

                sourceDataTable
.addRow(
                    o
.getNo(),
                    o
.getPrice()
               
);

           
}

            ps
.setObject(i, sourceDataTable);
       
}

       
@Override
       
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
           
return null;
       
}

       
@Override
       
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
           
return null;
       
}

       
@Override
       
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
           
return null;
       
}
   
}


then change the select method:

   
    @Select({
       
"exec Gp_UpdateOrderBillDT " +
               
"@tb=#{details, jdbcType=VARCHAR, typeHandler=com.xxx.TableTypeOrderHandler}," +
               
"@intFlag=0," +
               
"@billNumberId=#{billNumberId}," +
               
"@billType=301; "
   
})
   
@Options(statementType = StatementType.CALLABLE)
   
void savePurchaseOrderDetail(
       
@Param("billNumberId") Integer billNumberId,
       
@Param("details") List<DetailDTO> details
   
);



but jdbc exception is: Operand type conflict: table type is not compatible with int

What should I do to get it right?

ri yo

unread,
Dec 21, 2019, 2:23:34 AM12/21/19
to mybati...@googlegroups.com
test:
debug:error
run:ok
====
config file :
mybatis.type-handlers-package=com.xxxx.xxxxtypehandler

Ricky Paranoid <rick...@gmail.com> 于 2019年12月21日周六 上午11:56写道:
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/59b00afa-98ac-42e2-9fae-1205f419944c%40googlegroups.com.

Iwao AVE!

unread,
Dec 23, 2019, 9:40:33 AM12/23/19
to mybatis-user
Just for future reference, I left a comment on the same question on StackOverflow:
And the following answer seemed to help (another use case of 'unwrap' method).

Ricky Paranoid

unread,
Dec 29, 2019, 9:52:54 AM12/29/19
to mybatis-user
Sorry for replying so late
I also asked stackoverflow questions
thanks for your help,it works, great!!!
To unsubscribe from this group and stop receiving emails from it, send an email to mybati...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages