zoukankan      html  css  js  c++  java
  • 实战3--设计管理模块, 第5步,实体设计技巧

    1. 设计实体:

    1. 有几个实体? 一组增删改查对应一个实体

    系统中的3个实体: department, role, user

    2. 实体键的关系: 页面引用了其他实体, 那么就是和这个实体有关系

    Department有上级和下级, 一对多

    Department 1 ======> * User *======> * Role

    3. 每个实体都有什么属性?

       1) 主键,  Long类型的id

       2) 关联关系属性

      Department: Set<User>, Department parent, Set<Department> children.

      User: Deparment department, Set<Role>

      Role: Set<User>

     3) 一般属性: 分析相关的页面

          Department: id, name, description, Set<User>, Department parent, Set<Department> children

          User: id, name, password, loginName, gender(String), phoneNumber(String), email, description, Set<Role>, Department department.

       4) 特殊属性, 比如要显示年龄, 不设计int age, 而是一个Date birthday字段, 年龄是计算出来的.

    写全每个实体类文件:

    Department.java:

    package cn.itcast.oa.domain;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class Department {
    	private Long id;
    	private String name;
    	private String description;
    	private Department parent;
    	private Set<Department> children = new HashSet<Department>();
    	private Set<User> users = new HashSet<User>();
    	
    	
    	public Long getId() {
    		return id;
    	}
    	public void setId(Long id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getDescription() {
    		return description;
    	}
    	public void setDescription(String description) {
    		this.description = description;
    	}
    	public Department getParent() {
    		return parent;
    	}
    	public void setParent(Department parent) {
    		this.parent = parent;
    	}
    	public Set<Department> getChildren() {
    		return children;
    	}
    	public void setChildren(Set<Department> children) {
    		this.children = children;
    	}
    	public Set<User> getUser() {
    		return users;
    	}
    	public void setUser(Set<User> user) {
    		this.users = user;
    	}
    }
    

     Role.java:

    package cn.itcast.oa.domain;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class Role {
    	private Long id;
    	private String name;
    	private String description;
    	private Set<User> users = new HashSet<User>();
    	
    	public Set<User> getUsers() {
    		return users;
    	}
    	public void setUsers(Set<User> users) {
    		this.users = users;
    	}
    	public Long getId() {
    		return id;
    	}
    	public String getName() {
    		return name;
    	}
    	public String getDescription() {
    		return description;
    	}
    	public void setId(Long id) {
    		this.id = id;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public void setDescription(String description) {
    		this.description = description;
    	}		
    }
    

     User.java

    package cn.itcast.oa.domain;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class User {
    	private long id;
    	private String name;
    	private String loginName;
    	private String password;
    	private String gender;
    	private String phoneNumber;
    	private String email;
    	private String description;
    	
    	private Department department;
    	private Set<Role> roles = new HashSet<Role>();
    	
    	
    	public String getLoginName() {
    		return loginName;
    	}
    	public void setLoginName(String loginName) {
    		this.loginName = loginName;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	public String getGender() {
    		return gender;
    	}
    	public void setGender(String gender) {
    		this.gender = gender;
    	}
    	public String getPhoneNumber() {
    		return phoneNumber;
    	}
    	public void setPhoneNumber(String phoneNumber) {
    		this.phoneNumber = phoneNumber;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	public String getDescription() {
    		return description;
    	}
    	public void setDescription(String description) {
    		this.description = description;
    	}
    	public Set<Role> getRoles() {
    		return roles;
    	}
    	public void setRoles(Set<Role> roles) {
    		this.roles = roles;
    	}
    	public Department getDepartment() {
    		return department;
    	}
    	public void setDepartment(Department department) {
    		this.department = department;
    	}
    	public long getId() {
    		return id;
    	}
    	public void setId(long id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    }
    

     写配置文件:

    1. 写注释: ?属性, 表达的是本对象与?的?关系

    2. 拷 模板   

    <!-- department属性, 本类与Department的  多对一 -->
    <many-to-one name="" class="" column=""></many-to-one>
    
    <!-- users属性, 本类与User的一对多 -->
    <set name="">
    	<key column=""></key>
    	<one-to-many class=""/>
    </set>
    
    <!-- users属性, 本类与User的 多对多 -->
    <set name="" table="">
    	<key column=""></key>
    	<many-to-many class="" column=""></many-to-many>
    </set>
    

    3. 填空:

        name属性: 属性名, 第1个?

        class属性: 关联的实体类型: 第2个?

     column属性:

             <many-to-one column="">: 关系里1的那个属性名Id, 比如departmentId

             一对多中<key column=""> : 从关联的一方映射中把column值拷过来. 上面的departmentId

             多对多中的<key column="">, 本对象名加Id, 如userId.

      注意: 一对多有两个表, 一个外键, 多对多有3个表, 2个外键.

     User->Department多对一模板:  name填写属性, class填写Department

    外键在多的一方, 所以column写 属性Id:departmentId

    <!-- department属性, 本类与Department的  多对一 -->
    <many-to-one name="department" class="Department" column="departmentId"></many-to-one>

     Department->User 一对多模板:  name填写属性users     class填写User

    column也上面多对一的那个departmentId

    <!-- users属性, 本类与User的一对多 -->
    <set name="users"> <key column="departmentId"></key> <one-to-many class="User"/> </set>

    Role->User 多对多模板: name填users,  class填User

                      table写itcast_user_role,  key column写 roleId, many-to-many写 userId

    <!-- users属性, 本类与User的 多对多 -->
    <set name="users" table="itcast_user_role"> <key column="roleId"></key> <many-to-many class="User" column="userId"></many-to-many> </set>

    User->Role多对多模板:  name填roles,  class填Role

         table和上面一样写itcast_user_role,  key column写 userId, many-to-many写 roleId

    <!-- roles属性, 本类与Role的  多对多 -->
    <set name="roles" table="itcast_user_role">
    	<key column="userId"></key>
    	<many-to-many class="Role" column="roleId"></many-to-many>
    </set>
    

    Department.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="cn.itcast.oa.domain">
    	<class name="Department" table="itcast_department">
    		<id name="id">
    			<generator class="native"></generator>
    		</id>
    		<property name="name" />
    		<property name="description" />
    		<!-- users属性, 本类与User的一对多 -->
    		<set name="users">
    			<key column="departmentId"></key>
    			<one-to-many class="User"/>
    		</set>
    		<!-- parent属性, 本类与Department上级的多对一 -->
    		<many-to-one name="parent" class="Department" column="parentId"></many-to-one>
    		<!-- children属性, 本类与下级Department的一对多 -->
    		<set name="children">
    			<key column="parentId"></key>
    			<one-to-many class="Department"/>
    		</set>
    	</class>
    </hibernate-mapping>
    

     Role.hbm.xml:

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="cn.itcast.oa.domain">
    	<class name="Role" table="itcast_role">
    		<id name="id">
    			<generator class="native"></generator>
    		</id>
    		<property name="name" />
    		<property name="description" />
    		<!-- users属性, 本类与User的 多对多 -->
    		<set name="users" table="itcast_user_role">
    			<key column="roleId"></key>
    			<many-to-many class="User" column="userId"></many-to-many>
    		</set>
    	</class>
    </hibernate-mapping>
    

     User.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="cn.itcast.oa.domain">
    	<class name="User" table="itcast_user">
    		<id name="id">
    			<generator class="native"></generator>
    		</id>
    		<property name="name" />
    		<property name="loginName" />
    		<property name="password" />
    		<property name="gender" />
    		<property name="phoneNumber" />
    		<property name="email" />
    		<property name="description" />
    		<!-- department属性, 本类与Department的  多对一 -->
    		<many-to-one name="department" class="Department" column="departmentId"></many-to-one>
    		
    		<!-- roles属性, 本类与Role的  多对多 -->
    		<set name="roles" table="itcast_user_role">
    			<key column="userId"></key>
    			<many-to-many class="Role" column="roleId"></many-to-many>
    		</set>
    	</class>
    </hibernate-mapping>
    
  • 相关阅读:
    Unix命令大全
    vs2008 与 IE8出现的兼容性问题
    Java 创建文件、文件夹以及临时文件
    如何修改Wamp中mysql默认空密码
    PAT 乙级真题 1003.数素数
    Tags support in htmlText flash as3
    DelphiXE4 FireMonkey 试玩记录,开发IOS应用 还是移植
    10 Great iphone App Review sites to Promote your Apps!
    HTML tags in textfield
    Delphi XE4 IOS 开发, "No eligible applications were found“
  • 原文地址:https://www.cnblogs.com/wujixing/p/5504922.html
Copyright © 2011-2022 走看看