sno*_*ozy 5 java stored-procedures out-parameters mybatis
第一个问题: 我试图返回一个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);
Run Code Online (Sandbox Code Playgroud)
我从所有"保存"程序返回一条消息,因此我可以向用户返回响应:"用户保存成功","保存用户错误","您无权保存此用户"等.我知道返回结果集会解决问题,只是我不想改变我的所有程序!
第二个问题:是否可以返回从多个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);
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
第一个问题:我试图返回一个OUT参数而不是带注释的结果集.首先,它甚至可能吗?如果是的话,怎么会这样呢?
错了,有点儿.该映射不会return
out参数,但你可以得到的MyBatis将它们设置到参数对象,或者把它们放入一个地图像这样.
因此给出了一个简单的java对象,其中包含所有字段的getter和setter.调用映射器后,out参数将设置到对象上.
<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>
Run Code Online (Sandbox Code Playgroud)
因此,为了得到输出参数,它看起来像这样.
mapper.getSomeProcedure(someBean);
//out params populated on the object passed to the mapper :)
String outA = bean.getSomeOutParamA();
Run Code Online (Sandbox Code Playgroud)
这有点难以解释,这有意义吗?
使用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...
}
}
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
灵感来自这篇文章:http://ibatis.10938.n7.nabble.com/IBatis-3-0-beta-10-annotations-stored-procedures-td7806.html
归档时间: |
|
查看次数: |
39178 次 |
最近记录: |