目录
注解说明
元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:
@Target
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
- CONSTRUCTOR:用于描述构造器
- FIELD:用于描述域-全局变量
- LOCAL_VARIABLE:用于描述局部变量
- METHOD:用于描述方法
- PACKAGE:用于描述包
- PARAMETER:用于描述参数
- TYPE:用于描述类、接口(包括注解类型) 或enum声明
表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)
3.@Documented
4.@Inherited
代码
实现简单orm框架
定义两个注解
import java.lang.annotation.*;
/**
* 映射表名
*/
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TableField {
String value();
String desc() default "";
}
import java.lang.annotation.*;
/**
* 映射表字段
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TableName {
String value();
}
实体类
import lombok.Data;
@Data
@TableName("user") // @TableName表名
public class UserEntity {
@TableField("id") // @TableField对应的表字段
public int id;
@TableField("user_name")
private String userName;
@TableField("pass_word")
private String passWord;
@TableField("age")
private int age;
}
import cn.hutool.core.util.StrUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class TestTwoController {
public static void main(String[] args) {
// select 查询
UserEntity userEntity = new UserEntity();
userEntity.setId(10);
userEntity.setUserName("栗子");
userEntity.setPassWord("123456");
userEntity.setAge(22);
getSql(userEntity);
// select in多个查询
UserEntity userEntityTwo = new UserEntity();
userEntityTwo.setUserName("栗子, 大栗子, 小栗子");
userEntityTwo.setAge(20);
getSql(userEntityTwo);
}
public static void getSql(UserEntity userEntity) {
// 通过反射获取对象
Class userEntityClass = userEntity.getClass();
// 获取类上的TableName注解
TableName TableName = (TableName) userEntityClass.getAnnotation(TableName.class);
// sb封装select内容
StringBuffer sb = new StringBuffer();
sb.append("select ");
// where封装查询条件
StringBuffer where = new StringBuffer();
// 获取所有字段
Field[] declaredFields = userEntityClass.getDeclaredFields();
int a = declaredFields.length;
for (Field field : declaredFields){
a--;
// 获取字段上TableField注解内容
TableField annotation = field.getAnnotation(TableField.class);
String tableName = annotation.value();
// 拼接select字段
if(a == 0){
sb.append(tableName);
}else{
sb.append(tableName+", ");
}
// 字段名
String fieldName = field.getName();
if(StrUtil.isNotBlank(fieldName)){
// 获取属性值
Object valueByName = getFieldValueByName(fieldName, userEntity);
// 类型判断,不为空
if((valueByName instanceof String && valueByName != null) || (valueByName instanceof Integer && ((Integer) valueByName).intValue() != 0 )){
// 拼接 and 字段名
where.append(" and ").append(tableName);
// 判断是in多个条件查询还是普通查询
if(valueByName instanceof String && ((String) valueByName).contains(",")){
// 拼接in多个条件
String[] split = ((String) valueByName).split(",");
where.append(" in (");
for(int s = 0; s < split.length; s++){
if(s < split.length - 1){
where.append(split[s]).append(", ");
}else{
where.append(split[s]).append(")");
}
}
}else{// 拼接查询条件
where.append(" = ").append(valueByName);
}
}
}
}
// 拼接select和where
sb.append(" from ").append(TableName.value()).append(" where 1 = 1");
sb.append(where).append(";");
System.out.println(sb.toString());
}
/**
* 获取属性值
* - 反射获取getter属性值
*
* @param name 属性名
* @param user 属性名所在的entity类
* @return 属性值
*/
public static Object getFieldValueByName(String name, Object user) {
// 第一个字符改大写
String firstLetter = name.substring(0, 1).toUpperCase();
// 拼装成getter方法名称
String getter = "get" + firstLetter + name.substring(1);
Method method;
Object value;
try {
// 反射获值
method = user.getClass().getMethod(getter, new Class[] {});
value = method.invoke(user, new Class[] {});
return value;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}