接上一篇文章 创建NHibernateHelper帮助类,生成sessionFactory
http://www.cnblogs.com/fzxiaoyi/p/8443587.html
创建个新的类Manager,演示对TestUser进行增删改查功能
编写代码如下:
using MySQLDateBase.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MySQLDateBase.Manager { /// <summary> /// 这个类用于对TestUser进行增删改查功能 /// </summary> class Manager { //获取TestUser所有用户信息 public IList<TestUser> GetAllUser() { using (var session = NHibernateHelper.OpenSession()) { using (var transaction = session.BeginTransaction()) { var userList = session.QueryOver<TestUser>(); transaction.Commit(); return userList.List(); } } } //通过用户名查询用户 public IList<TestUser> GetUserByUserName(string userName) { using (var session = NHibernateHelper.OpenSession()) { using (var transaction = session.BeginTransaction()) { var userList = session.QueryOver<TestUser>().Where(user => user.Username == userName); transaction.Commit(); return userList.List(); } } } //增加用户 public void AddUser(TestUser user) { using (var session = NHibernateHelper.OpenSession()) { using (var transaction = session.BeginTransaction()) { session.Save(user); transaction.Commit(); } } } //删除用户 public void DeleteUserById(int id) { using (var session = NHibernateHelper.OpenSession()) { using (var transaction = session.BeginTransaction()) { TestUser user = new TestUser(); user.Id = id; //注意如果指定主键不存在,就会抛出异常 session.Delete(user); transaction.Commit(); } } } //修改用户 public void ModifyUser(TestUser user) { using (var session = NHibernateHelper.OpenSession()) { using (var transaction = session.BeginTransaction()) { session.Update(user); transaction.Commit(); } } } //测试用的入口,注释掉Program.cs中的Main函数 static void Main(string[] arrgs) { Manager testUserManager = new Manager(); //获取所有用户信息 Console.WriteLine("获取所有用户信息:"); IList<TestUser> allUserList = testUserManager.GetAllUser(); foreach (TestUser item in allUserList) { Console.WriteLine("用户名:{0} | 密码:{1}" ,item.Username,item.Password); } //通过用户名查询用户 Console.WriteLine("******************************"); Console.WriteLine("通过用户名查询用户:"); IList<TestUser> testuserList_1 = testUserManager.GetUserByUserName("xiaoyi"); foreach (var item in testuserList_1) { Console.WriteLine("用户名:{0} | 密码:{1}",item.Username,item.Password); } //增加用户 Console.WriteLine("******************************"); Console.WriteLine("增加用户:"); TestUser newUser = new TestUser(); newUser.Username = "新用户777"; newUser.Password = "001002"; newUser.Age = 50; testUserManager.AddUser(newUser); //删除用户 Console.WriteLine("******************************"); Console.WriteLine("删除用户:"); //删除上面新添加的用户 testUserManager.DeleteUserById(newUser.Id); //修改用户 Console.WriteLine("******************************"); Console.WriteLine("修改用户:"); IList<TestUser> testuserList_2 = testUserManager.GetUserByUserName("xiaoyi"); //修改所有用户中的第一个用户的密码为abcdefg allUserList[0].Password = "abcdefg"; testUserManager.ModifyUser(allUserList[0]); //最后获取所有用户信息 Console.WriteLine("最后获取所有用户信息:"); IList<TestUser> allUserList_1 = testUserManager.GetAllUser(); foreach (TestUser item in allUserList) { Console.WriteLine("用户名:{0} | 密码:{1}", item.Username, item.Password); } Console.ReadKey(); } } }