一:新建一些管理类,
二、实现每个管理类
(1)NHibernateHelper.cs 类,管理数据库连接
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(); //解析nhibernate.cfg配置文件 18 config.AddAssembly("Nbibernate"); //解析 映射文件 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 }
(2)IDManager.cs 类,定义一些接口
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); } }
(3)DbManager.cs 类,实现接口,实现数据库添加、修改、删除和查询(特别注意按照名字查询时有时有多个满足条件的返回)
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();如果使用下面的using,就不用写session.Close()来释放session了,因为using会自动释放。下面的嵌套是先释放transacion,再释放session。 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查询,这里取第一个返回 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)); //多条件查询 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; } } } }
三、修改Program.cs 文件进行测试
(1)、添加
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)、修改、删除
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); //删除 } } }
(3)查询
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); //获取所有数据 ICollection<DbModel> msgArr = manager.GetAllDb(); foreach (DbModel u in msgArr) { Console.WriteLine(u.Id + " " + u.Name + " " + u.Age); } //根据name和age查询,判断是否存在 bool sit = manager.VerifyModel("kokoko",100); Console.WriteLine(sit); } } }
查看数据库数据,是否操作成功
参考文档:https://blog.csdn.net/qq_40323256/article/details/82914340
原文地址:https://www.cnblogs.com/cj8988/p/11661877.html
时间: 2024-11-14 11:03:07