元注解

@Target   用来约束注解可以应用的地方

@Retention   用来约束注解的生命周期

@Documented   可以被javadoc等工具文档化@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)
@Documented public @interface Table { String name() default ""; } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface PrimaryKey { String name() default ""; } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Column { // 列名 默认为"" String name() default ""; // 长度 默认为255 int length() default 255; // 是否为varchar 默认为true boolean varchar() default true; // 是否为空 默认可为空 boolean isNull() default true; } @Table public class Person { @PrimaryKey private int id; @Column(isNull = false, length = 20) private String username; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }

 

注解与反射

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Insert {
    String value();
}

public interface UserMapper {
    @Insert("insert into user (name,pass) values (?,?) ")
    void addUser(String name, String pass);
}

 

// sql执行类
public class DaoOperatorFactory {
    public static Object handle(Method method, Object[] parameters) throws SQLException {
        String sql = null;
        if (method.isAnnotationPresent(Insert.class)) {
            sql = checkSql(method.getAnnotation(Insert.class).value(),
                    Insert.class.getSimpleName());
            insert(sql, parameters);
            return null;
        }
        return null;
    }

    private static void insert(String sql, Object[] parameters) throws SQLException {
        System.out.println("---sql---" + sql);
        for (int i = 0; parameters != null && i < parameters.length; i++) {
            System.out.println("---parameters---" + parameters[i]);
        }
        Connection connection = DBUtil.getConnection();
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
	for (int i = 0; parameters != null && i < parameters.length; i++) {
		prepareStatement.setObject(i + 1, parameters[i]);
	}
	prepareStatement.execute();
	connection.close();
    }

    private static String checkSql(String sql, String type) throws SQLException {
        String type2 = sql.split(" ")[0];
        if (type2 == null || !type2.equalsIgnoreCase(type)) {
            throw new SQLException("Incorrect SQL.");
        }
        return sql;
    }
}

 

public class MethodProxy implements InvocationHandler{
    @Override
    public Object invoke(Object proxy, Method method, Object[] parameters) throws Throwable {
        return DaoOperatorFactory.handle(method, parameters);
    }
}
public class MethodProxyFactory {
    public static <T> T getBean(Class<T> clazz) {
        final MethodProxy methodProxy = new MethodProxy();
        return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{clazz},
                methodProxy);
    }
}

 

通过反射获取注解信息,达到代理的目的

 

posted on 2019-09-04 21:58  MC伍  阅读(185)  评论(0编辑  收藏  举报