Java注解應用,自定義注解映射實現方案說明.


插件結構如圖:

注冊模塊定義了三個:用於實體與表映射的注解,用於屬性到表字段的映射,用於映射時過濾掉的注解.

1.用於實體與表映射的注解

package com.dobby.plugins.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * User: 蘇若年 * Date: 14-10-9 * Time: 下午21:12 * Description: */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface GeneratorTable { //標注映射時的表名
    public String name() default ""; }

2.用於屬性與字段映射的注解

package com.dobby.plugins.annotation; import java.lang.annotation.*; /** * User: 蘇若年 * Date: 14-10-9 * Time: 下午21:31 * Description: */ @Target({ElementType.METHOD,ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface GeneratorField { /** * 是否為主鍵 * @return
     */
    boolean primaryKey() default false; /** * 映射的字段名 * @return
     */
    public String name() default ""; }

3.用於廢棄字段過濾的注解

package com.dobby.plugins.annotation; import java.lang.annotation.*; /** * User: 蘇若年 * Date: 14-10-9 * Time: 下午21:15 * Description: */ @Target({ElementType.METHOD,ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface AbolishedField { //標注映射時排除的字段
}

注解定義完成后,我們需要在掃描到的實體中根據注解映射規則進行掃描時自動封裝

自動封裝的核心業務邏輯如下

 /** * 根據對象構造,表映射 * @param object * 實體對象 * @return * 實體對象到字段表的映射,基於注解處理 */
    public static EntityTable constructEntityTableWithObject(Object object){ EntityTable entityTable = null; try{ if(null == object){return null;} ConcurrentMap<String,String> entityToTable = new ConcurrentHashMap<String, String>(); boolean isGeneratorTable = object.getClass().isAnnotationPresent(GeneratorTable.class); System.out.println(object.getClass().getSimpleName()); if(isGeneratorTable){ GeneratorTable generatorTable = object.getClass().getAnnotation(GeneratorTable.class); if(StringUtils.isBlank(generatorTable.name())){ entityToTable.put(object.getClass().getSimpleName(),firstLower(object.getClass().getSimpleName())); }else{ entityToTable.put(object.getClass().getSimpleName(),generatorTable.name()); } }else{ entityToTable.put(object.getClass().getSimpleName(),firstLower(object.getClass().getSimpleName())); } Field[] fields = object.getClass().getDeclaredFields(); if(null != fields){ entityTable = new EntityTable(); //主鍵組
                ConcurrentMap<String,String> propertyToKey = new ConcurrentHashMap<String,String>(); //字段組
                ConcurrentMap<String,String> propertyToColumn = new ConcurrentHashMap<String, String>(); //主鍵到集合類型映射
                ConcurrentMap<String,Object> propertyPKeyType = new ConcurrentHashMap<String,Object>(); for (int i = 0; i < fields.length; i++) { //判斷是否剔除
                    boolean isAbolishedField = fields[i].isAnnotationPresent(AbolishedField.class); if(isAbolishedField){ //跳過該字段的提取
                        continue; } //判斷是否是主鍵
                    boolean isGeneratorField = fields[i].isAnnotationPresent(GeneratorField.class); if(isGeneratorField){ // 取注解中的文字說明
                        GeneratorField generatorField =  fields[i].getAnnotation(GeneratorField.class); boolean primaryKey = generatorField.primaryKey(); if(primaryKey){ //添加到主鍵集合
 propertyPKeyType.put(fields[i].getName(),fields[i].getType().getName()); propertyToKey.put(fields[i].getName(),generatorField.name()); }else{ if(StringUtils.isBlank(generatorField.name())){ propertyToColumn.put(fields[i].getName(),fields[i].getName()); }else{ propertyToColumn.put(fields[i].getName(),generatorField.name()); } } continue; } propertyToColumn.put(fields[i].getName(), fields[i].getName()); } entityTable.setPropertyPKeyType(propertyPKeyType); entityTable.setPropertyToPKey(propertyToKey); entityTable.setPropertyToColumn(propertyToColumn); } entityTable.setEntityToTable(entityToTable); }catch (Exception e){ e.printStackTrace(); } return entityTable; }
EntityTable時實體與表結構映射的一個簡易對象.

如下,我們定義的實體

package com.dobby.code.make.model; import com.dobby.plugins.annotation.AbolishedField; import com.dobby.plugins.annotation.GeneratorField; import com.dobby.plugins.annotation.GeneratorTable; import java.io.Serializable; /** * Created by 蘇若年 on 2014/11/26. */
//映射表別名
@GeneratorTable(name = "tb_member") public class Member implements Serializable { //映射該字段,並且為主鍵,自定義字段別名
    @GeneratorField(primaryKey = true, name = "m_Id") private Integer id; @GeneratorField(name = "member_name") private String memberName; //映射時不映射該字段
 @AbolishedField private String address; //不使用注解,默認為使用屬性名進行映射
    private String zipCode; //getter and setter
 }

 查看實體映射過程

 EntityTable entityTable = BeanUtils.constructEntityTableWithPath("com.dobby.code.make.model.Member"); System.out.println("主鍵映射" + entityTable.getPropertyToPKey()); System.out.println("字段映射" + entityTable.getPropertyToColumn()); System.out.println("主鍵集合" + entityTable.getPropertyPKeyType()); System.out.println("表名映射" + entityTable.getEntityToTable());

通過注解映射后的結果如下:

主鍵映射{id=m_Id}
字段映射{zipCode=zipCode, memberName=member_name}
主鍵集合{id=java.lang.Integer}
表名映射{Member=tb_member}

因為使用了注解映射過濾,所以address字段映射時被排除.

轉載請注明出處:[http://www.cnblogs.com/dennisit/p/4125103.html]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM