Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
nhibernate教程_sunchance使用说明,希望能够帮助你!!!。
NHibernate是一个面向.NET环境的对象/关系数据库映射工具。对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。
NHibernate下载地址:NHibernate
hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property> <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property> <property name="connection.connection_string">Server=localhost;Database=mygamedb;User ID=root;Password=123;</property> <property name="hbm2ddl.keywords">none</property> <property name="show_sql">true</property> </session-factory> </hibernate-configuration>
注意:hibernate.cfg.xml文件的属性中“复制到输出目录”应设置为“始终复制”
< property name=“hbm2ddl.keywords”>none这个配置不加的话会报错,暂时不知道为什么,知道的老铁评论区留个言,谢谢!
1.创建一个User类:
using System; namespace MyGameServer.Model { public class User { public virtual int Id { get; set; } public virtual string Username { get; set; } public virtual string Password { get; set; } public virtual DateTime Registerdate { get; set; } } }
2.创建User与表的映射文件User.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyGameServer5" namespace="MyGameServer5.Model"> <class name="User" table="users"> <id name="Id" column="id" type="Int32"> <generator class="native"></generator> </id> <property name="Username" column="username" type="String"></property> <property name="Password" column="password" type="String"></property> <property name="Registerdate" column="registerdate" type="Date"></property> </class> </hibernate-mapping>
**注意:User.hbm.xml文件的属性中“复制到输出目录”应设置为“不复制”,“生成操作”设置为“嵌入的资源” **
assembly:该项目的程序集
namespace:User类的命名空间
< class name=“User”>:配置User类
< id name=“Id” colum=“id” type=“Int32”>:配置主键Id
< property name=“Username” colum=“username” type=“String”>:配置普通字段
name表示类中的属性名 colum表示数据库表中的字段名
1.创建一个Configuration对象,用来解析xml文件
var configuration = new Configuration();
2.解析数据库链接配置文件/解析hibernate.cfg.xml
configuration.Configure();
3.解析表映射文件User.hbm.xml…
configuration.AddAssembly(“MyGameServer5”);
using System; using NHibernate; using NHibernate.Cfg; using MyGameServer5.Model; namespace MyGameServer5 { class Program { static void Main(string[] args) { var configuration = new Configuration(); configuration.Configure(); //解析hibernate.cfg.xml configuration.AddAssembly("MyGameServer5"); //解析表映射文件User.hbm.xml... Console.ReadKey(); } } }
1.获取会话工厂
sessionFactory = configuration.BuildSessionFactory();
2.开启一个和数据库的会话session
session = sessionFactory.OpenSession();
3.进行插入操作
User user = new User() { Username = “cch”, Password = “oop12” };
session.Save(user);
using System; using NHibernate; using NHibernate.Cfg; using MyGameServer5.Model; namespace MyGameServer5 { class Program { static void Main(string[] args) { var configuration = new Configuration(); configuration.Configure(); //解析hibernate.cfg.xml configuration.AddAssembly("MyGameServer5"); //解析表映射文件User.hbm.xml... ISessionFactory sessionFactory = null; ISession session = null; try { //获取会话工厂 sessionFactory = configuration.BuildSessionFactory(); //开启一个和数据库的会话session session = sessionFactory.OpenSession(); //进行插入操作 User user = new User() { Username = "cch", Password = "oop12" }; session.Save(user); } catch (Exception e) { Console.WriteLine(e); } finally { if (session != null) { session.Close(); } if (sessionFactory != null) { sessionFactory.Close(); } } Console.ReadKey(); } } }
using System; using NHibernate; using NHibernate.Cfg; using MyGameServer5.Model; namespace MyGameServer5 { class Program { static void Main(string[] args) { var configuration = new Configuration(); configuration.Configure(); //解析hibernate.cfg.xml configuration.AddAssembly("MyGameServer5"); //解析Ma映射文件User.hbm.xml... ISessionFactory sessionFactory = null; ISession session = null; ITransaction transaction = null; try { //获取会话工厂 sessionFactory = configuration.BuildSessionFactory(); //开启一个和数据库的会话session session = sessionFactory.OpenSession(); transaction = session.BeginTransaction(); User user = new User() { Username = "test1", Password = "oop12" }; User user1 = new User() { Username = "test1", Password = "oop12" }; session.Save(user); session.Save(user1); transaction.Commit(); } catch (Exception e) { Console.WriteLine(e); } finally { if (transaction != null) { transaction.Dispose(); } if (session != null) { session.Close(); } if (sessionFactory != null) { sessionFactory.Close(); } } Console.ReadKey(); } } }
使用单例模式:从工厂ISessionFactory中取出Session
using System; using NHibernate; using NHibernate.Cfg; namespace MyGameServer5 { class SessionFactory { private static ISessionFactory sessionFactory; public static ISessionFactory MSessionFactory { get { if (sessionFactory == null) { var configuration = new Configuration(); configuration.Configure(); //解析hibernate.cfg.xml configuration.AddAssembly("MyGameServer5"); //解析Ma映射文件User.hbm.xml... sessionFactory = configuration.BuildSessionFactory(); } return sessionFactory; } } public static ISession OpenSession() { return MSessionFactory.OpenSession(); } } }
1.添加操作:session.Save(user);
2.更新操作:session.Update(user);
3.删除操作:session.Delete(user);
4.简单查询操作:User user = session.Get< User>(id);
5.复杂查询操作:使用session得到ICriteria对象进行复杂查询
ICriteria criteria = session.CreateCriteria< User>();
添加查询条件Username为数据库中的字段名,username为查询的条件值,Add方法依然返回ICriteria对象,可添加多个条件进行查询
criteria.Add(Restrictions.Eq(“Username”,username)).Add(…)…;
criteria.UniqueResult< T>();获取单个结果,criteria.List< T>()获取多个结果集合
User user = criteria.UniqueResult< User>();
IList users = criteria.List< User>();
注意1:添加更新删除操作要进行事务Transaction操作,查询操作不需要
注意2:using(对象){}:using小括号中的对象只能作用在花括号范围,花括号执行结束,对象被释放,使用using可省略session.Close()操作
IUserManager:
using System; using System.Collections.Generic; using MyGameServer5.Model; namespace MyGameServer5.Manager { public interface IUserManager { void Add(User user); void Update(User user); void Delete(User user); User GetUserById(int id); User GetUserByUsername(string username); List<User> GetAll(); bool Verfiy(string username, string password); } }
UserManager:
using System; using System.Collections.Generic; using System.Linq; using NHibernate; using NHibernate.Criterion; using MyGameServer5.Model; namespace MyGameServer5.Manager { public class UserManager:IUserManager { public void Add(Model.User user) { using(ISession session = SessionFactory.OpenSession()){ using(ITransaction transation = session.BeginTransaction()){ session.Save(user); transation.Commit(); } } } public void Update(Model.User user) { using (ISession session = SessionFactory.OpenSession()) { using (ITransaction transation = session.BeginTransaction()) { session.Update(user); transation.Commit(); } } } public void Delete(Model.User user) { using (ISession session = SessionFactory.OpenSession()) { using (ITransaction transation = session.BeginTransaction()) { session.Delete(user); transation.Commit(); } } } public Model.User GetUserById(int id) { ISession session = SessionFactory.OpenSession(); User user = session.Get<User>(id); return user; } public Model.User GetUserByUsername(string username) { ISession session = SessionFactory.OpenSession(); ICriteria criteria = session.CreateCriteria<User>(); criteria.Add(Restrictions.Eq("Username",username)); User user = criteria.UniqueResult<User>(); return user; } public List<Model.User> GetAll() { ISession session = SessionFactory.OpenSession(); ICriteria criteria = session.CreateCriteria(typeof(User)); IList<User> users = criteria.List<User>(); return users.ToList<User>(); } public bool Verfiy(string username, string password) { bool flag = false; ISession session = SessionFactory.OpenSession(); ICriteria criteria = session.CreateCriteria(typeof(User)); criteria.Add(Restrictions.Eq("Username", username)); criteria.Add(Restrictions.Eq("Password", password)); User user = criteria.UniqueResult<User>(); if (user != null) { flag = true; } return flag; } } }
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章