iBatis.NET用Oracle存储过程插入记录,返回记录ID
我正在尝试使用函数在 Oracle 表中插入一条记录,该函数将通过 iBatis.NET 调用。直接调用时,函数在 Oracle 中按预期工作。
我尝试过使用
和
SqlMap 但我无法让 iBatis.NET 调用该函数,并且 Oracle 不支持返回任何内容来自存储过程。
我需要将对象的属性作为参数传递给函数/存储过程,并获取此新记录的 ID。
iBatis.NET 调用/SQLMap/Sproc 或 Oracle 中的函数签名的最佳组合是什么?
该文档仅包含内联 SQL 的示例,我只能使用存储过程。
由于真实对象的属性数量较多,哈希映射和参数数量在 30 多个。
理想情况下我能够做到这一点(不起作用):
<过程 id="InsertPerson"parameterClass="BOM.Person"> TestDB.PERSON_PKG.InsertPerson(#姓名#, #年龄#)
域对象:
public class Person
{
int ID { get; set; }
string Name { get; set; }
decimal Age { get; set; }
}
iBatis.NET 调用:
int personID = mapper.Insert("InsertPerson", person);
Oracle 存储过程:
FUNCTION InsertPerson(
p_Name IN Persons.Name%TYPE,
p_Age IN Persons.Age%TYPE,
) RETURN NUMBER
IS
NEW_ID Persons.ID%TYPE;
BEGIN
SELECT Persons_SEQ.NEXTVAL INTO NEW_ID FROM DUAL; /* Get new ID*/
INSERT INTO Persons(ID, Name, Age)
SELECT NEW_ID, p_Name, p_Age from dual; /* Insert record */
COMMIT;
RETURN NEW_ID;
END;
I am attempting to insert a record in an Oracle table with a Function, which would be called through iBatis.NET. Function works as expected in Oracle when called directly.
I have tried using <statement>
and <insert>
SqlMap but I can't get iBatis.NET to call the function, and Oracle doesn't support returning anything from Stored Procedure.
I would need to pass properties of my object as parameters to a function/sproc and get back the ID of this new record.
What would be a good combination of iBatis.NET call / SQLMap / Sproc or Function signature in Oracle?
The documentation only has examples of in-line SQL and I can only use sprocs.
Due to the number of properties in real objects, the hash-map and number of parameters is in the 30+.
Ideally I would be able to do this (doesn't work):
<procedure id="InsertPerson" parameterClass="BOM.Person">
TestDB.PERSON_PKG.InsertPerson(#Name#, #Age#)
</procedure>
Domain object:
public class Person
{
int ID { get; set; }
string Name { get; set; }
decimal Age { get; set; }
}
iBatis.NET call:
int personID = mapper.Insert("InsertPerson", person);
Oracle Stored Procedure:
FUNCTION InsertPerson(
p_Name IN Persons.Name%TYPE,
p_Age IN Persons.Age%TYPE,
) RETURN NUMBER
IS
NEW_ID Persons.ID%TYPE;
BEGIN
SELECT Persons_SEQ.NEXTVAL INTO NEW_ID FROM DUAL; /* Get new ID*/
INSERT INTO Persons(ID, Name, Age)
SELECT NEW_ID, p_Name, p_Age from dual; /* Insert record */
COMMIT;
RETURN NEW_ID;
END;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这对其他人有帮助,我无法找到解决我的问题的方法。
我最终将其实现为一个存储过程,该存储过程采用要插入到表中的所有字段的输入参数,以及一个返回由序列生成的唯一 ID 的输出参数。
执行
mapper.Insert(...)
后,我只需读取输出参数并返回它。C#:
MyBATIS 映射:
Sproc(Oracle):
In case this helps someone else, I was unable to find a workaround to my problem.
I ended up implementing this as a Stored Procedure which takes input parameters for all fields to be inserted in to a table, and one output parameter which returns the unique ID generated by sequence.
After executing
mapper.Insert(...)
I simply read the output parameter and return it.C#:
MyBATIS map:
Sproc (Oracle):