MySql(二)_NHibernateHelper管理会话工厂
1.定义接口的好处:
(1) 清楚的看到里面有哪些方法;
( 2 ) 可以更换实现类;Nhibernate实现件可以更换;
Manger文件夹(另外两个是Model、Mappings文件夹)
首先Model文件内,(对象)类名,对应着数据库中的表名;在类中定义属性(即对应表中的列名)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace NBOWeb.Model 8 { 9 class Unity 10 { 11 public virtual int Id { get; set; } 12 13 public virtual string Name { get; set; } 14 15 public virtual int Age { get; set; } 16 17 public virtual int WorkAge { get; set; } 18 19 } 20 }
在Mappings文件夹内,存放的是Model中所建立的类(对应的是数据库中的表)的配置文件,将所有的类中的属性与数据库中的表中的列名真正对应起来。注意Model中的类名与Mapping中的解析文件名一一对应。 “unity.cs”与表名一致,“unity.cs”与“unity.hbm.xml”一致。
1 <?xml version="1.0" encoding="utf-8" ?> 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 3 assembly="NBOWeb" 4 namespace="NBOWeb.Model"> 5 <!--assembly= Sikiedu 这是程序集的名称 namespace 是指User在哪个命名空间下的 --> 6 7 <class name="Unity" table="unity"> 8 <id name="Id" column="id" type="Int32"> 9 <generator class="native"></generator> 10 <!--自动增涨--> 11 <!--nativ 是本地 使用数据库自带的生成器--> 12 </id> 13 <property name="Name" column ="name" type="String" ></property> 14 <property name="Age" column ="age" type="Int32" ></property> 15 <property name="WorkAge" column ="workage" type="Int32" ></property> 16 17 </class> 18 19 </hibernate-mapping>
Manager文件夹中,存放的是接口与接口的实现,“IUnityManger.cs”和“UnityManager.cs” "IUnityManager.cs"存放的是各种要实现的方法或功能; “UnityManager”存放的是这些方法怎么具体实现的。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using NBOWeb.Model; 7 8 namespace NBOWeb.Manger 9 { 10 interface IUnityManager 11 { 12 void Add(Unity unity); 13 void Update(Unity unity); 14 void Remove(Unity unity); 15 Unity GetById(int id); 16 Unity GetByName(string name); //先定义出来有哪些操作 17 18 ICollection<Unity> GetAllUnityCollection(); 19 20 bool VerifyUnity(string name, int age); 21 } 22 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml; 7 using NBOWeb.Model; 8 using NHibernate; 9 using NHibernate.Criterion; 10 11 namespace NBOWeb.Manger 12 { 13 class UnityManger : IUnityManager 14 { 15 16 17 public void Add(Unity unity) 18 { 19 //ISession session = NHibernateHelper.OpenSession(); 20 //session.Save(unity); 21 //session.Close(); 22 23 24 25 //ISession session = NHibernateHelper.OpenSession() //当使用OpenSession方法的时候,将SessionFactory属性 中字段可读get创建 “属性”的加强 26 using (ISession session = NHibernateHelper.OpenSession()) //需要释放的资源 using的用法 27 { 28 using (ITransaction transaction = session.BeginTransaction()) 29 { 30 session.Save(unity); 31 transaction.Commit(); 32 } 33 } //自动实现资源的释放 34 } 35 36 public ICollection<Unity> GetAllUnityCollection() 37 { 38 using (ISession session = NHibernateHelper.OpenSession()) //需要释放的资源 using的用法 39 { 40 //ICriteria criteria = session.CreateCriteria(typeof(Unity)); // criteria标准 41 //criteria.Add(Restrictions.Eq("Name",name)); //更新,根据主键来操作 Restrictions限制约束,严格相等 42 // //将查询条件封装成了对象 43 //Unity unity = criteria.UniqueResult<Unity>(); //调用唯一的结果, //只会取到查询结果的第一行记录 44 45 IList<Unity> unity = session.CreateCriteria(typeof(Unity)).List<Unity>(); 46 return unity; 47 } 48 } 49 50 public Unity GetById(int id) 51 { 52 using (ISession session = NHibernateHelper.OpenSession()) //需要释放的资源 using的用法 53 { 54 using (ITransaction transaction = session.BeginTransaction()) 55 { 56 Unity unity = session.Get<Unity>(id); //更新,根据主键来操作 57 transaction.Commit(); 58 return unity; 59 } 60 } 61 } 62 63 public Unity GetByName(string name) 64 { 65 using (ISession session = NHibernateHelper.OpenSession()) //需要释放的资源 using的用法 66 { 67 //ICriteria criteria = session.CreateCriteria(typeof(Unity)); // criteria标准 68 //criteria.Add(Restrictions.Eq("Name",name)); //更新,根据主键来操作 Restrictions限制约束,严格相等 69 // //将查询条件封装成了对象 70 //Unity unity = criteria.UniqueResult<Unity>(); //调用唯一的结果 71 72 Unity unity = session.CreateCriteria(typeof(Unity)).Add(Restrictions.Eq("Name", name)) 73 .UniqueResult<Unity>(); 74 return unity; 75 } 76 } 77 78 public void Remove(Unity unity) 79 { 80 using (ISession session = NHibernateHelper.OpenSession()) //需要释放的资源 using的用法 81 { 82 using (ITransaction transaction = session.BeginTransaction()) 83 { 84 session.Delete(unity); //更新,根据主键来操作 85 transaction.Commit(); 86 } 87 } 88 } 89 90 public void Update(Unity unity) 91 { 92 using (ISession session = NHibernateHelper.OpenSession()) //需要释放的资源 using的用法 93 { 94 using (ITransaction transaction = session.BeginTransaction()) 95 { 96 session.Update(unity); //更新,根据主键来操作 97 transaction.Commit(); 98 } 99 } 100 } 101 102 public bool VerifyUnity(string name, int age) 103 { 104 using (ISession session = NHibernateHelper.OpenSession()) //需要释放的资源 using的用法 105 { 106 107 Unity unity = session 108 .CreateCriteria(typeof(Unity)) //建立unity标准格式 109 .Add(Restrictions.Eq("Name", name)) //添加条件约束 110 .Add(Restrictions.Eq("Age", age)) 111 .UniqueResult<Unity>(); //主键 加深对“主键”的理解 112 if (unity==null) 113 { 114 return false; 115 } 116 else 117 { 118 return true; 119 } 120 121 } 122 } 123 } 124 }
NHibernateHelper.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using NHibernate; 7 using NHibernate.Cfg; 8 9 namespace NBOWeb 10 { 11 class NHibernateHelper 12 { 13 14 private static ISessionFactory _sessionFactory; 15 16 private static ISessionFactory SessionFactory 17 { 18 get 19 { 20 if (_sessionFactory==null) 21 { 22 var configuration=new Configuration(); 23 configuration.Configure(); 24 configuration.AddAssembly("NBOWeb"); 25 26 _sessionFactory = configuration.BuildSessionFactory(); 27 } 28 return _sessionFactory; 29 } 30 } 31 32 public static ISession OpenSession() 33 { 34 return SessionFactory.OpenSession(); 35 } 36 37 } 38 }
接口,具体在进行学习。
总的运行
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using NHibernate; 8 using NHibernate.Cfg; 9 using NBOWeb.Model; 10 using NBOWeb.Manger; 11 12 namespace MySql_StudyTest 13 { 14 class Program 15 { 16 static void Main(string[] args) 17 { 18 //var configuration=new Configuration(); 19 //configuration.Configure(); 20 //configuration.AddAssembly("NBOWeb"); 21 //ISessionFactory sessionFactory = null; 22 //ITransaction transaction = null; 23 //ISession session = null; 24 //try 25 //{ 26 // sessionFactory = configuration.BuildSessionFactory(); 27 // session = sessionFactory.OpenSession(); 28 29 // //Unity unity = new Unity() {Name = "L.Juhnko", Age = 29, WorkAge = 10 }; 30 // //session.Save(unity); 31 32 // //事务,对数据库更改 33 // transaction= session.BeginTransaction();//开启事务 34 // Unity unity1 = new Unity() { Name = "ko01", Age = 29, WorkAge = 10 }; 35 // Unity unity2 = new Unity() { Name = "ko02", Age = 29, WorkAge = 10 }; 36 37 // session.Save(unity1); 38 // session.Save(unity2); 39 // transaction.Commit();//事务提交 40 //} 41 //catch (Exception e) 42 //{ 43 // Console.WriteLine(e); 44 45 //} 46 //finally 47 48 //{ 49 // if (transaction!=null) 50 // { 51 // transaction.Dispose(); 52 // } 53 54 // if (session!=null) 55 // { 56 // session.Close(); 57 // } 58 // if (sessionFactory!=null) 59 // { 60 // sessionFactory.Close(); 61 // } 62 //} 63 64 //Unity unity=new Unity() { Id=0}; //创建一个Unity的对象 65 //IUnityManager unityManager=new UnityManger(); //接口的应用 “接口” //此时与数据库未发生链接 66 ////unityManager.Add(unity); 67 68 ////unityManager.Update(unity); 69 //unityManager.Remove(unity); 70 71 //IUnityManager unityManager = new UnityManger(); 72 //Unity unity = unityManager.GetById(23); 73 74 //Console.WriteLine(""); 75 //Console.WriteLine(unity.Id); 76 //Console.WriteLine(unity.Name); 77 //Console.WriteLine(unity.Age); 78 //Console.WriteLine(unity.WorkAge); 79 //Console.ReadKey(); 80 81 82 83 84 85 //IUnityManager unityManager=new UnityManger(); 86 //Unity unity = unityManager.GetByName("juhnko"); 87 88 //Console.WriteLine(""); 89 //Console.WriteLine(unity.Id); 90 //Console.WriteLine(unity.Name); 91 //Console.WriteLine(unity.Age); 92 //Console.WriteLine(unity.WorkAge); 93 //Console.ReadKey(); 94 95 96 97 98 99 100 101 //IUnityManager unityManager = new UnityManger(); 102 //ICollection<Unity> unity= unityManager.GetAllUnityCollection(); 103 //foreach (var u in unity) 104 //{ 105 // Console.WriteLine("用户名:{0}" + "\t" + "Id:{1}" + "\t" + "年龄:{2}" + "\t" + "工作年龄:{3}"+ "\n", u.Name, u.Id, u.Age, u.WorkAge); 106 //} 107 //Console.ReadKey(); 108 109 110 111 IUnityManager unityManager = new UnityManger(); 112 Console.WriteLine(unityManager.VerifyUnity("juhnko",30)); 113 114 Console.ReadKey(); 115 116 } 117 } 118 }