美文网首页
FluentNHibernate SQL语句射映c#

FluentNHibernate SQL语句射映c#

作者: GB_speak | 来源:发表于2018-06-04 09:49 被阅读52次

    SQL即结构化查询语句(Structured Query LangUage)
    但是对于创建对象来说sql语句不够用不方便所以有了Orm

    ORM对象关系映射(Object Relational Mapping)
    那么C#如何使用ROM呢

    image.png
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using NHibernate;
    using FluentNHibernate.Cfg;
    using FluentNHibernate.Cfg.Db;
    
    namespace ArpgDatabase
    {
        public class NHibernateHelper
        {
            private static ISessionFactory sessionFactory = null;
    
            private static void InitializeSessionFactory()
            {
                //定义数据库对象(服务器地址,数据库名字,账号,密码)
                sessionFactory = Fluently.Configure()
                .Database(MySQLConfiguration.Standard.ConnectionString(db => db
               .Server("127.0.0.1")
               .Database("fourthmonthwork")
               .Username("root")
               .Password("1001")))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateHelper>())
                .ExposeConfiguration(e => e.Properties.Add("hbm2ddl.keywords", "none"))
                .BuildSessionFactory();
            }
    
            private static ISessionFactory SessionFactory
            {
                //类似于单利有且只有一个 InitializeSessionFactory();
                get
                {
                    if (sessionFactory == null)
                        InitializeSessionFactory();
    
                    return sessionFactory;
                }
            }
            /// <summary>
            /// 多条sql语句可以通过ISession来操作
            /// </summary>
            /// <returns></returns>
            public static ISession OpenSession()
            {
                return SessionFactory.OpenSession();
            }
        }
    }
    

    通过OpenSession方法提供一个ISession 对象共外界使用操作数据库

    using ArpgDatabase;
    using NHibernate;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace WWWServer0504
    {
        public class DBAccountController
        {
            private static DBAccountController instance;
            public static DBAccountController Instance
            {
                get
                {
                    if (instance == null)
                        instance = new DBAccountController();
                    return instance;
                }
            }
    
            public Account SelectAccount(string name)
            {
                ISession session = NHibernateHelper.OpenSession();
                IList<Account> list = session.QueryOver<Account>().Where(x => x.username == name).List();
                if (list.Count > 0)
                    return list[0];
                return null;
            }
    
            public void InsertAccount(string name , string pwd)
            {
                ISession session = NHibernateHelper.OpenSession();
                Account acc = new Account();
                acc.username = name;
                acc.password = pwd;
                session.Save(acc);
            }
        }
    }
    
    using FluentNHibernate.Mapping;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace WWWServer0504
    {
        public class AccountMap : ClassMap<Account>
        {
            public AccountMap()
            {
                Table("account");
                Id(x => x.id).Column("id");
                Map(x => x.username).Column("username");
                Map(x => x.password).Column("pwd");
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace WWWServer0504
    {
        public class Account
        {
            public virtual int id { get; set; }
            public virtual string username { get; set; }
            public virtual string password { get; set; }
        }
    }
    
    image.png

    相关文章

      网友评论

          本文标题:FluentNHibernate SQL语句射映c#

          本文链接:https://www.haomeiwen.com/subject/azdjsftx.html