- 首先建立相關(guān)的存儲(chǔ)過程祭阀,如圖:
2.在service實(shí)現(xiàn)層填充參數(shù) 例如:
@Override
public Dto addFloorList(BannerRequest req)
{
Dto dto = new Dto();
List<IndexFloor> list = new ArrayList<IndexFloor>();
for (int i = 0; i < 3; i++)
{
IndexFloor flr = new IndexFloor();
flr.setFloorId(i + 3);
flr.setFloorTitle("測試數(shù)據(jù) " + i + 1);
flr.setFlrSort(i + 1);
flr.setFlrCat(2);
list.add(flr);
}
Map<String, Object> param = new HashMap<String, Object>();
param.put("id", 2);
param.put("floor_list", list);
//調(diào)用存儲(chǔ)過程
webMapper.spInsertInfo(param);
int rtnCode = (Integer) param.get("rtnCode");
String rtnMsg = String.valueOf(param.get("rtnMsg"));
System.out.print(rtnCode + "------" + rtnMsg);
3.編寫工具類,用來處理Array參數(shù):
核心代碼如下(詳見附件中的 handler/ArrayHandler.java):
(注意:上圖中的IndexFloor為測試bean。具體bean對(duì)象,根據(jù)需要編寫)
4.在MyBatis配置文件中,配置<typeHandlers>骑篙,handler為我們寫好的工具類:
配置代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 自定義傳入?yún)?shù)類型(TypeHandler) -->
<typeHandlers>
<!-- 如果沒有在Handler中加注解,用第一個(gè) -->
<!--<typeHandler javaType="list" jdbcType="ARRAY" handler="xxx.handler.ArrayHandler"/>-->
<typeHandler javaType="list" handler="xxx.handler.ArrayHandler" />
</typeHandlers>
</configuration>
5.在Mapper.xml文件中調(diào)用存儲(chǔ)過程并傳參數(shù):
<select id="spInsertInfo" parameterType="Map" resultType="Map" statementType="CALLABLE">
call TMP_LUJIE_TEST_ARRAY
(
#{rtnCode,mode=OUT,jdbcType=INTEGER,javaType=Integer},
#{rtnMsg,mode=OUT,jdbcType=VARCHAR,javaType=String},
#{id,mode=IN,jdbcType=INTEGER,javaType=Integer},
** #{floor_list,jdbcType=ARRAY,javaType=list}**
)
</select>
6.調(diào)試代碼森书。
參考資料:
1.how-to-pass-java-list-of-objects-to-oracle-stored-procedure-using-mybatis:
2. myBatis自定義傳入?yún)?shù)類型(TypeHandler)