Photon Server鍒濊瘑(涓? ---ORM鏄犲皠鏀硅繘

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Photon Server鍒濊瘑(涓? ---ORM鏄犲皠鏀硅繘相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%b5%8b%e8%af%95' title='娴嬭瘯'>娴嬭瘯   for   new   閲婃斁   view   cto   鎶ラ敊   lis   action   

 

涓€锛氭柊寤轰竴浜涚鐞嗙被锛?/p>

鎶€鏈浘鐗? src=

 

 

 

浜屻€佸疄鐜版瘡涓鐞嗙被

銆€銆€锛?锛塏HibernateHelper.cs 绫?绠$悊鏁版嵁搴撹繛鎺?/p>

 1 using NHibernate;
 2 using NHibernate.Cfg;
 3 
 4 namespace Nbibernate
 5 {
 6     public class NHibernateHelper
 7     {
 8         private static ISessionFactory _sessionFactory;
 9 
10         private static ISessionFactory SessionFactory
11         {
12             get
13             {
14                 if (_sessionFactory == null)
15                 {
16                     var config = new Configuration();
17                     config.Configure(); //瑙f瀽nhibernate.cfg閰嶇疆鏂囦欢
18                     config.AddAssembly("Nbibernate"); //瑙f瀽 鏄犲皠鏂囦欢
19 
20                     _sessionFactory = config.BuildSessionFactory();
21                 }
22 
23                 return _sessionFactory;
24             }
25         }
26 
27         public static ISession OpenSession()
28         {
29             ////鎵撳紑涓€涓窡鏁版嵁搴撶殑鍥炶瘽
30             return SessionFactory.OpenSession();
31         }
32     }
33 }

銆€銆€

銆€銆€锛?锛塈DManager.cs 绫?瀹氫箟涓€浜涙帴鍙?/p>

using System.Collections.Generic;
using Nbibernate.Model;

namespace Nbibernate.Manager
{
    internal interface IDbManager
    {
        void Add(DbModel msg);
        void Update(DbModel msg);
        void Remove(DbModel msg);

        DbModel GetById(int id);
        DbModel GetByName(string name);
        ICollection<DbModel> GetAllDb();

        bool VerifyModel(string name, int age);
    }
}

 

銆€銆€锛?锛塂bManager.cs 绫伙紝瀹炵幇鎺ュ彛锛屽疄鐜版暟鎹簱娣诲姞銆佷慨鏀广€佸垹闄ゅ拰鏌ヨ(鐗瑰埆娉ㄦ剰鎸夌収鍚嶅瓧鏌ヨ鏃舵湁鏃舵湁澶氫釜婊¤冻鏉′欢鐨勮繑鍥?

鎶€鏈浘鐗? id=
using System.Collections.Generic;
using Nbibernate.Model;
using NHibernate.Criterion;
using NHibernate.Util;

namespace Nbibernate.Manager
{
    public class DbManager : IDbManager
    {
        public void Add(DbModel msg)
        {
            ////session.Close();濡傛灉浣跨敤涓嬮潰鐨剈sing锛屽氨涓嶇敤鍐檚ession.Close()鏉ラ噴鏀緎ession浜嗭紝鍥犱负using浼氳嚜鍔ㄩ噴鏀俱€備笅闈㈢殑宓屽鏄厛閲婃斁transacion,鍐嶉噴鏀緎ession銆?/span>
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(msg);
                    transaction.Commit();
                }
            }
        }

        public void Update(DbModel msg)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Update(msg);
                    transaction.Commit();
                }
            }
        }

        public void Remove(DbModel msg)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(msg);
                    transaction.Commit();
                }
            }
        }

        public DbModel GetById(int id)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var msg = session.Get<DbModel>(id);
                    transaction.Commit();
                    return msg;
                }
            }
        }

        public DbModel GetByName(string name)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var iCriteria = session.CreateCriteria(typeof(DbModel));
                iCriteria.Add(Restrictions.Eq("Name", name));
                
                //浣跨敤 UniqueResult 鏌ヨ鏃?濡傛灉鏈夊涓弧瓒崇殑,杩欎細鎶ラ敊
                //DbModel msg = iCriteria.UniqueResult<DbModel>();

                //浣跨敤List鏌ヨ,杩欓噷鍙栫涓€涓繑鍥?/span>
                DbModel msg = null;
                var msgArr = iCriteria.List<DbModel>();
                if (msgArr.Any()) msg = msgArr[0];

                return msg;
            }
        }

        public ICollection<DbModel> GetAllDb()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var msgArr = session.CreateCriteria(typeof(DbModel)).List<DbModel>();

                return msgArr;
            }
        }

        public bool VerifyModel(string name, int age)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var iCriteria = session.CreateCriteria(typeof(DbModel));
                
                //澶氭潯浠舵煡璇?/span>
                iCriteria.Add(Restrictions.Eq("Name", name))
                    .Add(Restrictions.Eq("Age", age));

                DbModel msg = null;
                var msgArr = iCriteria.List<DbModel>();
                if (msgArr.Any()) return true;

                return false;
            }
        }
    }
}
View Code

 

銆€涓夈€佷慨鏀筆rogram.cs 鏂囦欢杩涜娴嬭瘯

銆€銆€锛?锛夈€佹坊鍔?/p>

namespace Nbibernate
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            DbModel msg = new DbModel() { Name = "gggggKddd",Title = "jttttt", Age = 100};
            IDbManager manager = new DbManager();
            manager.Add(msg);
        }
    }
}    

銆€銆€(2)銆佷慨鏀广€佸垹闄?/p>

namespace Nbibernate
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            IDbManager manager = new DbManager();
            DbModel msg = new DbModel() {Id = 8, Name = "kokoko", Age = 100, Title = "okoko"};
            manager.Update(msg); //鏇存柊,蹇呴』鎸囧畾ID

            DbModel msgrm = new DbModel() { Id = 1};
            manager.Remove(msgrm);  //鍒犻櫎
        } 
    }
}        

銆€锛?锛夋煡璇?/p>

namespace Nbibernate
{
    internal class Program
    {
        public static void Main(string[] args)
        {
 銆€銆€銆€銆€銆€銆€ IDbManager manager = new DbManager();
            
            //鎸夌収涓婚敭ID鏌ヨ
            DbModel msg = manager.GetById(2);
            Console.WriteLine(msg.Name);
            Console.WriteLine(msg.Age);

            //鎸夌収name鏌ヨ
            DbModel msgv2 = manager.GetByName("cjcjcc");
            Console.WriteLine(msgv2.Id);
            Console.WriteLine(msgv2.Age);
            
            //鑾峰彇鎵€鏈夋暟鎹?/span>
            ICollection<DbModel> msgArr = manager.GetAllDb();
            foreach (DbModel u in msgArr)
            {
                Console.WriteLine(u.Id + " " + u.Name + " " + u.Age);
            }

            //鏍规嵁name鍜宎ge鏌ヨ,鍒ゆ柇鏄惁瀛樺湪
            bool sit = manager.VerifyModel("kokoko",100);
            Console.WriteLine(sit);
        } 
    }
}        

 

銆€鏌ョ湅鏁版嵁搴撴暟鎹?鏄惁鎿嶄綔鎴愬姛

 

鍙傝€冩枃妗o細https://blog.csdn.net/qq_40323256/article/details/82914340

 

以上是关于Photon Server鍒濊瘑(涓? ---ORM鏄犲皠鏀硅繘的主要内容,如果未能解决你的问题,请参考以下文章

Python 操作Redis

python爬虫入门----- 阿里巴巴供应商爬虫

Python词典设置默认值小技巧

《python学习手册(第4版)》pdf

Django settings.py 的media路径设置

Python中的赋值,浅拷贝和深拷贝的区别