java注解

来源:互联网 发布:国内域名需要备案吗 编辑:程序博客网 时间:2024/06/07 09:38

1.jdk

@override覆盖

@Deprecated已过时

@Suppvisewarnings("")忽略警告

2.spring

@Autowired自动注入

@Service

等等

3.自定义注解

Name自定义注解

package test;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface Name {//String value();//只有一个参数是必须写成value这个函数名String a();int b() default 20;}

解析注解

package test;@Name( a="hello",b=30)public class Test {@Name(a="hi")public void test(){}}
package test;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;public class Test1 {//解析注解方法public static void main(String[] args) {try {Class c=Class.forName("test.Test");boolean b=c.isAnnotationPresent(Name.class);if(b){Name n=(Name) c.getAnnotation(Name.class);String s=n.a();int t=n.b();System.out.println(s+" "+t);}//找到所有方法Method[] methods=c.getMethods();for(Method m:methods){boolean result=m.isAnnotationPresent(Name.class);if(result){Name name=m.getAnnotation(Name.class);String s=name.a();int t=name.b();System.out.println(s+" "+t);}}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

实现简单注解拼装sql语句

User类

package test;@Table("User")public class User {@Column("age")private int age;@Column("name")private String name;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

注解类

package test;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface Table {String value();}

package test;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface Column {String value();}

根据注解,反射拼装sql语句

package test;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;public class Test1 {//解析注解方法/*public static void main(String[] args) {try {Class c=Class.forName("test.Test");boolean b=c.isAnnotationPresent(Name.class);if(b){Name n=(Name) c.getAnnotation(Name.class);String s=n.a();int t=n.b();System.out.println(s+" "+t);}//找到所有方法Method[] methods=c.getMethods();for(Method m:methods){boolean result=m.isAnnotationPresent(Name.class);if(result){Name name=m.getAnnotation(Name.class);String s=name.a();int t=name.b();System.out.println(s+" "+t);}}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}*///实现简易的拼接sqlpublic static void main(String[] args) {User u1=new User();u1.setAge(18);User u2=new User();u2.setAge(22);u2.setName("Mike");String s1=query(u1);String s2=query(u2);System.out.println(s1);System.out.println(s2);}private static String query(User u1) {StringBuilder sb=new StringBuilder();Class c=u1.getClass();if(c.isAnnotationPresent(Table.class)){Table t=(Table) c.getAnnotation(Table.class);String tableName=t.value();sb.append("select * from").append(tableName).append(" where 1=1");}else{return null;}Field[] fields=c.getDeclaredFields();for(Field f:fields){if(f.isAnnotationPresent(Column.class)){Column column=f.getAnnotation(Column.class);String columnName=column.value();String getMethodName="get"+columnName.substring(0, 1).toUpperCase()+columnName.substring(1);//System.out.println(getMethodName);Object fieldValue=null;try {Method method=c.getMethod(getMethodName);//invoke里面参数是要实例化的参数,返回该方法的结果fieldValue= method.invoke(u1);if(fieldValue==null||(fieldValue instanceof Integer &&(Integer) fieldValue==0) ){continue;}else if(fieldValue instanceof String){sb.append(" and ").append(columnName).append("=").append("'").append(fieldValue).append("'");}else if(fieldValue instanceof Integer){sb.append(" and ").append(columnName).append("=").append(fieldValue);}} catch (Exception e) {e.printStackTrace();} }}return sb.toString();}}




原创粉丝点击