Java 来自mybatis的SQL过程调用
总之,我试图从Java用户Mybatis调用一个SQL过程,但是我得到了 “java.sql.SQLException:索引::1处缺少IN或OUT参数”。在谷歌上搜索这个并不能给我正确的答案。 下面是我写的代码。我是第一次使用mybatis,有人能给我指点一下吗 Mepper.xml:---Java 来自mybatis的SQL过程调用,java,oracle,hibernate,mybatis,ibatis,Java,Oracle,Hibernate,Mybatis,Ibatis,总之,我试图从Java用户Mybatis调用一个SQL过程,但是我得到了 “java.sql.SQLException:索引::1处缺少IN或OUT参数”。在谷歌上搜索这个并不能给我正确的答案。 下面是我写的代码。我是第一次使用mybatis,有人能给我指点一下吗 Mepper.xml:--- { 调用P_HW_SW_CRUD.SAVE_HARDWARE_INVENTORY( #{pDataIn,javaType=Object,jdbcType=ARRAY,jdbcTypeName=TAB_I
{
调用P_HW_SW_CRUD.SAVE_HARDWARE_INVENTORY(
#{pDataIn,javaType=Object,jdbcType=ARRAY,jdbcTypeName=TAB_IDESK_HW_INFO,mode=IN,typeHandler=com.tristar.IDESK.mybatis.mapper.HardwareInventoryHandler}
#{pMsgCodeOut,javaType=Long,jdbcType=DECIMAL,mode=OUT}
)
}
这里是我设置地图的地方--
public void addUpdateInventory(JsonNode-JsonNode){
Map inventoryMap=getMapFromJSONAddUpdate(jsonNode);
HashMap returnMap=新的HashMap();
Long pErrodCodeOut=null;
字符串pErrorMsg=null;
returnMap.put(“pDataIn”,(List)inventoryMap.get(“beanHardwareList”);
returnMap.put(“perrocodeout”,pErrodCodeOut);
//returnMap.put(“pErrodMsgOut”,pErrorMsg);
硬件InventoryMapper.addHardwareList(返回地图);
}
我还实现了一个自定义类型处理程序,将我的JavaBean属性映射到oracleType对象
public class HardwareInventoryHandler implements TypeHandler{
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
try{
//new RecLoaderService().getJSONObj("REC_ICAST_CLAIMANT_INFO", "TAB_ICAST_CLAIMANT_INFO", jsonObject, i, ps);
System.out.println(parameter);
List<HardwareInventoryBean> hwInventoryList = (List<HardwareInventoryBean>) parameter;
System.out.println("Parameter Here --"+parameter);
StructDescriptor structDescriptor = StructDescriptor.createDescriptor(InventoryConstants.HW_DB_REC_TYPE_NAME, ps.getConnection());
STRUCT[] structs = null;
structs = new STRUCT[hwInventoryList.size()];
for (int index = 0; index < hwInventoryList.size(); index++) {
HardwareInventoryBean hwInventoryBean = hwInventoryList.get(index);
Object[] params = new Object[12];
params[0] = hwInventoryBean.getHwId();
params[1] = hwInventoryBean.getHwTypeId();
params[2] = hwInventoryBean.getHwModelId();
params[3] = hwInventoryBean.getAssetId();
params[4] = hwInventoryBean.getDateReceived();
params[5] = hwInventoryBean.getHwPrice();
params[6] = hwInventoryBean.getHwStatus();
params[7] = hwInventoryBean.getHwWarranty();
params[8] = hwInventoryBean.getCreatedBy();
params[9] = hwInventoryBean.getCreatedDate();
params[10] = hwInventoryBean.getModifiedBy();
params[11] = hwInventoryBean.getModifiedDate();
System.out.println("---------> " +hwInventoryBean.getHwId());
STRUCT struct = new STRUCT(structDescriptor, ps.getConnection(), params);
structs[index] = struct;
}
ArrayDescriptor desc = ArrayDescriptor.createDescriptor(InventoryConstants.HW_DB_TEB_TYPE_NAME, ps.getConnection());
ARRAY oracleArray = new ARRAY(desc, ps.getConnection(), structs);
ps.setArray(i, oracleArray);
}
catch(Exception e){
e.printStackTrace();
}
}
public JSONObject getResult(ResultSet rs, String columnName)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public JSONObject getResult(ResultSet rs, int columnIndex)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public JSONObject getResult(CallableStatement cs, int columnIndex)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
公共类HardwareInventoryHandler实现TypeHandler{
public void setParameter(PreparedStatement ps,int i,Object参数,JdbcType JdbcType)抛出SQLException{
试一试{
//new RecLoaderService().getJSONObj(“REC_ICAST_索赔人信息”,“TAB_ICAST_索赔人信息”,jsonObject,i,ps);
System.out.println(参数);
List HWINTORYLIST=(List)参数;
System.out.println(“此处参数--”+参数);
StructDescriptor StructDescriptor=StructDescriptor.createDescriptor(InventoryConstants.HW_DB_REC_TYPE_NAME,ps.getConnection());
STRUCT[]structs=null;
structs=新结构[hwInventoryList.size()];
对于(int index=0;index”+hwinentorybean.getHwId());
STRUCT STRUCT=new STRUCT(structDescriptor,ps.getConnection(),params);
structs[index]=struct;
}
ArrayDescriptor desc=ArrayDescriptor.createDescriptor(inventoryStants.HW_DB_TEB_TYPE_NAME,ps.getConnection());
ARRAY oracleArray=新数组(desc,ps.getConnection(),structs);
ps.setArray(i,oracleArray);
}
捕获(例外e){
e、 printStackTrace();
}
}
公共JSONObject getResult(结果集rs,字符串columnName)
抛出SQLException{
//TODO自动生成的方法存根
返回null;
}
公共JSONObject getResult(结果集rs,int columnIndex)
抛出SQLException{
//TODO自动生成的方法存根
返回null;
}
公共JSONObject getResult(CallableStatement cs,int columnIndex)
抛出SQLException{
//TODO自动生成的方法存根
返回null;
}
}
映射器接口--
public void addHardwareList(HashMap returnMap);
我的SQL过程代码是:-
create or replace PACKAGE BODY P_HW_SW_CRUD AS -- body
PROCEDURE save_hardware_inventory (hw IN TAB_IDESK_HW_INFO, nu OUT NUMBER) IS
ERROR_CODE NUMBER(10);
ERROR_DESC VARCHAR2(200);
BEGIN
nu := 10;
FOR i IN 1 .. hw.COUNT
loop
if(hw(i).HW_ID <> 0) then
INSERT INTO IDESK_HW_INVENTORY VALUES (IDESK_HW_INVENTORY_seq.nextval, hw(i).hwTypeId , hw(i).hwModelId,hw(i).assetId
, hw(i).dateReceived , hw(i).hwPrice ,hw(i).hwStatus , hw(i).hwWarrenty , hw(i).createdBy , sysdate,hw(i).modifiedBy, sysdate);
else
update IDESK_HW_INVENTORY set HW_TYPE_ID=hw(i).hwTypeId,HW_MODEL_ID=hw(i).hwModelId,ASSET_ID=hw(i).assetId,DATE_RECEIVED=hw(i).dateReceived,HW_PRICE=hw(i).hwPrice,HW_STATUS=hw(i).hwStatus,HW_WARRENTY=hw(i).hwWarrenty,MODIFIED_BY=hw(i).modifiedBy,MODIFIED_DATE=sysdate where HW_ID=hw(i).HW_ID;
end If;
end loop;
Exception
when others then
ROLLBACK;
ERROR_CODE := SQLCODE;
ERROR_DESC := SQLERRM;
END save_hardware_inventory ;
PROCEDURE save_software_inventory (sw IN TAB_IDESK_SW_INFO ) IS
ERROR_CODE NUMBER(10);
ERROR_DESC VARCHAR2(200);
BEGIN
FOR i IN 1 .. sw.COUNT
loop
if(sw(i).SW_ID <> 0) then
INSERT INTO IDESK_SW_INVENTORY VALUES (IDESK_SW_INVENTORY_seq.nextval, sw(i).SW_VENDOR_ID , sw(i).SW_VERSION_TITLE,sw(i).QUANTITY
, sw(i).PRICE , sw(i).CONTRACT_ACOUNT_NUM ,sw(i).SW_STATUS , sw(i).CREATED_BY ,sysdate, sw(i).MODIFIED_BY , sysdate);
else
update IDESK_SW_INVENTORY set SW_VENDOR_ID=sw(i).SW_VENDOR_ID,SW_VERSION_TITLE=sw(i).SW_VERSION_TITLE,QUANTITY=sw(i).QUANTITY,PRICE=sw(i).PRICE,CONTRACT_ACOUNT_NUM=sw(i).CONTRACT_ACOUNT_NUM,SW_STATUS=sw(i).SW_STATUS,MODIFIED_BY=sw(i).MODIFIED_BY,MODIFIED_DATE=sysdate where SW_ID=sw(i).SW_ID;
end If;
end loop;
Exception
when others then
ROLLBACK;
ERROR_CODE := SQLCODE;
ERROR_DESC := SQLERRM;
END save_software_inventory ;
END P_HW_SW_CRUD ;
创建或替换包体P_HW_SW_CRUD AS--BODY
程序保存硬件资源清册(硬件在选项卡中,硬件信息,nu OUT编号)为
错误代码(10);
错误描述VARCHAR2(200);
开始
nu:=10;
因为我在1。。硬件计数
环
如果(hw(i).hw_ID 0)那么
在IDESK_HW_库存值中插入(IDESK_HW_库存序列下一步,HW(i).hwTypeId,HW(i).hwModelId,HW(i).assetId
,hw(i).dateReceived,hw(i).hwPrice,hw(i).hwStatus,hw(i).hw(i).hw(i).createdBy,sysdate,hw(i).modifiedBy,sysdate);
其他的
更新IDESK_HW_库存集HW_TYPE_ID=HW(i).hwTypeId,HW_MODEL_ID=HW(i).hwModelId,assetId=HW(i).assetId,DATE_RECEIVED=HW(i).dateReceived,HW_PRICE=HW(i).HW价格,HW_状态=HW(i).HW状态,HW_WARRENTY=HW(i).HW WARRENTY,MODIFIED_BY=HW(i).modifiedBy,MODIFIED_日期=sysdate,其中HW_ID=HW(i);
如果结束;
端环;
例外情况
当其他人
回降;
错误代码:=SQLCODE;
错误描述:=SQLERRM;
结束保存硬件库存;
程序保存软件清单(软件在标识栏中)
错误代码(10);
错误描述VARCHAR2(200);
开始
因为我在1。。软件计数
环
如果(sw(i).sw_ID 0)那么
在IDESK_SW_库存值中插入(IDESK_SW_库存序列,SW(i).SW_供应商ID,SW(i).SW_版本标题,SW(i).数量
,软件(i).价格,软件(i).合同数量,软件(i).软件状态,软件(i).创建人,系统日期,软件(i).修改人,系统日期);
其他的
更新IDESK\u SW\u库存集SW\u VENDOR\u ID=SW(i).SW\u VENDOR\u ID,SW\u VERSION\u TITLE=SW(i).SW\u VERSION\u TITLE,QUANTITY=SW(i).数量,价格=SW(i).合同\u account\u NUM=SW(i).SW\u STATUS,MODIFIED\u BY=SW(i).MODIFIED\u BY,MODIFIED\u DATE=sysdate其中SW\u ID=SW(i).SW\u ID;
如果结束;
端环;
例外情况
whe
public class HardwareInventoryHandler implements TypeHandler{
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
try{
//new RecLoaderService().getJSONObj("REC_ICAST_CLAIMANT_INFO", "TAB_ICAST_CLAIMANT_INFO", jsonObject, i, ps);
System.out.println(parameter);
List<HardwareInventoryBean> hwInventoryList = (List<HardwareInventoryBean>) parameter;
System.out.println("Parameter Here --"+parameter);
StructDescriptor structDescriptor = StructDescriptor.createDescriptor(InventoryConstants.HW_DB_REC_TYPE_NAME, ps.getConnection());
STRUCT[] structs = null;
structs = new STRUCT[hwInventoryList.size()];
for (int index = 0; index < hwInventoryList.size(); index++) {
HardwareInventoryBean hwInventoryBean = hwInventoryList.get(index);
Object[] params = new Object[12];
params[0] = hwInventoryBean.getHwId();
params[1] = hwInventoryBean.getHwTypeId();
params[2] = hwInventoryBean.getHwModelId();
params[3] = hwInventoryBean.getAssetId();
params[4] = hwInventoryBean.getDateReceived();
params[5] = hwInventoryBean.getHwPrice();
params[6] = hwInventoryBean.getHwStatus();
params[7] = hwInventoryBean.getHwWarranty();
params[8] = hwInventoryBean.getCreatedBy();
params[9] = hwInventoryBean.getCreatedDate();
params[10] = hwInventoryBean.getModifiedBy();
params[11] = hwInventoryBean.getModifiedDate();
System.out.println("---------> " +hwInventoryBean.getHwId());
STRUCT struct = new STRUCT(structDescriptor, ps.getConnection(), params);
structs[index] = struct;
}
ArrayDescriptor desc = ArrayDescriptor.createDescriptor(InventoryConstants.HW_DB_TEB_TYPE_NAME, ps.getConnection());
ARRAY oracleArray = new ARRAY(desc, ps.getConnection(), structs);
ps.setArray(i, oracleArray);
}
catch(Exception e){
e.printStackTrace();
}
}
public JSONObject getResult(ResultSet rs, String columnName)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public JSONObject getResult(ResultSet rs, int columnIndex)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public JSONObject getResult(CallableStatement cs, int columnIndex)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
public void addHardwareList(HashMap<String, Object> returnMap);
create or replace PACKAGE BODY P_HW_SW_CRUD AS -- body
PROCEDURE save_hardware_inventory (hw IN TAB_IDESK_HW_INFO, nu OUT NUMBER) IS
ERROR_CODE NUMBER(10);
ERROR_DESC VARCHAR2(200);
BEGIN
nu := 10;
FOR i IN 1 .. hw.COUNT
loop
if(hw(i).HW_ID <> 0) then
INSERT INTO IDESK_HW_INVENTORY VALUES (IDESK_HW_INVENTORY_seq.nextval, hw(i).hwTypeId , hw(i).hwModelId,hw(i).assetId
, hw(i).dateReceived , hw(i).hwPrice ,hw(i).hwStatus , hw(i).hwWarrenty , hw(i).createdBy , sysdate,hw(i).modifiedBy, sysdate);
else
update IDESK_HW_INVENTORY set HW_TYPE_ID=hw(i).hwTypeId,HW_MODEL_ID=hw(i).hwModelId,ASSET_ID=hw(i).assetId,DATE_RECEIVED=hw(i).dateReceived,HW_PRICE=hw(i).hwPrice,HW_STATUS=hw(i).hwStatus,HW_WARRENTY=hw(i).hwWarrenty,MODIFIED_BY=hw(i).modifiedBy,MODIFIED_DATE=sysdate where HW_ID=hw(i).HW_ID;
end If;
end loop;
Exception
when others then
ROLLBACK;
ERROR_CODE := SQLCODE;
ERROR_DESC := SQLERRM;
END save_hardware_inventory ;
PROCEDURE save_software_inventory (sw IN TAB_IDESK_SW_INFO ) IS
ERROR_CODE NUMBER(10);
ERROR_DESC VARCHAR2(200);
BEGIN
FOR i IN 1 .. sw.COUNT
loop
if(sw(i).SW_ID <> 0) then
INSERT INTO IDESK_SW_INVENTORY VALUES (IDESK_SW_INVENTORY_seq.nextval, sw(i).SW_VENDOR_ID , sw(i).SW_VERSION_TITLE,sw(i).QUANTITY
, sw(i).PRICE , sw(i).CONTRACT_ACOUNT_NUM ,sw(i).SW_STATUS , sw(i).CREATED_BY ,sysdate, sw(i).MODIFIED_BY , sysdate);
else
update IDESK_SW_INVENTORY set SW_VENDOR_ID=sw(i).SW_VENDOR_ID,SW_VERSION_TITLE=sw(i).SW_VERSION_TITLE,QUANTITY=sw(i).QUANTITY,PRICE=sw(i).PRICE,CONTRACT_ACOUNT_NUM=sw(i).CONTRACT_ACOUNT_NUM,SW_STATUS=sw(i).SW_STATUS,MODIFIED_BY=sw(i).MODIFIED_BY,MODIFIED_DATE=sysdate where SW_ID=sw(i).SW_ID;
end If;
end loop;
Exception
when others then
ROLLBACK;
ERROR_CODE := SQLCODE;
ERROR_DESC := SQLERRM;
END save_software_inventory ;
END P_HW_SW_CRUD ;
((DelegatingConnection)ps.getConnection()).getInnermostDelegate().
select id="addHardwareList"
statementType="CALLABLE" parameterType="java.util.HashMap">
{
call P_HW_SW_CRUD.SAVE_HARDWARE_INVENTORY(
#{pDataIn,javaType=Object,
jdbcType=ARRAY,jdbcTypeName=TAB_IDESK_HW_INFO,
mode=IN,typeHandler=com.tristar.idesk.mybatis.mapper.HardwareInventoryHandler}
#{pMsgCodeOut,javaType=Long,jdbcType=DECIMAL,mode=OUT}
)
}