zoukankan      html  css  js  c++  java
  • NHibernate 存储过程 第十四篇

      NHibernate也是能够操作存储过程的,不过第一次配置可能会碰到很多错误。

    一、删除

      首先,我们新建一个存储过程如下:

      CREATE PROC DeletePerson
      @Id int
      AS
      DELETE FROM Person WHERE PersonId = @Id;

      修改映射文件,添加删除对象的存储过程:

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class name="Model.PersonModel, Model" table="Person">
        <id name="PersonId" column="PersonId" type="Int32">
          <generator  class="native"/>
        </id>
        <property name="PersonName" column="PersonName" type="String"/>
        <!--多对一关系:Person属于一个Country name是Person实体类里的-->
        <many-to-one name="Country" column="CountryId" not-null="true" class="Model.CountryModel,Model" foreign-key="FK_Person_Country" />
        <!-- 一个Country里面有多个Person -->
        <set name="ListChild" table="Child" generic="true">
          <key column="ParentId" foreign-key="FK_Child_Person"/>
          <one-to-many class="Model.ChildModel,Model"/>
        </set>
        <sql-delete>DeletePerson ?</sql-delete>
      </class>
    </hibernate-mapping>
    复制代码

      执行存代码:

                using (ISession session = sessionFactory.OpenSession())
                {
                    PersonModel p = session.Get<PersonModel>(4);
                    session.Delete(p);
                    session.Flush();
                }

      从监控到,SQLServer执行的语句如下:

    exec sp_executesql N'DeletePerson @p0',N'@p0 int',@p0=5

      明显已经看到哥写的存储过程名字。如果不是执行存储过程,则执行的语句如下:

    exec sp_executesql N'DELETE FROM Person WHERE PersonId = @p0',N'@p0 int',@p0=5

      可以看到,如果你配置了存储过程,那么Delete()方法就执行存储过程,否则就执行NHibernate自己生成的SQL语句。

    二、添加

        测试了一个NHibernate的添加存储过程之后,感觉就一个字,不爽,估计这种方式配置的存储也不会是好的选择,还是生成语句比较好。

      存储过程如下:

    ALTER PROC InsertCountry
    @CountryName nvarchar(50),
    @CountryId int OUTPUT
    AS
    INSERT INTO Country VALUES(@CountryName);

      首先,添加将CountryId设置为自增的。

      然后配置文件如下(注意两个加粗的地方):

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class name="Model.CountryModel,Model" table="Country">
        <id name="CountryId" column="CountryId" type="Int32">
          <generator  class="increment"/>
        </id>
        <property name="CountryName" column="CountryName" type="String"/>
        <!-- 一个Country里面有多个Person -->
        <set name="ListPerson" table="Person" generic="true">
          <key column="CountryId" foreign-key="FK_Person_Country"/>
          <one-to-many class="Model.PersonModel,Model"/>
        </set>
        <sql-insert>InsertCountry ?,?</sql-insert>
      </class>
    </hibernate-mapping>
    复制代码

      设置increment的目的是主键,本处为CountryId由NHibernate生成。实际上不是一个什么好的方法,因为NHibernate是通过SQL语句:

    SELECT MAX(CountryId) FROM Country

      获得的。并且,你还必须设置数据库主键自增。一句话,不爽。都说了没什么人会选择这种方式咯。

      执行代码如下:

    复制代码
            static void Main(string[] args)
            {
                ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
    
                using (ISession session = sessionFactory.OpenSession())
                {
                    CountryModel c = new CountryModel();
                    c.CountryName = "青州";
                    session.Save(c);
                    session.Flush();
                }
                Console.ReadKey();
            }
    复制代码

      监控到SQL Server执行了:

    exec sp_executesql N'InsertCountry @p0,@p1',N'@p0 nvarchar(4000),@p1 int',@p0=N'青州',@p1=8

    三、更新

      更新的方式与Insert类似,不过好点过Insert了,还不错。

      存储过程:

    CREATE PROC UpdateCountry
    @CountryName nvarchar(50),
    @CountryId int OUTPUT
    AS
    UPDATE Country SET CountryName = @CountryName WHERE CountryId = @CountryId;

      映射文件:

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class name="Model.CountryModel,Model" table="Country">
        <id name="CountryId" column="CountryId" type="Int32">
          <generator  class="increment"/>
        </id>
        <property name="CountryName" column="CountryName" type="String"/>
        <!-- 一个Country里面有多个Person -->
        <set name="ListPerson" table="Person" generic="true">
          <key column="CountryId" foreign-key="FK_Person_Country"/>
          <one-to-many class="Model.PersonModel,Model"/>
        </set>
        <sql-insert>InsertCountry ?,?</sql-insert>
        <sql-update>UpdateCountry ?,?</sql-update>
      </class>
    </hibernate-mapping>
    复制代码

      执行代码如下:

    复制代码
            static void Main(string[] args)
            {
                ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
    
                using (ISession session = sessionFactory.OpenSession())
                {
                    CountryModel c = session.Get<CountryModel>(4);
                    c.CountryName = "修改测试";
                    session.Update(c);
                    session.Flush();
                }
    
                Console.ReadKey();
            }
    复制代码

      监控到SQL执行的语句如下:

    exec sp_executesql N'UpdateCountry @p0,@p1',N'@p0 nvarchar(4000),@p1 int',@p0=N'修改测试',@p1=4

    四、查询

       首先,创建一个存储过程如下:

    CREATE PROC EntityCountry
    @CountryId int
    AS
    SELECT * FROM Country WHERE CountryId =@CountryId 

      在NHibernate中,通过命名查询来映射存储过程,使用return返回具体的实体类,使用<return-property>告诉NHibernate使用哪些属性值,允许我们来选择如何引用字段以及属性。

      映射文件如下:

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class name="Model.CountryModel,Model" table="Country">
        <id name="CountryId" column="CountryId" type="Int32">
          <generator  class="increment"/>
        </id>
        <property name="CountryName" column="CountryName" type="String"/>
        <!-- 一个Country里面有多个Person -->
        <set name="ListPerson" table="Person" generic="true">
          <key column="CountryId" foreign-key="FK_Person_Country"/>
          <one-to-many class="Model.PersonModel,Model"/>
        </set>
      </class>
      <sql-query name="Cc">
        <return class="Model.CountryModel,Model" />
        EXEC EntityCountry :CountryId
      </sql-query>
    </hibernate-mapping>
    复制代码

      执行操作:

    复制代码
            static void Main(string[] args)
            {
                ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
    
                using (ISession session = sessionFactory.OpenSession())
                {
                    CountryModel c = session.GetNamedQuery("Cc").SetInt32("CountryId", 1).UniqueResult<CountryModel>();
                    Console.WriteLine(c.CountryName);
                }
    
                Console.ReadKey();
            }
    复制代码

      监控到SQL Server执行的语句如下:

    exec sp_executesql N'EXEC EntityCountry @p0',N'@p0 int',@p0=1

      考虑到在NHibernate中使用存储过程并不是一个很好的选择,也无谓学习得太深入了,这个真心不想用。

    五、总结

      <sql-query>配置在<class>后面。

      其他增删改要遵循以下顺序:

    •   <sql-insert>
    •   <sql-update>
    •   <sql-delete>

      OK,NHibernate执行存储过程就这么多了,以后用到的时候,再更新。

  • 相关阅读:
    codeblocks 配置
    2020-7-28
    echarts markPoint在极坐标散点图中不显示value值
    Oracle cve 2020-14644 分析利用以及回显思路
    Openfire Admin Console SSRF&任意文件读取漏洞 CVE-2019-18394 CVE-2019-18393 poc
    Shiro 回显利用工具(burp
    java反序列化提取payload之Xray高级版的shiro回显poc的提取过程
    CVE-2020-3452 CISCO ASA远程任意文件读取 poc
    记事本陈列-历届数学建模大赛优秀论文(含国赛、美赛、研赛)目录
    懒人必备 |通过爬虫 筛选以及查看CSDN 满足相应积分的资源列表 简单好用
  • 原文地址:https://www.cnblogs.com/liuguanghai/p/5031882.html
Copyright © 2011-2022 走看看