@Retention(RetentionPolicy.RUNTIME)
public @interface SqlField {
KeyType type() default KeyType.NOT_KEY;
enum KeyType { NOT_KEY, PRIMARY_KEY, AUTO_INCREMENT_KEY }
}
public class Student {
@SqlField(type = KeyType.AUTO_INCREMENT_KEY) public int id;
@SqlField public float score;
@SqlField public int sex;
@SqlField public String num;
}
type 屬性進一步指定主鍵策略。Field[] fields = clazz.getDeclaredFields()。field.isAnnotationPresent(SqlField.class)。SqlField ann = field.getAnnotation(SqlField.class); KeyType kt = ann.type();。INSERT/UPDATE/SELECT SQL,主鍵字段自動生成或回填。columnName(), nullable(), length() 等屬性支援更精細映射。Hibernate/JPA 使用 @Entity, @Id, @Column, @GeneratedValue;MyBatis Plus 亦支援註解模式。自定義註解適合輕量級或教學專案,生產環境建議使用成熟 ORM。