Question

I'd like to call a MySQL stored procedure from Java using MyBatis & Spring. Do I need to use a POJO to do it?

I'm using the following versions:

  • Java 1.6
  • MyBatis 3.2.2
  • Spring / MyBatis
  • 1.2 Spring 3.2.3
  • MySQL 5.1

The following code snippets code does work.

Mapper XML:

<update id="calculateNonTaxableOrderAmount"
        parameterType="CalculateNonTaxableAmountDTO"  
        statementType="CALLABLE" >

    { call sp_calc_non_taxable_order_amount( 
              #{orderNum,jdbcType=INTEGER,mode=IN}, 
              #{nonTaxableAmount,jdbcType=DECIMAL,mode=OUT} )
        }       
</update>

The method on the DAO Interface:

public void calculateNonTaxableOrderAmount(CalculateNonTaxableAmountDTO dto);

CalculateNonTaxableAmountDTO:

public class CalculateNonTaxableAmountDTO {

private Long orderNum;
private BigDecimal nonTaxableAmount;

public Long getOrderNum() {
    return orderNum;
}
public void setOrderNum(Long orderNum) {
    this.orderNum = orderNum;
}
public BigDecimal getNonTaxableAmount() {
    return nonTaxableAmount;
}
public void setNonTaxableAmount(BigDecimal nonTaxableAmount) {
    this.nonTaxableAmount = nonTaxableAmount;
}
}

The above works great, but what I would like to do is something like this:

Mapper XML: Note this missing parameterType attribute.

<update id="calculateNonTaxableOrderAmount"             
        statementType="CALLABLE" >

    { call sp_calc_non_taxable_order_amount( 
              #{orderNum,jdbcType=INTEGER,mode=IN}, 
              #{nonTaxableAmount,jdbcType=DECIMAL,mode=OUT} )
        }       
</update>

The method on the DAO Interface:

public void calculateNonTaxableOrderAmount(
            @Param("orderNum") Long orderNum, 
            @Param("nonTaxableAmount") BigDecimal nonTaxableAmount);

The DAO method is called with some code similar to:

BigDecimal nonTaxAmount = new BigDecimal(-1).setScale(2);
orderHeaderDAO.calculateNonTaxableOrderAmount(new Long(11111), nonTaxAmount);
System.out.println("nonTaxAmount = " + nonTaxAmount);

The code executes successfully, but the nonTaxAmount is never updated. The println prints out -1.

Any help, or guidance would be appreciated.

Was it helpful?

Solution

I believe it's not possible since MyBatis sets nonTaxableAmount value to new instance of BigDecimal, and you have reference to original BigDecimal instance. To handle this issue I use some sort of data holder (in a way resembling jax-ws data holders):

public class Holder<T> {
    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data= data;
    }
}

MyBatis config(asume that nonTaxableAmount is instance of Holder):

#{nonTaxableAmount.data,jdbcType=DECIMAL,mode=OUT}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top