SSM入门:彻底弄懂实体层entity/POJO

一.基础实体类部分:BaseEntity

@SupTreeList
又定义一个抽象类BaseEntity,该抽象类实现接口Serializable
public abstract class BaseEntity implements Serializable{
    private static final long serialVersionUID = 1L;
   

//先记住id属性,这是最重要的
    protected String id;
  

 //这个五个属性先忽略
    protected User currentUser;
    protected Page page;
    protected Map sqlMap;
    protected Map> inSqlMap;
    protected boolean isNewRecord = flase;
   

//再知道这里的三个常量属性:DEL_FLAG_NORMAL,DEL_FLAG_DELETE,DEL_FLAG_AUDIT
    public static final String DEL_FLAG_NORMAL = "0";
    public static final String DEL_FLAG_DELETE = "1";
    public static final String DEL_FLAG_AUDIT = "2";

  

 //定义无参构造方法:BaseEntity()
      在何时调用?在哪里调用?调用后做了什么?
    public BaseEntity(){}

   

//定义有参构造方法:BaseEntity(String id)
      在何时调用?在哪里调用?调用后做什么?
    public BaseEntity(String id){
      this();//调用无参构造方法!为什么在这里调用呢?
      this.id = id;
    }

  

 //id属性的getter方法getId():多了1个注解,问题:注解做了什么?
    @SupCol(isUnique="true",isHide="true")
    public String getId(){
        return this.id;
    }
   

//id属性的setter方法setId():正常
    public void setId(String id){
        this.id = id;
    }
   
  

 //currentUser属性的getter方法:getCurrentUser():1.多了两个注解;2.与一般的setter不同,不是直接返回,而是初始化之后再返回!
    //两种返回的区别在哪里
    @JsonIgnore
    @XmlTransient
    public User getCurrentUser(){
        //先忽略这个代码:
        if(this.currentUser == null){
            //初始化currentUser属性:值从哪来呢?
            this.currentUser = UserUtils.getUser();
            //为什么要初始化呢?
            //之前的为什么都没有初始化呢?
            //如果没有初始化,可能调用该getter方法取到的值就是空
            //先初始化无非就是让该方法不返回空值
        }
        return this.currentUser;
    }
  

 //currentUser属性的setter方法setCurrentUser():正常
 //该方法在何时调用?在哪里调用?由谁调用?
    public void setCurrentUser(User currentUser){
        this.currentUser = currentUser;
    }  
  

    //page属性的getter方法 getPage():1.多了两个注解;2.与一般getter不同,不是直接返回,而是先初始化再返回!两种返回的区别在哪里?
    @JsonIgnore
    @XmlTransient
    //任何时候取page属性值时,都不允许取空值
    //该方法在何时调用?在哪里调用?由谁调用?
    public Page getPage(){
        if(this.page == null){
            //初始化:值从哪里来呢?
            this.page = new Page();
        }
        //然后再返回
        return this.page;
    }     

   
    //page属性的setter方法setPage():有返回值!
    //为什么page属性的setter方法会有返回值呢?
    //莫非该方法调用后,还能用到返回值?
    public Page setPage(Page page){
        this.page = page;
        return page;
    }

    //sqlMap属性的getter方法getSqlMap():1.多了两个注解;2.先初始化,再返回!   
    @JsonIgnore
    @XmlTransient
    //不允许sqlMap取空值
    public Map getSqlMap(){
        if(this.sqlMap == null){
            //先初始化
            this.sqlMap = Maps.newHashMap();
        }
        return this.sqlMap;
    }
   
    //setInSqlMap属性的setter方法:setSqlMap()正常
    public void setSqlMap(Map sqlMap){
        this.sqlMap = sqlMap;   
    }
   

    //定义getInSqlMap()方法 getInSqlMap():1.该方法多了两个注解;其他都正常!
    @JsonIgnore
    @XmlTransient
    //该方法允许获取空值?还是一般情况下,inSqlMap不为空值?
    public Map> getInSqlMap(){
        return this.inSqlMap;
    }

    //定义setInSqlMap()方法setInSqlMap():十分不正常
    public void setInSqlMap(Map> inSqlMap){
        if(inSqlMap == null){
            return;//结束本方法的执行,前提:inSqlMap为null
        }
        //只有inSqlMap不为空值才进行设置操作
        //忽略
        StringBuffer sqlIn = new StringBuffer();
        //忽略
        StringBuffer sqlInKeySet = inSqlMap.keySet();
       
        for(String key : sqlInKeySet){

            List values = (List)inSqlMap.get(key);
           
            if(values.size() >= 1){
                for(int i = 0; i < values.size(); i++){
                    if(i == values.size() - 1){

                       sqlIn.append("'" + values.get(i) + "'");

                    }else{

                       sqlIn.append("'" + values.get(i) + "',");

                    }
                }
                sqlIn.insert(0,"AND" + key + "IN(");

                sqlIn.append(")");
            }
            getSqlMap().put("IN",sqlIn.toString());
        }
        this.inSqlMap = inSqlMap;
    }

    //先把方法都挨个定义出来,再逐一完善
   
    //再定义一个setInSqlMap方法(与上面参数不同) 
  
    //定义preInsert()方法:先忽略这个方法
    public abstract void preInsert();

    //定义preUpdate()方法:先忽略这个方法
    public abstract void preUpdate();


    //定义getIsNewRecord()方法
    @JsonIgnore
    public boolean getIsNewRecord(){
   
    }


    //定义setIsNewRecord()方法
    public void setIsNewRecord(){

    }


    //定义Global属性的getter方法:先忽略这个方法
    @JsonIgnore
    public Global getGlobal(){

    } 

    //定义Dbname属性的getter方法:先忽略这个方法
    public String getDbname(){

    }

    //定义equals方法:先忽略这个方法
    public boolean equals(){


    }
  
   
    //定义toString方法:先忽略这个方法
    public String toString(){
        return ReflectionToStringBuilder.toString(this);
    }

}

总结:首先这个类定义的属性有:id;currentUser;page;sqlMap;inSqlMap;isNewRecord;DEL_FLAG_NORMAL;DEL_FLAG_DELETE;DEL_FLAG_AUDIT
//这个BaseEntity中定义的在结构上与其他实体类共同的方法有:实体类的构造方法,属性的getter和setter方法
//在属性的getter方法中,如何才能让gett方法不取空值?在方法中先初始化该属性!注意:在这里初始化有前提条件:该属性为空值null
//也就是说,逻辑是这样:如果该属性为null,那么就先初始化该属性!
//除此之外还定义的方法有:
     1.getGlobal()
     2.getDbname()
     3.equals()
     4.toString()
     5.preInsert()
     6.preUpdate()
//这里5和6两种方法是抽象方法

现在重点研究一下Map类
Map类是一个接口类:
其中,K和V表示两种数据类型!
接口类中只能定义方法:定义属性也没用的,又不能被实例化
Map类定义的方法有:
size();isEmpty();containsKey(Object key);containsValue(Object value);这些都叫查询操作!
get(Object key);put(K key, V value);remove(Object key);这些都叫更改操作
putAll(Map m);clear();这些叫批量操作
keySet();values();entrySet();
equals(Object o);hashCode();比较和hash操作!!


public interface Map {
    // Query Operations(查询操作)
    //int size()方法的说明
    /**
     * Returns the number of key-value mappings in this map.  If the
       (返回当前映射map实例中,键值映射对的数量;如果当前映射实例map中包含的元素的数量)
     * map contains more than Integer.MAX_VALUE elements, returns
       (超过了Integer.MAX_VALUE,那么调用该方法将只会返回Integer.MAX_VALUE)
     * Integer.MAX_VALUE.
     *
     * @return the number of key-value mappings in this map
     */(对返回值的说明:返回当前映射实例map中键值对映射的数量)
    int size();

    /**isEmpty()方法的说明
     * Returns true if this map contains no key-value mappings.
     * (如果该映射实例map中包含的键值对映射的数目为0,那么将会返回true)
     * @return true if this map contains no key-value mappings
     */(对返回值的说明:如果该映射实例map中包含的键值对映射的数目为0,那么将会返回true)
    boolean isEmpty();

    /**containsKey(Object key)方法的说明
     * Returns true if this map contains a mapping for the specified
       (如果当前映射实例map中一个有这个指定的key的映射对的话,就返回true)
     * key.  More formally, returns true if and only if
       (更正式的表达是:当且仅当这个映射实例map含有一个对该key的映射时,)
     * this map contains a mapping for a key k such that
       (比如:如果key为null,那就检测map实例中的k有没有为null的)
     * (key==null ? k==null : key.equals(k)).  (There can be
       (如果有,就返回true,如果key不为null,就将key和k做相等比较)
     * at most one such mapping.)
     *
     * @param key key whose presence in this map is to be tested
     * @return true if this map contains a mapping for the specified
     *         key
     * @throws ClassCastException if the key is of an inappropriate type for
     *         this map
     * (optional)
     * @throws NullPointerException if the specified key is null and this map
     *         does not permit null keys
     * (optional)
     */
    boolean containsKey(Object key);

    /**containsValue(Object value)方法说明
     * Returns true if this map maps one or more keys to the
       (如果当前映射实例中对于给定的这个value能映射一个或多个键的话,就返回true)
     * specified value.  More formally, returns true if and only if
       (理论上讲,当且仅当map实例包含至少一个对给定值value的映射对时,才返回true)
     * this map contains at least one mapping to a value v such that
       (比如,先检测value是否为null值,如果为null值就检测v有没有null值,如果有就返回true)
     * (value==null ? v==null : value.equals(v)).  This operation
       (如果没有就返回false;如果不为null值,就将value与map中的v比较,有相等就返回true,否则返回false)
     * will probably require time linear in the map size for most
     * implementations of the Map interface.
     *
     * @param value value whose presence in this map is to be tested
     * @return true if this map maps one or more keys to the
     *         specified value
     * @throws ClassCastException if the value is of an inappropriate type for
     *         this map
     * (optional)
     * @throws NullPointerException if the specified value is null and this
     *         map does not permit null values
     * (optional)
     */
    boolean containsValue(Object value);

    /**get(Object key)方法说明
     * Returns the value to which the specified key is mapped,
      (返回指定key映射的那个值)
     * or {@code null} if this map contains no mapping for the key.
     *(如果这个map实例不含有对该key的映射就返回Null值)
     *

More formally, if this map contains a mapping from a key
      (如果这个map映射实例包含一个从该key到value的映射)
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
      (比如先检测有没有对应的key值,如果有的话,就返回对应key的k对应的v)
     * key.equals(k))}, then this method returns {@code v}; otherwise
     * it returns {@code null}.  (There can be at most one such mapping.)
     *
     *

If this map permits null values, then a return value of
     * {@code null} does not necessarily indicate that the map
     * contains no mapping for the key; it's also possible that the map
     * explicitly maps the key to {@code null}.  The {@link #containsKey
     * containsKey} operation may be used to distinguish these two cases.
     *
     * @param key the key whose associated value is to be returned
     * @return the value to which the specified key is mapped, or
     *         {@code null} if this map contains no mapping for the key
     * @throws ClassCastException if the key is of an inappropriate type for
     *         this map
     * (optional)
     * @throws NullPointerException if the specified key is null and this map
     *         does not permit null keys
     * (optional)
     */
    V get(Object key);

    // Modification Operations

    /**
     * Associates the specified value with the specified key in this map
       (在该map实例中,将特定的值和特定的key联系起来)
     * (optional operation).  If the map previously contained a mapping for
       (如果 映射实例map中先前包含对该键的映射)
     * the key, the old value is replaced by the specified value.  (A map
        那么, 旧值将会被指定的这个value值替换掉(我怎么就没想到过这个问题呢?)
     * m is said to contain a mapping for a key k if and only
     * if {@link #containsKey(Object) m.containsKey(k)} would return
     * true.)
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with key, or
     *         null if there was no mapping for key.
     *         (A null return can also indicate that the map
     *         previously associated null with key,
     *         if the implementation supports null values.)
     * @throws UnsupportedOperationException if the put operation
     *         is not supported by this map
     * @throws ClassCastException if the class of the specified key or value
     *         prevents it from being stored in this map
     * @throws NullPointerException if the specified key or value is null
     *         and this map does not permit null keys or values
     * @throws IllegalArgumentException if some property of the specified key
     *         or value prevents it from being stored in this map
     */
    V put(K key, V value);

    /**
     * Removes the mapping for a key from this map if it is present
       (从当前映射实例map中移除对该键的映射)
     * (optional operation).   More formally, if this map contains a mapping
     * from key k to value v such that
     * (key==null ?  k==null : key.equals(k)), that mapping
     * is removed.  (The map can contain at most one such mapping.)
     *
     *

Returns the value to which this map previously associated the key,
     * or null if the map contained no mapping for the key.
     *
     *

If this map permits null values, then a return value of
     * null does not necessarily indicate that the map
     * contained no mapping for the key; it's also possible that the map
     * explicitly mapped the key to null.
     *
     *

The map will not contain a mapping for the specified key once the
          (一旦方法调用返回,这个map实例将不再包含对这个特定key的映射)
     * call returns.
     *
     * @param key key whose mapping is to be removed from the map
     * @return the previous value associated with key, or
     *         null if there was no mapping for key.
     * @throws UnsupportedOperationException if the remove operation
     *         is not supported by this map
     * @throws ClassCastException if the key is of an inappropriate type for
     *         this map
     * (optional)
     * @throws NullPointerException if the specified key is null and this
     *         map does not permit null keys
     * (optional)
     */
    V remove(Object key);


    // Bulk Operations

    /**
     * Copies all of the mappings from the specified map to this map
       (复制从指定map中复制所有的映射对到当前的map)
     * (optional operation).  The effect of this call is equivalent to that
       该方法调用的效果等同于
     * of calling {@link #put(Object,Object) put(k, v)} on this map once
       (依次挨个针对指定map中的每个键值对基于当前map调用put(k,v)方法)
     * for each mapping from key k to value v in the
     * specified map.  The behavior of this operation is undefined if the
     * specified map is modified while the operation is in progress.
     *
     * @param m mappings to be stored in this map
       (参数说明:要存储到当前map中的所有键值对)
     * @throws UnsupportedOperationException if the putAll operation
     *         is not supported by this map
     * @throws ClassCastException if the class of a key or value in the
     *         specified map prevents it from being stored in this map
     * @throws NullPointerException if the specified map is null, or if
     *         this map does not permit null keys or values, and the
     *         specified map contains null keys or values
     * @throws IllegalArgumentException if some property of a key or value in
     *         the specified map prevents it from being stored in this map
     */
    void putAll(Map m);

    /**
     * Removes all of the mappings from this map (optional operation).
       (从当前map中移除所有的的映射对)
     * The map will be empty after this call returns.
     * 调用返回后,该映射将变成空映射
     * @throws UnsupportedOperationException if the clear operation
     *         is not supported by this map
     */
    void clear();


    // Views(记录概览集组操作)

    /**keySet()
     * Returns a {@link Set} view of the keys contained in this map.
       (返回对包含在该映射Map中的所有键的一个概览)
     * The set is backed by the map, so changes to the map are
       (这个概览组是受当前map映射支持的,所以对当前映射的变化都会反映到)
     * reflected in the set, and vice-versa.  If the map is modified
       (这个键的set中,反之亦然.)
     * while an iteration over the set is in progress (except through
     * the iterator's own remove operation), the results of
     * the iteration are undefined.  The set supports element removal,
     * which removes the corresponding mapping from the map, via the
     * Iterator.remove, Set.remove,
     * removeAll, retainAll, and clear
     * operations.  It does not support the add or addAll
     * operations.
     *
     * @return a set view of the keys contained in this map
     */
    Set keySet();

    /**values();
     * Returns a {@link Collection} view of the values contained in this map.
       (返回当前映射map中所有值的一个概览组集)
     * The collection is backed by the map, so changes to the map are
     * reflected in the collection, and vice-versa.  If the map is
     * modified while an iteration over the collection is in progress
     * (except through the iterator's own remove operation),
     * the results of the iteration are undefined.  The collection
     * supports element removal, which removes the corresponding
     * mapping from the map, via the Iterator.remove,
     * Collection.remove, removeAll,
     * retainAll and clear operations.  It does not
     * support the add or addAll operations.
     *
     * @return a collection view of the values contained in this map
     */
    Collection values();

    /**entrySet();(记录集方法)
     * Returns a {@link Set} view of the mappings contained in this map.
       (返回当前映射map中所有映射对的概览集合)
     * The set is backed by the map, so changes to the map are
     * reflected in the set, and vice-versa.  If the map is modified
     * while an iteration over the set is in progress (except through
     * the iterator's own remove operation, or through the
     * setValue operation on a map entry returned by the
     * iterator) the results of the iteration are undefined.  The set
     * supports element removal, which removes the corresponding
     * mapping from the map, via the Iterator.remove,
     * Set.remove, removeAll, retainAll and
     * clear operations.  It does not support the
     * add or addAll operations.
     *
     * @return a set view of the mappings contained in this map
     */
    Set> entrySet();

    /**
     * A map entry (key-value pair).  The Map.entrySet method returns
     * a collection-view of the map, whose elements are of this class.  The
     * only way to obtain a reference to a map entry is from the
     * iterator of this collection-view.  These Map.Entry objects are
     * valid only for the duration of the iteration; more formally,
     * the behavior of a map entry is undefined if the backing map has been
     * modified after the entry was returned by the iterator, except through
     * the setValue operation on the map entry.
     *
     * @see Map#entrySet()
     * @since 1.2
     */
     //这里又定义一个接口:接口中又定义了很多方法,这里先忽略!
    interface Entry {
        /**
         * Returns the key corresponding to this entry.
         *
         * @return the key corresponding to this entry
         * @throws IllegalStateException implementations may, but are not
         *         required to, throw this exception if the entry has been
         *         removed from the backing map.
         */
        K getKey();

        /**
         * Returns the value corresponding to this entry.  If the mapping
         * has been removed from the backing map (by the iterator's
         * remove operation), the results of this call are undefined.
         *
         * @return the value corresponding to this entry
         * @throws IllegalStateException implementations may, but are not
         *         required to, throw this exception if the entry has been
         *         removed from the backing map.
         */
        V getValue();

        /**
         * Replaces the value corresponding to this entry with the specified
         * value (optional operation).  (Writes through to the map.)  The
         * behavior of this call is undefined if the mapping has already been
         * removed from the map (by the iterator's remove operation).
         *
         * @param value new value to be stored in this entry
         * @return old value corresponding to the entry
         * @throws UnsupportedOperationException if the put operation
         *         is not supported by the backing map
         * @throws ClassCastException if the class of the specified value
         *         prevents it from being stored in the backing map
         * @throws NullPointerException if the backing map does not permit
         *         null values, and the specified value is null
         * @throws IllegalArgumentException if some property of this value
         *         prevents it from being stored in the backing map
         * @throws IllegalStateException implementations may, but are not
         *         required to, throw this exception if the entry has been
         *         removed from the backing map.
         */
        V setValue(V value);

        /**
         * Compares the specified object with this entry for equality.
         * Returns true if the given object is also a map entry and
         * the two entries represent the same mapping.  More formally, two
         * entries e1 and e2 represent the same mapping
         * if


         *     (e1.getKey()==null ?
         *      e2.getKey()==null : e1.getKey().equals(e2.getKey()))  &&
         *     (e1.getValue()==null ?
         *      e2.getValue()==null : e1.getValue().equals(e2.getValue()))
         *

         * This ensures that the equals method works properly across
         * different implementations of the Map.Entry interface.
         *
         * @param o object to be compared for equality with this map entry
         * @return true if the specified object is equal to this map
         *         entry
         */
        boolean equals(Object o);

        /**
         * Returns the hash code value for this map entry.  The hash code
         * of a map entry e is defined to be:


         *     (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
         *     (e.getValue()==null ? 0 : e.getValue().hashCode())
         *

         * This ensures that e1.equals(e2) implies that
         * e1.hashCode()==e2.hashCode() for any two Entries
         * e1 and e2, as required by the general
         * contract of Object.hashCode.
         *
         * @return the hash code value for this map entry
         * @see Object#hashCode()
         * @see Object#equals(Object)
         * @see #equals(Object)
         */
        int hashCode();
    }

    // Comparison and hashing

    /**
     * Compares the specified object with this map for equality.  Returns
     * true if the given object is also a map and the two maps
     * represent the same mappings.  More formally, two maps m1 and
     * m2 represent the same mappings if
     * m1.entrySet().equals(m2.entrySet()).  This ensures that the
     * equals method works properly across different implementations
     * of the Map interface.
     *
     * @param o object to be compared for equality with this map
     * @return true if the specified object is equal to this map
     */
    boolean equals(Object o);

    /**
     * Returns the hash code value for this map.  The hash code of a map is
       (返回当前map的哈希码,当前map的哈希码定义为当前map中的所有映射记录中每条映射记录的hash码值)
     * defined to be the sum of the hash codes of each entry in the map's
     * entrySet() view.  This ensures that m1.equals(m2)
     * implies that m1.hashCode()==m2.hashCode() for any two maps
     * m1 and m2, as required by the general contract of
     * {@link Object#hashCode}.
     *
     * @return the hash code value for this map
     * @see Map.Entry#hashCode()
     * @see Object#equals(Object)
     * @see #equals(Object)
     */
    int hashCode();

}

 

 

 

 

 

看第二部分的实体类:DataEntity实体类

//定义一个抽象类
//抽象是具体的反义
//也就是说抽象不能具体化=不能实例化
//这样的类只能用来继承了
//抽象类中是含有构造方法的!
//注意这里为什么要加上呢?
public abstract class DataEntity extends BaseEntity{
    //private 表示私有,归谁私有呢?就是归类DataEntity私有
    //一旦规定为私有,该变量只能在本类中使用了!
    private static final long serialVersionUID = 1L;
    //protected 表示私有财产保护 私有财产保护什么意思呢?
    //变量归类所有,但类的子类都能使用
    //抽象类中定义的变量一定包含大量的protected类型的变量!

    protected String remarks;//表示注意类似于note(备注)
   
    protected User createBy;//表示创建者

    protected Date createDate;//表示创建日期
 
    protected User updateBy;//表示更新者

    protected String delFlag;//表示删除标志:0表示正常,1表示删除

    protected String aab301;//表示区域

    protected String aaf013;//应该也表示区域

    protected String aaf030;//应该也表示区域
   
    //类的无参构造方法:DataEntity()
    //该方法调用后,会做什么事情呢?
    public DataEntity(){
        //调用该方法后会初始化delFlag属性为0
        //该方法在何时调用?
        //子类调用无参构造方法时,调用该方法
        this.delFlag = "0";
    }
  
    //该类的有参构造方法:DataEntity(String id)
    public DataEntity(String id){
        //该方法调用后,会调用父类中的有参构造方法!
        //那这个方法调用后,会做什么事情呢?还是初始化么?
        //该方法在何时调用?
        //子类的有参构造方法调用时,调用该方法!
        super(id);
    }
 
    //问一个问题:这连个构造方法在何时调用?由谁调用呢?

    //这里定义一个方法getUserArea(T entity)
    //这个方法做什么呢?
    //什么时候调用这个方法?何时调用这个方法呢?在哪里调用这个方法?
    public T getUserArea(T entity){

       //这里出现了一种if结构:这种结构用来处理的逻辑通常是什么?
       //先别管这个,把所有的if代码全忽略
       //只在意最关键的事情:无非就是:初始化类的三个表示区域的属性:aaf030,aaf013,aab301
       //为什么要在这里初始化呢?
       //初始化不是有自己的setter么?
       //何时/在哪里才会用到这个方法呢?
       if(!UserUtils.getUser().isAdmin()){
   
           UserArea userArea = UserUtils.getUserArea();

           if(StringUtils.isNotBlank(userArea.getAaf030())){
               this.aaf030 = userArea.getAaf030();
           }
          
           if(StringUtils.isNotBlank(userArea.getAaf013())){
               this.aaf013 = userArea.getAaf013();
           }
 
           if((StringUtils.isNotBlank(userArea.getAab301()))&&(!"340220".equals(userArea.getAab301()))){
               this.aab301 = userArea.getAab301();
           }            
       }
       return entity;
    
    }
    //这里的getUserArea()方法,

    //这里再定义一个方法preInsert()
    //该方法在何时调用?由谁调用?在哪里调用?
    //何时调用?在执行插入方法之前!
    //由谁调用?实体的实例!
    public void preInsert(){
        //这里先忽略if(非关键代码)
        //很明显这句代码是初始化id
        if(!this.isNewRecord){
            setId(IdGen.uuid());
        }
        //这句代码也忽略掉(非重要代码!)
        User user = UserUtils.getUser();
        //忽略if代码:初始化updateBy和createBy
        if(StringUitls.isNotBlank(user.getId())){
            this.updateBy = user;
            this.createBy = user;
        }
        this.updateDate = new Date();
        this.createDate = this.updateDate;
    }
    //这个方法在很大程度上会觉得暂时还不需要呢,那就先完全彻底的忽略它!!

    //这里定义一个preUpdate()方法
    public void preUpdate(){
        //先忽略这种代码!!
        User user = UserUtils.getUser();
        //也忽略这种代码!!
        //很明显就是初始化update属性
        if(StringUtils.isNotBlank(user.getId())){
            this.update = user;
        }
        //这代码就是初始化updateDate属性
        this.updateDate = new Date();
    }
    //这个方法在何时调用?由谁调用?在哪里调用?
    //似乎在这里完全体现不出来,那就彻底忽略这个方法!!

    .......getter and setter.......

   // 剩下的所有方法就是对上述属性的get和set方法,就不赘述了!

}
总结:
     1.该类和其他类的在结构上的共同点是:属性 + 构造方法 + getter和setter
     2.该类和其他类的在结果上的不同点是:多了三个方法: getUserArea(),preInsert(),preUpdate()
     3.getUserArea()方法不同于其他两种方法的地方是:用到了T
     4.该类定义了七个属性:remarks,createBy,createDate,updateBy,updateDate,aab301,aaf013,aaf030

//任何一个类,都要非常清楚它的属性,这很重要,直接决定你清不清晰这个类

 

 

再来看第三部分的实体:PersonalApplyBean如下
public class PersonalApplyBean extends DataEntity{
    private static final long serialVersionUID = 1L;
 
    private String certType;
    private String idcard;
    private String name;
    private String sex;
    private String nation;
    private String regionalCode;
    private String cardPhoto;
    private String frontPhoto;
    private String backPhoto;
    private String verificatePhoto;
    private String certValidity;
    private String nationality;
    private String bankNo;
    private String bankNum;
    private String ryzt;
    private String aac001;
    private String phone;
    private String address;
    private String aab001;
    private String aab004;
    private String opinions;
    private String rylb;
    private String status;
    private String brithday;
    private String checkDate;

    private String message;

    private String agentidcard;
 
    private String agentname;

    public PersonApplyBean(){
        super();
    }

    public PersonalApplyBean(String id){
        super(id);
    }
    .........剩下的为属性的getter和setter.........         
}
//以上三个实体类的关系:
  PersonalApplyBean继承DataEntity
  DataEntity继承BaseEntity
//不同的是:DataEntity和BaseEntity都是抽象类
//让程序现在人脑中跑一圈,这件事情很有意义!
//PersonalApplyBean是肯定要实例化的
//但是另外两个抽象类是不能实例化的
//假如Bean实例化调用的是无参构造方法
//该无参方法会干两件事情:
  1.创建本类的一个实例
  2.调用父类中的无参构造方法
  父类中的无参构造方法执行后,只做一件事情:将父类的del_flag属性初始化为0
  然后呢?

 

 

 

 

第二天:再读这实体层!!!如下:

 

 

/操作层Bean:PersonalApplyBean(表示个人申请)

public class PersonalApplyBean extends DataEntity{
    private static final long serialVersionUID = 1L;

    private String certType;//证件类型
    private String idcard;//身份证号
    private String name;//姓名
    private String sex;//性别
    private String nation;//民族
    private String regionalCode;//区域
    private String cardPhoto;//证件照
    private String frontPhoto;//正面照
    private String backPhoto;//背面照
    private String verificatePhoto;//验证照
    private String certValidity;//证件有效期
    private String nationality;//国籍
    private String bankNo;//合作银行
    private String bankNum;//合作网点
    private String ryzt;//人员状态
    private String acc001;//参保号
    private String phone;//手机号码
    private String address;//居住地址
    private String aab001;//单位编码
    private String aab004;//单位名称
    private String opinions;//审核意见
    private String rylb;//人员类别
    private String status;//状态
    private String birthday;//出生日期
    private Date checkDate;//审核日期
    
    private String message;

    private String agentidcard;//代理人证件类型
    private String agentname;//代理人证件号码
   
    .....有参和无参构造方法....
    public PersonalApplyBean(){
        super();
    }

    public PersonalApplyBean(String certType){
        super(id);
    }
   
    ......属性的getter和setter.....
    
}

//第一底层的Bean:DataEntity
public abstract class DataEntity extends BaseEntity{
    private static final long serialVersionUID = 1L;

    protected String remarks;//备注
    protected String createBy;//创建者
    protected String createDate;//创建日期
    protected String updateBy;//更新者
    protected String updateDate;//更新日期
    protected String delFlag;//删除标志
    protected String aab301;//区域
    protected String aaf013;//区域
    protected String aaf030;//区域

    .....无参和有参的构造方法....
    public DataEntity(){
        this.delFlag = "0";
    }

    public DataEntity(String id){
        super(id);
    }


    ....所有属性的getter和setter.....
    
   //到现在为止都不需要
   //下面就用到了
   ....定义getUserArea(T entity)方法.....
   public T getUserArea(T entity){

   }

   ....定义preInsert()方法....
   public void preInsert(){

   }
   
   
   ...定义preUpdate()方法....
   public void preUpdate(){

   }

       
}

第二底层的实体类:BaseEntity

public abstract class BaseEntity implements Serializable{
    private static final long serialVersionUID = 1L;
  
    protected String id;
    protected User currentUser;
    protected Page page;
    protected Map sqlMap;
    protected Map> inSqlMap;
    protected boolean isNewRecord = false;
    
    public static final String DEL_FLAG_NORMAL = "0";
    public static final String DEL_FLAG_DELETE = "1";
    public static final String DEL_FLAG_AUDIT = "2";

    ....定义无参和有参构造方法.....
    public BaseEntity(){

    }    

    public BaseEntity(String id){
        this();
        this.id = id;
    }

    .....属性的getter和setter....

    ...定义方法签名:preInsert()
    public abstract void preInsert();

    
    ...定义方法签名:preUpdate()
    public abstract void preUpdate();

   
    ...定义getGlobal()方法...
    public Global getGlobal(){

    }

    ...定义getDbName()方法...
    public String getDbname(){

    }

    ...定义equals()方法...
    public boolean equals(Object obj){

    }
    

    ...定义toString()方法....
    public String toString(){

    }

}


总结:
    注意:如果将所有的实体都整合在一起的话
    会发现,实体中的属性是多于表的字段的,方法也不仅仅限于对属性的get和set
    这些属性分为:表属性,和其他属性
    这些方法分为:表属性方法,其他属性方法和其他方法
    在清楚了表属性和对应的属性方法之后,密切注意其他属性和其他方法;
    所有的实体都由属性和方法组成
    方法一般都是对属性的get和set
    先关注属性:
    1.三个实体的分解实际上是把一张表的所有字段以及需要的其他属性各分担到不同的实体上;
    2.越往底层,相应的实体所具备的属性就越稳定,适用范围就越广;
    再关注方法:
    1.越往底层,定义的方法本身就越稳定!
    
    
注意:
   第二层和第三层也可以合并为一层,但合并为一层之后,这一层的代码量将变成原来的两倍
   阅读起来心烦,修改起来心塞;能分开的最好分开!事实上,这三成都可以合并为一层的,但是你能想象出合并出来的样子么?
   你还能在合并出来的大层中进行行云流水,稳而不乱的操作么?
   肯定要拆成这三块?
   问题是:怎么拆了之后,还能很好的合在一起,并且像合在一起的那样去工作呢?
   这才是应该关注的问题:
   我们一般这么操作:
   1.将比较稳定的部分拆成一个抽象类,剩下的不稳定的部分拆成一个实体层,然后让这个实体层继承这个抽象类
   2.接着,在这些比较稳定的部分中把更稳定的部分再拆成一个抽象类,然后,让上个抽象类再继承当前这个抽象类

   这就形成了你现在看到的这个样子!!!

  

   在任何重复的过程中,第一次永远是最难受的,第二次会舒服很多!
   因为:重复的力量在前期虽然做不到累乘很少但那本身也是一个累加啊
   现在:先关注那些其他方法去!
   在DataEntity中,首先多出来的就是getUserArea(T entity)方法
   对这个方法:至少搞明白的问题有做什么?由谁调用?在哪里调用?在何时调用?
   至于方法的返回值和参数暂时先不考虑!!!!(因为这是个后续问题,不是主要矛盾)
   回答第一个问题:首先可以通过方法名来理解:获取用户区域=换句话说,调用这个方法之后,可以获取到用户的区域!!!
   思考如下问题:最终是谁获取了用户的区域呢? 如何能获取到用户的区域呢? 为什么要获取用户的区域呢?

   在回答这些问题之前:
   先了解两个类:UserArea 和 UserUtils
   先了解一下类UserUtils:
   
   public class UserUtils{
       private static UserDao userDao = (UserDao)SpringContextHolder.getBean(UserDao.class);
       private static RoleDao roleDao = (RoleDao)SpringContextHolder.getBean(RoleDao.class);
       private static MenuDao menuDao = (MenuDao)SpringContextHolder.getBean(MenuDao.class);
       private static AreaDao areaDao = (AreaDao)SpringContextHolder.getBean(AreaDao.class);
       private static UserAreaDao userAreaDao = (UserAreaDao)SpringContextHolder.getBean(UserAreaDao.class);
       private static Af08Dao af08Dao = (Af08Dao)SpringContextHolder.getBean(Af08Dao.class);
       private static Af02Dao af02Dao = (Af02Dao)SpringContextHolder.getBean(Af02Dao.class);
       private static OfficeDao officeDao = (OfficeDao)SpringContextHolder.getBean(OfficeDao.class);


       public static final String USER_CACHE = "userCache";
     
       public static final String USER_CACHE_ID_ = "id_";

       public static final String USER_CACHE_LOGIN_NAME_ = "ln";
  
       public static final String USER_CACHE_LIST_BY_OFFICE_ID ="oid_";

       public static final String USER_AREA_CACHE ="userAreaCache";

       public static final String USER_AREA_CACHE_ID_ = "uaId";

       public static final String AREA_CACHE = "areaCache";

       public static final String AREA_CHACHE_ID = "aId";
   
       public static final String CACHE_ROLE_LIST = "roleList";

       public static final String CACHE_MENU_LIST = "menuList";

       public static final String CACHE_AF08_ALL_LIST = "af08AllList";
     
       public static final String CACHE_AF02_ALL_LIST = "af02AllList";

       public static final String CACHE_OFFICE_LIST = "officeList";
   
       public static final String CACHE_OFFICE_ALL_LIST ="officeAllList";
 
       ......定义无参构造方法....
       public UserUtils(){}
       ....定义get方法......
       //准备获取什么?
       public static User get(String id){


       }       
   
       ....定义getByLoginName(String loginName)方法....
       //通过登录名称获取什么呢?
       public static User getByLoginName(String loginName){

       }

       ....定义无参clearCache()方法...
       public static void clearCache(){

       }

       ...定义有参clearCache(User user)方法...
       public static void clearCache(User user){

       }

   
       ...定义getUser()方法...
       public static User getUser(){

       }

       ...定义getRoleList()方法......
       public static List getRoleList(){
           
       }

       ...定义getMenuList()方法....
       public static List

getMenuList(){

       }

    
       ...定义getUserMenuList(String roleId)方法....
       public static List

getUserMenuList(String roleId){

       }


       ...定义getUserArea()方法...
       public static UserArea getUserArea(){

       }

       ...定义无参getAreaList()方法....
       public static List getAreaList(){

       }

       ...定义getAreaList(String parentId)方法......
       public static List getAreaList(String parentId){

       }

       ...定义getAf08AllList()方法...
       public static List getAf08AllList(){

       }

       ...定义getAf02AllList()....
       public static List getAf02List(){

       }

       ...定义getOfficeList().....
       public static List getOfficeList(){

       }


       ...定义getOfficeAllList()方法....
       public static List getOfficeAllList(){

       }


       ...定义getSubject()方法....
       public static Subject getSubject(){

 
       }

       ...定义getPrincipal()方法....
       public static SystemAuthorizingRealm.Principal getPrincipal(){

       }
       
       ...定义getSession()方法...
       public static Session getSession(){

       }

       ...定义getCache(String key)方法....
       public static Object getCache(String key){

       }

       ...定义getCache(String key,Object defaultValue)....
       public static Object getCache(String key,Object defaultValue){

       }
       ...定义putCache()方法.....
       public static void putCache(String key,Object value){

       }
       ...定义removeCache()方法....
       public static void removeCache(){

       }

}

 

 

 

 

 

 

 

 

你可能感兴趣的:(SSM)