C# NHibernate MySQL 搭建与简单增删改查

hibernate.cfg.xml



  
    NHibernate.Connection.DriverConnectionProvider
    NHibernate.Dialect.MySQL5Dialect
    NHibernate.Driver.MySqlDataDriver
    
    Server=localhost;Port=3306;Database=mygamedb;User ID=root;Password=root;

    true
  


User.hbm.xml




  
      
        
    

    
    
    
  
  

User.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XpLearning.Model
{
    class User
    {
        public virtual Int32 Id { set; get; }
        public virtual string Username { set; get; }
        public virtual string Password { set; get; }
        public virtual DateTime Registedate { set; get; }
    }
}
IUserManager.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XpLearning.Model;

namespace XpLearning.Manager
{
    interface IUserManager
    {
        void AddUser(User user);
        void RemoveUser(User user);
        void UpDateUser(User user);

        User GetUserById(int id);
        User GetUserByName(string name);

        ICollection GetUsers();

        bool VerifyUser(string name, string password);
    }
}

重点:

UserManager.cs

using NHibernate;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XpLearning.Model;

namespace XpLearning.Manager
{
    class UserManager:IUserManager
    {
        public void AddUser(Model.User user)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(user);
                    transaction.Commit();
                }
            }
        }

        public void RemoveUser(Model.User user)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(user);//删除操作id号是关键
                    transaction.Commit();   //删除报错,找不到ID
                }
            }
        }

        public void UpDateUser(Model.User user)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(user);//更新操作id号是关键
                    transaction.Commit();
                }
            }
        }

        public ICollection GetUsers()
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                //ICriteria criteria = session.CreateCriteria(typeof(User));
                //criteria.Add(Restrictions.Eq("Username", name));
                //User user = criteria.UniqueResult();
                IList list =  session.CreateCriteria(typeof(User)).List();
                return list;
            }
        }


        public Model.User GetUserById(int id)
        {
            User user = null;
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    user = session.Get(id);//此处ID为主键
                    transaction.Commit();
                }
            }
            return user;
        }

        public Model.User GetUserByName(string name)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                //ICriteria criteria = session.CreateCriteria(typeof(User));
                //criteria.Add(Restrictions.Eq("Username", name));
                //User user = criteria.UniqueResult();
                //UniqueResult()方法:只返回第一个结果
                //Add(Restrictions.Eq("Username", name)方法,添加查询条件
                User user = session.CreateCriteria(typeof(User)).Add(Restrictions.Eq("Username", name)).UniqueResult();
                
                return user;
            }
        }


        public bool VerifyUser(string name, string password)
        {
            User user = null;
            using (ISession session = NHibernateHelper.OpenSession())
            {

                user = session.CreateCriteria(typeof(User))
                    .Add(Restrictions.Eq("Username", name))   //这里的字符串与User类对应
                    .Add(Restrictions.Eq("Password", password))
                    .UniqueResult();
            }
            if(user == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Cfg;
using XpLearning.Model;
using XpLearning.Manager;

namespace XpLearning
{
    class Program
    {
        static void Main(string[] args)
        {
            //Configuration configuration = new Configuration();
            //configuration.Configure();//解析配置文件var configuration = new Configuration();
            //configuration.AddAssembly("XpLearning");
            ////Console.WriteLine("Init ok");
            //ISessionFactory sessionFactory = null;
            //ISession session = null;
            //ITransaction transaction = null;
            //try
            //{
            //    sessionFactory = configuration.BuildSessionFactory();
            //    session = sessionFactory.OpenSession();

            //    transaction = session.BeginTransaction();
            //    User user1 = new User(){Username = "zhubajie", Password = "zhubajie"};
            //    session.Save(user1);
            //    User user2 = new User() { Username = "zhubajie1", Password = "zhubajie" };
            //    session.Save(user2);
            //    transaction.Commit();
            //}
            //catch(Exception e)
            //{
            //    Console.Write(e);
            //}
            //finally
            //{
            //    if(transaction != null)
            //    {
            //        transaction.Dispose();
            //    }
            //    if(session != null)
            //    {
            //        session.Close();
            //    }

            //    if (sessionFactory != null)
            //    {
            //        sessionFactory.Close();
            //    }
            //}
            // User user = new User() { Id = 1, Username = "shaheshang", Password = "shaheshang" };
            //User user = new User() { Id = 1};
            UserManager userManager = new UserManager();
            //userManager.AddUser(user);
            //userManager.RemoveUser(user);
            //User userGet =  userManager.GetUserById(1);
            //User userGet = userManager.GetUserByName("tangseng");

            //ICollection list = userManager.GetUsers();
            //foreach( User userGet in list)
            //{
            //    Console.WriteLine(userGet.Username);
            //    Console.WriteLine(userGet.Id);
            //}
            //Console.WriteLine(userGet.Username);
            //Console.WriteLine(userGet.Id);

            bool pass1 = userManager.VerifyUser("kangkang", "kangkang");
            bool pass2 = userManager.VerifyUser("kangkang1", "kangkang");
            Console.WriteLine(pass1);
            Console.WriteLine(pass2);
            Console.ReadKey();
        }
    }
}



你可能感兴趣的:(C#,MySQL)