查询类及其继承类和实现接口注解中对应的实体类

  1. 项目中使用到需要查询注解对应的实体类的位置做个记录
    1. 使用递归查询该类及其父类和接口是否有对应注解

  2. AnnotationUtils 是spring提供的:比较懒就用了
    1. org.springframework.core.annotation.AnnotationUtils

    2. isAnnotationDeclaredLocally: 是在本地声明的注释

      1. 人话:这个类有指定注解

  3. 本人是个菜鸡有什么不对的地方欢迎大佬指出
    /**
     * use recursion find the class that contains annotationType in all classes
     *
     * @param annotationType annotationType
     * @param clazz clazz
     * @param classes classes
     */
    public void findAnnotationDeclaring(Class<? extends Annotation> annotationType, Class<?> clazz,
                                        List<Class<?>> classes) {
        Assert.notNull(annotationType, "Annotation type must not be null");
        if (clazz == null || Object.class == clazz) {
            return;
        }
        
        if (AnnotationUtils.isAnnotationDeclaredLocally(annotationType, clazz)) {
            classes.add(clazz);
        }
        
        //Superclass
        if (!Object.class.equals(clazz.getSuperclass())) {
            findAnnotationDeclaring(annotationType, clazz.getSuperclass(), classes);
        }
        
        //Interface
        if (ArrayUtils.isNotEmpty(clazz.getInterfaces())) {
            for (Class<?> clazzInterface : clazz.getInterfaces()) {
                findAnnotationDeclaring(annotationType, clazzInterface, classes);
            }
        }
    }
好的,让我来给您展示一下代码。 首先,创建 Contact 实体类,代码如下: ```java @Entity(tableName = "contact") public class Contact extends OrmObject{ @PrimaryKey(autoGenerate = true) private int id; // 主键 private String name; // 姓名 private String phone; // 电话 public Contact() { } public Contact(String name, String phone) { this.name = name; this.phone = phone; } // getter 和 setter 方法 // ... } ``` 接着,创建 ContactStore 数据库对应,代码如下: ```java @Database(version = 1, entities = {Contact.class}) public class ContactStore extends OrmDatabase { } ``` 最后,创建 DataContactUtil 数据库工具,代码如下: ```java public class DataContactUtil { private static ContactStore contactStore; public static ContactStore getContactStore(Context context) { if (contactStore == null) { contactStore = Orm.databaseBuilder(context, ContactStore.class).build(); } return contactStore; } // 添加联系人 public static void addContact(Context context, Contact contact) { getContactStore(context).getContactDao().insert(contact); } // 删除联系人 public static void deleteContact(Context context, Contact contact) { getContactStore(context).getContactDao().delete(contact); } // 查询所有联系人 public static List<Contact> getAllContacts(Context context) { return getContactStore(context).getContactDao().findAll(); } // 查询指定联系人 public static Contact getContact(Context context, int id) { return getContactStore(context).getContactDao().findById(id); } } ``` 以上就是联系人数据库的实现,其 OrmObject、OrmDatabase、Orm 是一个开源 ORM 框架,可以通过 Maven 或 Gradle 进行引入。您可以根据自己的需要进行相应的修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值