新建一个项目,第一个net版本的ssh项目,命名为:Nssh.project:
新建类库,作为项目model层
然后,开始编写代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Model { public class Users { public virtual int userID { get; set; } public virtual string uname { get; set; } public virtual Roles roles { get; set; }//一个用户有一个角色,一个角色可以有多个用户 } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Model { public class Roles { public virtual int rolesID { get; set; } public virtual string RolesName { get; set; } public virtual IList<Users> UserList { get; set; }//一个用户有一个角色,一个角色可以有多个用户 public virtual IList<Permission> permissionList { get; set; }//用户角色与权限是多对多关系 } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Model { public class Permission { public virtual int permissionID { get; set; } public virtual string PermissionName { get; set; } public virtual IList<Roles> rolesList { get; set; }//用户角色与权限是多对多关系 } }
hbm.xml文件
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model"> <class name="Model.Users, Model" table="P_Users"> <id name="userID" column="userID" type="int" > <generator class="native" /> </id> <property name="uname" column="uname" type="string" length="50" not-null="true"/> <many-to-one name="roles" column="RoleID" /> </class> </hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model"> <class name="Model.Roles, Model" table="P_Roles"> <id name="rolesID" column="rolesID" type="int" > <generator class="native" /> </id> <property name="RolesName" column="RolesName" type="string" length="50" not-null="true"/> <!--Bag:对象集合,每个元素可以重复。例如{1,2,2,6,0,0},在.Net中相当于IList或者IList<T> 实现。 Set:对象集合,每个元素必须唯一。例如{1,2,5,6},在.Net中相当于ISet或者ISet<T> 实现,Iesi.Collections.dll程序集提供ISet集合。 List:整数索引对象集合,每个元素可以重复。例如{{1,"YJingLee"},{2,"CnBlogs"},{3,"LiYongJing"}},在.Net中相当于ArraryList或者List<T> 实现。 Map:键值对集合。例如{{"YJingLee",5},{"CnBlogs",7},{"LiYongJing",6}},在.Net中相当于HashTable或者IDictionary<Tkey,TValue> 实现。 --> <bag name="UserList" inverse="true" cascade="all-delete-orphan" table="P_Users"> <key column="rolesID" foreign-key="FK_user_role"/> <one-to-many class="Model.Users, Model" /> </bag> <bag name="permissionList" table="P_Role_Permission"> <key column="rolesID" foreign-key="FK_role_permission"/> <many-to-many class="Permission" column="permissionID"/> </bag> </class> </hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model"> <class name="Model.Permission, Model" table="P_Permission"> <id name="permissionID" column="permissionID" type="int" > <generator class="native" /> </id> <property name="RolesName" column="RolesName" type="string" length="50" not-null="true"/> <bag name="rolesList" table="P_Role_Permission"> <key column="permissionID" foreign-key="FK_role_permission"/> <many-to-many class="Roles" column="rolesID"/> </bag> </class> </hibernate-mapping>
最后记得设置hbm.xml文件属性:
时间: 2024-10-23 20:01:19