我想知道是否可以将两个或更多参数传递给mybatis SqlSession插入方法,而无需为此创建新类.我知道resultType ="hashmap"可以用来从select中返回数据,但是如何传递数据呢?还想知道为什么不为多个参数使用varargs插入方法.
在此先感谢Remis B.
有几种不同的方法.
映射器
如果你使用我推荐的Mapper类,你可以做这样的事情.
interface Mapper
{
void insertSomeObject(@Param("a") Integer a, @Param("b") Integer b);
}
Run Code Online (Sandbox Code Playgroud)
然后在xml映射器中,您可以使用#{a}和#{b}引用您的参数.
HashMap中
您可以将参数包装在HashMap中.
HashMap map = new HashMap();
map.put("a", 1);
map.put("b", 2);
Run Code Online (Sandbox Code Playgroud)
然后将哈希映射传递给SQL会话插入,并使用键值引用参数.
session.insert("myInsertStatment", map);
Run Code Online (Sandbox Code Playgroud)
任何收藏
我不确定您的确切需求,但如果您有任意数量的整数传递给insert语句,只需传递一个List或任何Collection.
interface Mapper
{
void insertSomeObject(@Param("integers") Collection<Integer> integers);
}
Run Code Online (Sandbox Code Playgroud)
然后在xml中,您可以使用for each生成动态xml以满足您的需求.
<foreach collection="integers" item="integer" open="(" close=")" separator=",">
#{integer}
</foreach>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5041 次 |
| 最近记录: |