7

第一个问题: 我试图返回一个 OUT 参数,而不是带有注释的结果集。首先,这可能吗?如果是,人们将如何做到这一点?

MyBatis:3.0.6

数据库:SQL Server 2008

下面是我在 UserDAO 中的方法调用的语法示例:

@Select(value= "{ CALL saveUser( "
        + "#{userId, mode=IN, jdbcType=INTEGER},"
        + "#{firstname, mode=IN, jdbcType=VARCHAR},"
        + "#{lastname, mode=IN, jdbcType=VARCHAR},"
        + "#{message, mode=OUT, jdbcType=VARCHAR}"
        + ")}")
@Options(statementType=StatementType.CALLABLE)
public String saveUser(
        @Param("userId") int userId,
        @Param("firstname") String firstname,
        @Param("lastname") String lastname);

我正在从我的所有“保存”过程中返回一条消息,因此我可以向用户返回一条响应:“用户保存成功”、“保存用户错误”、“您无权保存此用户”等。我知道返回结果集会解决问题,只是我不想更改所有程序!

第二个问题:是否可以返回由多个 OUT 参数填充的“SaveProcedureResponse”?例如:

@Select(value= "{ CALL saveUser( "
        + "#{userId, mode=IN, jdbcType=INTEGER},"
        + "#{firstname, mode=IN, jdbcType=VARCHAR},"
        + "#{lastname, mode=IN, jdbcType=VARCHAR},"
        + "#{message, mode=OUT, jdbcType=VARCHAR},"
        + "#{status, mode=OUT, jdbcType=VARCHAR},"
        + "#{returnCode, mode=OUT, jdbcType=INTEGER}"
        + ")}")
@Options(statementType=StatementType.CALLABLE)
public SaveProcedureResponse saveUser(
        @Param("userId") int userId,
        @Param("firstname") String firstname,
        @Param("lastname") String lastname);

bean 看起来像这样:

public class SaveProcedureResponse {
    private String message;
    private String status;
    private int returnCode;

    public SaveProcedureResponse(String message, String status, int returnCode) {
        this.message = message;
        this.status = status;
        this.returnCode = returnCode;
    }
}

谢谢!

4

2 回答 2

9

第一个问题:我试图返回一个 OUT 参数,而不是带有注释的结果集。首先,这可能吗?如果是,人们将如何做到这一点?

错了,有点。Mapper 不会return输出参数,但是你可以让 Mybatis 将它们设置到参数对象上,或者将它们放入这样的映射

因此,给定一个简单的 java 对象,其中包含所有字段的 getter 和 setter。调用映射器后,输出参数将被设置到对象上。

<update id="someProcedure" statementType="CALLABLE">
    {call some procedure(
            #{someInParamA, mode=IN},
            #{someInParamB, jdbcType=ARRAY, mode=IN},
            #{someOutParamA, javaType=Boolean, jdbcType=NUMERIC, mode=OUT },
            #{someOutParamB, javaType=Object, jdbcType=ARRAY, jdbcTypeName=SOMEJDBCTYPE, mode=OUT})}
</update>

所以要获取输出参数,它看起来像这样。

mapper.getSomeProcedure(someBean);
//out params populated on the object passed to the mapper :)
String outA = bean.getSomeOutParamA();

这有点难以解释,这有意义吗?

于 2012-02-10T03:04:52.110 回答
7

使用MyBatis-Spring注释,像这样编写代码。

public interface ProductMapper
{
    @Select("exec Pup_ProductSearch_Sel #{searchString}, #{pageNum}, #{pageSize}, #{totalRows,mode=OUT,jdbcType=NUMERIC}")
    @Options(statementType = StatementType.CALLABLE)
    public List<Map<String, Object>> productSearch(ProductSearchParameters params);


    public class ProductSearchParameters
    {
        private String searchString = null;
        private Integer pageNum = 1;
        private Integer pageSize = 5;
        private Integer totalRows = -1;

        // Accessors go here...
    }
}

IN 参数由我们在调用代码中填充。OUT 参数由数据访问层填充,并在调用映射器后出现在 params 对象中。

@Service
public class TestService
{
    @Resource
    private ProductMapper mapper;


    public void run()
    {
        final ProductMapper.ProductSearchParameters params = new ProductMapper.ProductSearchParameters();
        params.setSearchString("book"); // IN parameter
        final List<Map<String, Object>> results = mapper.productSearch(params);

        for(final Map<String, Object> product : results)
        {
            System.out.println(product.get("title"));
        }

        System.out.println("Total results: " + params.getTotalRows()); // OUT parameter
    }
}

受这篇文章的启发:http: //ibatis.10938.n7.nabble.com/IBatis-3-0-beta-10-annotations-stored-procedures-td7806.html

于 2013-04-30T17:15:41.010 回答