代码有四个组成,分别为:main、userEntity、SetTable、SetPropertity
package com.wang.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
*
* @methodDesc: 功能描述:(使用java自定义注解,模拟ORM框架注解)
* @author: 王帅发
* @param: @return
* @createTime:2020年2月27日
* @returnType:@return String
*
*/
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
//反射Class
Class<?> forName = Class.forName("com.wang.annotation.UserEntity");
//getAnnotations当前类使用哪些注解
/* Annotation[] annotations = forName.getAnnotations();
for ( Annotation annotation:annotations) {
System.out.println(annotation);
}*/
//getAnnotation 当前类使用的某个注解对象
/*SetPropertity forNameAnnotation = forName.getAnnotation(SetPropertity.class);
System.out.println(forNameAnnotation);*/
//获取表名称注解
Field[] declaredFields = forName.getDeclaredFields();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("select ");
for (int i = 0; i < declaredFields.length; i++) {
// declaredFields[i].setAccessible(true);
SetPropertity setPropertity = declaredFields[i].getAnnotation(SetPropertity.class);
// String id = setPropertity.id();
String name = setPropertity.name();
stringBuffer.append(name);
if (i == declaredFields.length-1) {
stringBuffer.append(" from");
}
else{
stringBuffer.append(",");
}
}
//获取表名
SetTable setTable = forName.getAnnotation(SetTable.class);
String tablName = setTable.value();
stringBuffer.append(" "+tablName);
System.out.println(stringBuffer.toString());
}
}
package com.wang.annotation;
import lombok.Data;
/**
*
* @methodDesc: 功能描述:(用户实体类)
* @author: 王帅发
* @param: @return
* @createTime:2020年2月27日
* @returnType:@return String
*
*/
@SetTable(value = "user_table")
public class UserEntity {
@SetPropertity(name="user_name",len=12)
private String name;
@SetPropertity(name="user_age",len=12)
private int age;
}
package com.wang.annotation;
import javax.xml.bind.Element;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @methodDesc: 功能描述:(自定义表映射注解)
* @author: 王帅发
* @param: @return
* @createTime:2020年2月27日
* @returnType:@return String
*
*/
@Target(value = ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SetTable {
/**
*
* @methodDesc: 功能描述:(对应数据库表名称)
* @author: 王帅发
* @param: @return
* @createTime:2020年2月27日
* @returnType:@return String
*
*/
String value();
}
package com.wang.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @methodDesc: 功能描述:(自定义字段属性)
* @author: 王帅发
* @param: @return
* @createTime:2020年2月27日
* @returnType:@return String
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface SetPropertity {
String name();
int len();
}