在数据访问层中充分体现了Spring.NET和NHibernate的无缝集成,只要继承HibernateDaoSupport就能很便捷的使用NHibernate,而不需要很深入了解NHibernate。
3.1. 人事子系统领域模型层(DomainModel)
部门(Dept.cs)
using System;
using System.Collections.Generic;
using System.Text;
namespace Guushuuse.SalaryPrj.HR.DomainModel
{
/// <summary>
/// 部门
/// </summary>
public class Dept
{
private int _id;
private string _code;
private string _name;
属性 属性
构造函数 构造函数
}
}
using System.Collections.Generic;
using System.Text;
namespace Guushuuse.SalaryPrj.HR.DomainModel
{
/// <summary>
/// 部门
/// </summary>
public class Dept
{
private int _id;
private string _code;
private string _name;
属性 属性
构造函数 构造函数
}
}
员工(Employee.cs)
using System;
using System.Collections.Generic;
using System.Text;
namespace Guushuuse.SalaryPrj.HR.DomainModel
{
/// <summary>
/// 员工
/// </summary>
public class Employee
{
private int _id;
private string _code;
private string _name;
private Dept _dept;
属性 属性
构造函数 构造函数
}
}
using System.Collections.Generic;
using System.Text;
namespace Guushuuse.SalaryPrj.HR.DomainModel
{
/// <summary>
/// 员工
/// </summary>
public class Employee
{
private int _id;
private string _code;
private string _name;
private Dept _dept;
属性 属性
构造函数 构造函数
}
}
3.2. 人事子系统映射文件(HBM)
Dept.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Guushuuse.SalaryPrj.HR.DomainModel.Dept, Guushuuse.SalaryPrj.HR" table="t_depts">
<id name="ID" column="dept_id" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>
<property name="Code" column="dept_code" type="String" length="255" not-null="true" />
<property name="Name" column="dept_name" type="String" length="255" not-null="true" />
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Guushuuse.SalaryPrj.HR.DomainModel.Dept, Guushuuse.SalaryPrj.HR" table="t_depts">
<id name="ID" column="dept_id" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>
<property name="Code" column="dept_code" type="String" length="255" not-null="true" />
<property name="Name" column="dept_name" type="String" length="255" not-null="true" />
</class>
</hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Guushuuse.SalaryPrj.HR.DomainModel.Employee, Guushuuse.SalaryPrj.HR" table="t_employees">
<id name="ID" column="employee_id" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>
<property name="Code" column="employee_code" type="String" length="255" not-null="true" />
<property name="Name" column="employee_name" type="String" length="255" not-null="true" />
<many-to-one name="Dept" column="dept_id" class="Guushuuse.SalaryPrj.HR.DomainModel.Dept, Guushuuse.SalaryPrj.HR" not-null="true" />
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Guushuuse.SalaryPrj.HR.DomainModel.Employee, Guushuuse.SalaryPrj.HR" table="t_employees">
<id name="ID" column="employee_id" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>
<property name="Code" column="employee_code" type="String" length="255" not-null="true" />
<property name="Name" column="employee_name" type="String" length="255" not-null="true" />
<many-to-one name="Dept" column="dept_id" class="Guushuuse.SalaryPrj.HR.DomainModel.Dept, Guushuuse.SalaryPrj.HR" not-null="true" />
</class>
</hibernate-mapping>
3.3. 人事子系统数据访问层(Dao)
部门数据访问接口(IDeptDao.cs)
using System;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.HR.Dao
{
/// <summary>
/// 部门数据访问接口
/// </summary>
public interface IDeptDao
{
void CreateDept(Dept dept);
void DeleteDept(Dept dept);
IList GetAllDepts();
Dept GetDept(int deptID);
void UpdateDept(Dept dept);
}
}
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.HR.Dao
{
/// <summary>
/// 部门数据访问接口
/// </summary>
public interface IDeptDao
{
void CreateDept(Dept dept);
void DeleteDept(Dept dept);
IList GetAllDepts();
Dept GetDept(int deptID);
void UpdateDept(Dept dept);
}
}
部门数据访问类(DeptDao.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate.Support;
using Spring.Transaction.Interceptor;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.HR.Dao
{
/// <summary>
/// 部门数据访问类
/// </summary>
public class DeptDao : HibernateDaoSupport, IDeptDao
{
public DeptDao()
{
}
[Transaction(ReadOnly = false)]
public void CreateDept(Dept dept)
{
HibernateTemplate.Save(dept);
}
[Transaction(ReadOnly = false)]
public void UpdateDept(Dept dept)
{
HibernateTemplate.Update(dept);
}
[Transaction(ReadOnly = false)]
public void DeleteDept(Dept dept)
{
HibernateTemplate.Delete(dept);
}
public IList GetAllDepts()
{
return HibernateTemplate.LoadAll(typeof(Dept));
}
public Dept GetDept(int deptID)
{
return (Dept)HibernateTemplate.Get(typeof(Dept), deptID);
}
}
}
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate.Support;
using Spring.Transaction.Interceptor;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.HR.Dao
{
/// <summary>
/// 部门数据访问类
/// </summary>
public class DeptDao : HibernateDaoSupport, IDeptDao
{
public DeptDao()
{
}
[Transaction(ReadOnly = false)]
public void CreateDept(Dept dept)
{
HibernateTemplate.Save(dept);
}
[Transaction(ReadOnly = false)]
public void UpdateDept(Dept dept)
{
HibernateTemplate.Update(dept);
}
[Transaction(ReadOnly = false)]
public void DeleteDept(Dept dept)
{
HibernateTemplate.Delete(dept);
}
public IList GetAllDepts()
{
return HibernateTemplate.LoadAll(typeof(Dept));
}
public Dept GetDept(int deptID)
{
return (Dept)HibernateTemplate.Get(typeof(Dept), deptID);
}
}
}
员工数据访问接口(IEmployeeDao.cs)
using System;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.HR.Dao
{
/// <summary>
/// 员工数据访问接口
/// </summary>
public interface IEmployeeDao
{
void CreateEmployee(Employee employee);
void DeleteEmployee(Employee employee);
IList GetAllEmployees();
Employee GetEmployee(int employeeID);
void UpdateEmployee(Employee employee);
}
}
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.HR.Dao
{
/// <summary>
/// 员工数据访问接口
/// </summary>
public interface IEmployeeDao
{
void CreateEmployee(Employee employee);
void DeleteEmployee(Employee employee);
IList GetAllEmployees();
Employee GetEmployee(int employeeID);
void UpdateEmployee(Employee employee);
}
}
员工数据访问类(EmployeeDao.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate.Support;
using Spring.Transaction.Interceptor;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.HR.Dao
{
/// <summary>
/// 员工数据访问类
/// </summary>
public class EmployeeDao : HibernateDaoSupport, IEmployeeDao
{
public EmployeeDao()
{
}
[Transaction(ReadOnly = false)]
public void CreateEmployee(Employee employee)
{
HibernateTemplate.Save(employee);
}
[Transaction(ReadOnly = false)]
public void UpdateEmployee(Employee employee)
{
HibernateTemplate.Update(employee);
}
[Transaction(ReadOnly = false)]
public void DeleteEmployee(Employee employee)
{
HibernateTemplate.Delete(employee);
}
public IList GetAllEmployees()
{
return HibernateTemplate.LoadAll(typeof(Employee));
}
public Employee GetEmployee(int employeeID)
{
return (Employee)HibernateTemplate.Get(typeof(Employee), employeeID);
}
}
}
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate.Support;
using Spring.Transaction.Interceptor;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.HR.Dao
{
/// <summary>
/// 员工数据访问类
/// </summary>
public class EmployeeDao : HibernateDaoSupport, IEmployeeDao
{
public EmployeeDao()
{
}
[Transaction(ReadOnly = false)]
public void CreateEmployee(Employee employee)
{
HibernateTemplate.Save(employee);
}
[Transaction(ReadOnly = false)]
public void UpdateEmployee(Employee employee)
{
HibernateTemplate.Update(employee);
}
[Transaction(ReadOnly = false)]
public void DeleteEmployee(Employee employee)
{
HibernateTemplate.Delete(employee);
}
public IList GetAllEmployees()
{
return HibernateTemplate.LoadAll(typeof(Employee));
}
public Employee GetEmployee(int employeeID)
{
return (Employee)HibernateTemplate.Get(typeof(Employee), employeeID);
}
}
}
修改Config/Guushuuse.SalaryPrj.HR.config文件,新增object
<object id="deptDao" type="Guushuuse.SalaryPrj.HR.Dao.DeptDao, Guushuuse.SalaryPrj.HR">
<property name="HibernateTemplate" ref="hibernateTemplate" />
</object>
<object id="employeeDao" type="Guushuuse.SalaryPrj.HR.Dao.EmployeeDao, Guushuuse.SalaryPrj.HR">
<property name="HibernateTemplate" ref="hibernateTemplate" />
</object>
<property name="HibernateTemplate" ref="hibernateTemplate" />
</object>
<object id="employeeDao" type="Guushuuse.SalaryPrj.HR.Dao.EmployeeDao, Guushuuse.SalaryPrj.HR">
<property name="HibernateTemplate" ref="hibernateTemplate" />
</object>