Java注解的使用
0)注解可修饰类的所有成员
1)内置注解
JDK所提供的注解,如下一些常用注解的使用:
package com.annotation; /** * @author: 肖德子裕 * @date: 2018/8/28 23:17 * @description: 注解的使用(注解可修饰类的所有成员) * 选中注解名:ctrl+鼠标左键可以查看源代码 * 在源代码中@Target表示该注解使用的范围 * 内置注解:JDK所提供的注解 */
public class Demo1 { @Override public String toString() { return ""; } @Deprecated public static void test() { //这个注解表示这个方法不建议使用,在调用时可以看见去除线
} //@SuppressWarnings("all") //@SuppressWarnings("unchecked")
@SuppressWarnings(value = {"unchecked", "deprecation"}) public static void test1() { //这个注解表示去除这个方法的所有警告信息,有警告的话会有感叹号 //第二个表示去除相应的警告信息 //第三个表示去除相应的多个警告
} public static void main(String[] args) { } }
2)自定义注解(关键字@interface)
package com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author: 肖德子裕 * @date: 2018/8/28 23:46 * @description: 自定义注解(关键字@interface) * 元注解:注解其他注解 * 光定义注解没什么用,还要通过反射解析注解才有意义 * ORM:对象关系映射 */ @Target(value = ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SxtAnnotation { //上面第一个元注解表示这个注解只能用于方法上 //第二个一般用于反射,表示该注解在运行时有效 //studentName()代表参数,如果注解处没有该参数会报错,所以一般会设置一个默认值 //一般等于-1表示不存在
String studentName() default ""; int age() default 0; }
package com.annotation; /** * @author: 肖德子裕 * @date: 2018/8/28 23:51 * @description: */
public class Demo2 { @SxtAnnotation(studentName = "xdzy") public static void test() { } }
3)注解的应用
使用自定义注解表示类与表的关系
package com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author: 肖德子裕 * @date: 2018/8/29 07:36 * @description: 使用自定义注解表示类与表的关系 */ @Target(value = ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface SxtTable { //如果只有一个参数一般取名叫value
String value(); }
对属性进行相关注解
package com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author: 肖德子裕 * @date: 2018/8/29 07:40 * @description: 对属性进行相关注解 */ @Target(value = ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface SxtField { String columnName(); String type(); int length(); }
在实体类里面的注解
实体类例子
package com.annotation; /** * @author: 肖德子裕 * @date: 2018/8/29 07:34 * @description: 使用自定义注解表示类与表的关系 */ @SxtTable("tb_student") public class SexStudent { @SxtField(columnName = "sid", type = "int", length = 10) private int id; @SxtField(columnName = "sname", type = "varchar", length = 10) private String name; @SxtField(columnName = "sage", type = "int", length = 3) private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
4)使用反射读取注解信息,模拟处理注解信息的流程
package com.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.Field; /** * @author: 肖德子裕 * @date: 2018/8/29 07:50 * @description: 使用反射读取注解信息,模拟处理注解信息的流程 */
public class Demo3 { public static void main(String[] args) { try { Class clazz = Class.forName("com.annotation.SexStudent"); //获取类的所有有效注解
Annotation[] annotations = clazz.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(annotation); } //获取指定的注解
SxtTable st = (SxtTable) clazz.getAnnotation(SxtTable.class); System.out.println(st.value()); //获取属性
Field field = clazz.getDeclaredField("studentName"); //获取指定属性的注解
SxtField sxtField = field.getAnnotation(SxtField.class); System.out.println(sxtField.columnName() + sxtField.type() + sxtField.length()); //在上面获取了注解的一些信息,就可以拼接sql语句,执行数据库操作
} catch (Exception e) { e.printStackTrace(); } } }