自定义注解实现基本ORM

主要实现思路,自定义注解+反射

package com.zl.annotion;

import jdk.nashorn.internal.runtime.logging.Logger;

import java.lang.annotation.*;
import java.lang.reflect.Field;

/**
 * @author zhanglei
 * @description : 自定义注解实现简单ORM
 * @date 2021/4/9$ 10:48$
 * @return $
 */
public class ORMDemo {

    public static void main(String[] args) throws ClassNotFoundException {
        String table = "";
        Class<?> aClass = Class.forName("com.zl.annotion.Student");
        //获取类上指定注解
        Table declaredAnnotation = aClass.getDeclaredAnnotation(Table.class);
        table = declaredAnnotation.value();

        Field[] declaredFields = aClass.getDeclaredFields();
        StringBuffer sb = new StringBuffer("select ");
        for(Field field : declaredFields){
            //获取属性上指定注解
            Property annotation = field.getAnnotation(Property.class);
            String filed = annotation.filed();
            sb.append(filed + ",");
        }
        String substring = sb.substring(0, sb.length() - 1);

        System.out.println(substring+" from "+ table);
    }

}

@Table("T_student")
@Logger
class Student{

    @Property(filed = "id",length = 18)
    String studentId;

    @Property(filed = "name")
    String studentName;

    @Property(filed = "age")
    int studentAge;

}

/**
 * 只允许注解到类上(type:类,method:方法,filed:属性)
 */
@Target({ElementType.TYPE})
/**
 * 表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效
 */
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
     String value();
}


@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Property{

    String filed();
    int length() default 999999999;
}

运行结果: select id,name,age from T_student

只需要在controller的方法上加上@sign注解,就会走到以下切面,做一个基本的验签功能

2.自定义注解+AOP的使用

package com.zl.projectone.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Sign {


}


package com.zl.projectone.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wuwf on 17/4/27.
 */
@Component
@Aspect
public class SignAspect {

    @Pointcut(value = "@annotation(com.zl.projectone.aop.Sign)")
    public void sign() {

    }

    @Before("sign()")
    public void deBefore(JoinPoint joinPoint) throws Throwable {
        System.out.println("second before");
    }

    @Around("@annotation(sign)")
    public Object around(ProceedingJoinPoint pjp, Sign sign) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        if(sign != null){
            String signKey = request.getParameter("sign");
            if("666".equals(signKey)){
                try {
                    return pjp.proceed();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }else{
                return "验签失败";
            }
        }else{
            try {
                return pjp.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
                return null;
            }
        }
        return null;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值