插件结构如图:

注册模块定义了三个:用于实体与表映射的注解,用于属性到表字段的映射,用于映射时过滤掉的注解.

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]

Java注解应用,自定义注解映射实现方案说明.的更多相关文章

  1. Java注解-元数据、注解分类、内置注解和自定义注解|乐字节

    大家好,我是乐字节的小乐,上次说过了Java多态的6大特性|乐字节,接下来我们来看看Java编程里的注解. Java注解有以下几个知识点: 元数据 注解的分类 内置注解 自定义注解 注解处理器 Ser ...

  2. Java中的注解及自定义注解你用的怎么样,能不能像我这样应用自如?

    Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...

  3. Springboot--元注解及自定义注解(表单验证)

    本文简单说明一下元注解,然后对元注解中的@Retention做深入的讨论,在文章最后使用元注解写一个自定义注解来结尾. 一.结论: @Target:注解的作用目标 @Target(ElementTyp ...

  4. java注解和自定义注解的简单使用

    前言 在使用Spring Boot的时候,大量使用注解的语法去替代XML配置文件,十分好用. 然而,在使用注解的时候只知道使用,却不知道原理.直到需要用到自定义注解的时候,才发现对注解原理一无所知,所 ...

  5. Java反射与自定义注解

    反射,在Java常用框架中屡见不鲜.它存在于java.lang.reflact包中,就我的认识,它可以拿到类的字段和方法,及构造方法,还可以生成对象实例等.对深入的机制我暂时还不了解,本篇文章着重在使 ...

  6. java内置注解、元注解和自定义注解

    注解的作用: 1.生成文档 2.跟踪代码依赖性 3.编译时进行格式检查 ---------------------------------------------------------------- ...

  7. Java:深入自定义注解(Annotation)

    在网上找了很多资料也有写的比较好的,但是总有有一点半点的细节没有写出来,在这里自己总结下使用. 使用Java的自定义注解,首先个人需要了解下Java为我们提供的元注解和相关定义注解的语法.(这个我在网 ...

  8. Java注解(自定义注解、view注入)

    注解这东西虽然在jdk1.5就加进来了,但他的存在还是因为使用Afinal框架的view注入才知道的.一直觉得注入特神奇,加了一句就可以把对应view生成了. 下面我们来认识一下注解这个东西 一.注解 ...

  9. Java中的自定义注解

    ## 元注解 要声明一个注解, 我们需要元注解, 元注解是指注解的注解,包括@Retention, @Target, @Document, @Inherited. @Retention 注解的保留位置 ...

随机推荐

  1. WebApi表头设置跨域

    public static class WebApiConfig { public static void Register(HttpConfiguration config) { var setti ...

  2. Rstudio+mysql写入中文表

    1.在mysql中输入sql语句,新建一个支持中文的数据库 create database Chinadaydata DEFAULT CHARACTER SET gbk COLLATE gbk_chi ...

  3. with 重写enter exit 方法

  4. ClassLoader如何加载class?

    ClassLoader一个经常出现又让很多人望而却步的词,本文将试图以最浅显易懂的方式来讲解 ClassLoader,希望能对不了解该机制的朋友起到一点点作用. 要深入了解ClassLoader,首先 ...

  5. 14,EasyNetQ-使用EasyNetQ.Hosepipe重新提交错误消息

    EasyNetQ队列管理实用程序. 用它从队列中抓取消息并重新发布. 还可以用它来检查错误队列消息并重试它们. 1,用法: EasyNetQ.Hosepipe.exe <command> ...

  6. C# 基于 adb 控制安卓

    我司呢,有深信服网关. 我们做项目,日常是比较“清闲”的,所以呢,深信服让你没有办法愉快的使用电脑, 但是手机是可以连外网和外面的 wifi 的,所以我们就玩手机,但是玩手机这么明显会被 Leader ...

  7. android sdk 汉化

    作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com === ===== ==== ==== == ...

  8. json字符串转换对象的方法

    为了方便读者了解json的使用,读者直接粘贴下面代码看效果即可: var json1 = {'name':'小李','age':'11','sex':'女'}; console.log(json1.n ...

  9. linux常用服务程序一键安装

    PHP7安装 rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm service php-fpm stop yum remove php5* ...

  10. .net framework 4.6 asp.net mvc 使用NLog

    NUGET添加NLog和NLog.Config的引用 配置NLog.config <?xml version="1.0" encoding="utf-8" ...