Spring.Net+Nhibernate+ASP.MVC+EasyUI实践
目录
Spring.Net+Nhibernate+ASP.MVC+EasyUI实践(一)
Spring.Net+Nhibernate+ASP.MVC+EasyUI实践(二)
Spring.Net+Nhibernate+ASP.MVC+EasyUI实践(三)
Spring.Net+Nhibernate+ASP.MVC+EasyUI实践(四)
使用工具:VS2012,PostGreSQL
1.新建一个MVC4.0的项目,命名为:FinanceWebSys,选择.Net Framework 4.5
2.新建数据库,库名:FinanceWebDb ,建表
CREATE TABLE T_user_depinfo
(
id serial NOT NULL PRIMARY KEY,
depid int,
depname varchar(50)
)
3.向项目中添加新项目
模型:Model;数据库层:IDao,NhibernateDao;业务逻辑层:IBLL,BLL
4.01Model中建立基本对象
(1)新建类UserDepInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FinanceWebSys.Model
{
public class UserDepInfo
{
/// <summary>
/// ID
/// </summary>
public virtual int Id { get; set; }
/// <summary>
/// DepID
/// </summary>
public virtual int DepID { get; set; }
/// <summary>
/// DepName
/// </summary>
public virtual string DepName { get; set; }
}
}
(2)新建NHibernate映射文件UserDepInfo.hbm.xml
可以用CodeSmith、MyGeneration等工具辅助生成,也可以自己手写
(注:所有的XML映射都需要定义XML名称空间。XSD文件可以NHibernate的分发包src\nhibernate-mapping.xsd里找到。
小窍门: 为了开启编辑映射文件和配置文件的智能提示功能,请将上述.xsd文件拷贝到 <VS 2012 安装目录>\Xml\Schemas中去。)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="FinanceWebSys.Model" namespace="FinanceWebSys.Model">
<class name ="UserDepInfo" table="t_user_depinfo">
<cache usage="read-write"/>
<id name="Id" type="Int32" unsaved-value="null">
<column name="id" sql-type="int" not-null ="true" unique="true"/>
<generator class="identity"/>
</id>
<property name="DepID" type="Int32">
<column name ="depid" sql-type="int" not-null="false"/>
</property>
<property name="DepName" type="String">
<column name="depname" length="50" sql-type="varchar" not-null="false"/>
</property>
</class>
</hibernate-mapping>
(注意:在User.hbm.xml文件上 右击-->的属性 将[生成操作] 设置为嵌入资源。程序集要准确命名空间)
5.编写数据层接口IDao,引用Model(使用NuGet添加引用)
(1)采用Repository模式,编写IRepository.cs(用IList还是IQueryable需要考究一下)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FinanceWebSys.IDao
{
public interface IRepository<T> where T : class
{
/// <summary>
/// 获取实体
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
T Get(object id);
/// <summary>
/// 获取实体
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
T Load(object id);
/// <summary>
/// 插入实体
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
object Save(T entity);
/// <summary>
/// 修改实体
/// </summary>
/// <param name="entity"></param>
void Update(T entity);
/// <summary>
/// 修改或保存实体
/// </summary>
/// <param name="entity"></param>
void SaveOrUpdate(T entity);
/// <summary>
/// 删除实体
/// </summary>
/// <param name="id"></param>
void Delete(object id);
/// <summary>
/// 删除实体集合
/// </summary>
/// <param name="idList"></param>
void Delete(IList<object> idList);
/// <summary>
/// 获取全部集合
/// </summary>
/// <returns></returns>
IList<T> LoadAll();
/// <summary>
/// 分页获取全部集合
/// </summary>
/// <param name="count">记录总数</param>
/// <param name="pageIndex">页码</param>
/// <param name="pageSize">每页大小</param>
/// <returns></returns>
IList<T> LoadAllWithPage(out long count, int pageIndex, int pageSize);
}
}
(2)编写接口IUserDepInfoRepository.cs,新增方法LoadAllByPage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FinanceWebSys.Model;
namespace FinanceWebSys.IDao
{
public interface IUserDepInfoRepository : IRepository<UserDepInfo>
{
IList<UserDepInfo> LoadAllByPage(out long total, int page, int rows, string order, string sort);
IList<UserDepInfo> Get(string DepName);
}
}
6.编写数据层实现NhibernateDao,添加引用Model,IDao,Spring.Data.NHibernate32(用NuGet会自动添加其他依赖项)
(1)编写RepositoryBase.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FinanceWebSys.IDao;
using Spring.Data.NHibernate.Generic.Support;
namespace FinanceWebSys.NhibernateDao
{
public abstract class RepositoryBase<T>:HibernateDaoSupport,IRepository<T> where T:class
{
public virtual object Save(T entity)
{
return this.HibernateTemplate.Save(entity);
}
public virtual T Get(object id)
{
return this.HibernateTemplate.Get<T>(id);
}
public virtual T Load(object id)
{
return this.HibernateTemplate.Load<T>(id);
}
public virtual IList<T> LoadAll()
{
return this.HibernateTemplate.LoadAll<T>();
}
public virtual void Update(T entity)
{
this.HibernateTemplate.Update(entity);
}
public virtual void Delete(object id)
{
var entity=this.HibernateTemplate.Get<T>(id);
if (entity == null)
{
return;
}
this.HibernateTemplate.Delete(entity);
}
public virtual void Delete(IList<object> idList)
{
foreach(var item in idList)
{
var entity = this.HibernateTemplate.Get<T>(item);
if(entity==null)
{
return;
}
this.HibernateTemplate.Delete(entity);
}
}
public virtual void SaveOrUpdate(T entity)
{
this.HibernateTemplate.SaveOrUpdate(entity);
}
public virtual IList<T> LoadAllWithPage(out long count, int pageIndex, int pageSize)
{
IList<T> result = this.LoadAll();
count = result.LongCount();
return result.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
(2)编写UserDepInfoRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FinanceWebSys.IBLL;
namespace FinanceWebSys.BLL
{
public abstract class GenericManager<T>:IGenericManager<T> where T:class
{
public FinanceWebSys.IDao.IRepository<T> CurrentRepository { get; set; }
public virtual T Get(object id)
{
if (id == null)
{
return null;
}
return this.CurrentRepository.Get(id);
}
public virtual T Load(object id)
{
if(id==null)
{
return null;
}
return this.CurrentRepository.Load(id);
}
public virtual object Save(T entity)
{
if (entity == null)
{
return null;
}
return this.CurrentRepository.Save(entity);
}
public virtual void Update(T entity)
{
if (entity == null)
{
return;
}
this.CurrentRepository.Update(entity);
}
public virtual void Delete(object id)
{
if (id == null)
{
return;
}
this.CurrentRepository.Delete(id);
}
public virtual IList<T> LoadAll()
{
return this.CurrentRepository.LoadAll();
}
public virtual IList<T> LoadAllWithPage(out long count, int pageIndex, int pageSize)
{
return this.CurrentRepository.LoadAllWithPage(out count, pageIndex, pageSize);
}
public virtual void Delete(IList<object> idList)
{
if (idList == null || idList.Count == 0)
{
return;
}
this.CurrentRepository.Delete(idList);
}
public virtual void SaveOrUpdate(T entity)
{
if (entity == null)
{
return;
}
this.CurrentRepository.SaveOrUpdate(entity);
}
}
}
(2)编写UserDepInfoManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FinanceWebSys.Model;
using FinanceWebSys.IBLL;
namespace FinanceWebSys.BLL
{
public class UserDepInfoManager:GenericManager<UserDepInfo>,IUserDepInfoManager
{
public IList<UserDepInfo> LoadAllByPage(out long total, int page, int rows, string order, string sort)
{
return ((FinanceWebSys.IDao.IUserDepInfoRepository)(this.CurrentRepository)).LoadAllByPage(out total, page, rows, order, sort);
}
}
}
写到这里已经把数据层跟逻辑业务层基本写完了,下面接着就是配置问题了,这部分比较麻烦,放到下一篇博客讲