iBatis.NET用Oracle存储过程插入记录,返回记录ID

发布于 2024-09-30 15:06:21 字数 1302 浏览 3 评论 0原文

我正在尝试使用函数在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-10-07 15:06:21

如果这对其他人有帮助,我无法找到解决我的问题的方法。

我最终将其实现为一个存储过程,该存储过程采用要插入到表中的所有字段的输入参数,以及一个返回由序列生成的唯一 ID 的输出参数。

执行mapper.Insert(...)后,我只需读取输出参数并返回它。

C#:

mapper.BeginTransaction(System.Data.IsolationLevel.Serializable);

// Add new Record
Hashtable param = new Hashtable();
param.Add("ID", user.ID); // Output
param.Add("DeptID", user.DeptID);
param.Add("RightID", user.RightID);

mapper.Insert("AddUserRight", param);

user.ID = Convert.ToInt32(param["ID"]);

MyBATIS 映射:

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="CCP" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <statements>
        <procedure id="AddUserRight" parameterMap="AddUserRight-param">
            Database.USER_PKG.ADDUSERRIGHT
        </procedure>
    </statements>

    <parameterMaps>
        <parameterMap id="AddUserRight-param">
            <parameter property="ID" column="ID" direction="Output" />
            <parameter property="DeptID" column="DeptID" direction="Input" />
            <parameter property="RightID" column="RightID" direction="Input" />
        </parameterMap>
    </parameterMaps>
</sqlMap>

Sproc(Oracle):

   PROCEDURE AddUserRight(
            ID OUT USERRIGHTS.USERID%TYPE,
            DEPTID IN USERRIGHTS.DEPTID%TYPE,
            RIGHTID IN USERRIGHTS.RIGHTID%TYPE)
   IS
   BEGIN
        SELECT USERRIGHTS_UNQ_SEQ.NEXTVAL INTO ID FROM DUAL;

             INSERT INTO USERRIGHTS(ID, DEPTID, RIGHTID)
             VALUES (ID, DEPTID, RIGHTID);
   END;

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#:

mapper.BeginTransaction(System.Data.IsolationLevel.Serializable);

// Add new Record
Hashtable param = new Hashtable();
param.Add("ID", user.ID); // Output
param.Add("DeptID", user.DeptID);
param.Add("RightID", user.RightID);

mapper.Insert("AddUserRight", param);

user.ID = Convert.ToInt32(param["ID"]);

MyBATIS map:

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="CCP" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <statements>
        <procedure id="AddUserRight" parameterMap="AddUserRight-param">
            Database.USER_PKG.ADDUSERRIGHT
        </procedure>
    </statements>

    <parameterMaps>
        <parameterMap id="AddUserRight-param">
            <parameter property="ID" column="ID" direction="Output" />
            <parameter property="DeptID" column="DeptID" direction="Input" />
            <parameter property="RightID" column="RightID" direction="Input" />
        </parameterMap>
    </parameterMaps>
</sqlMap>

Sproc (Oracle):

   PROCEDURE AddUserRight(
            ID OUT USERRIGHTS.USERID%TYPE,
            DEPTID IN USERRIGHTS.DEPTID%TYPE,
            RIGHTID IN USERRIGHTS.RIGHTID%TYPE)
   IS
   BEGIN
        SELECT USERRIGHTS_UNQ_SEQ.NEXTVAL INTO ID FROM DUAL;

             INSERT INTO USERRIGHTS(ID, DEPTID, RIGHTID)
             VALUES (ID, DEPTID, RIGHTID);
   END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文