我的java学习之路--Java注解专题
学习网址:http://www.imooc.com/video/8861
2.Java中的常见注解
JDK自带注解:<br>
@Override @Deprecated @Suppvisewarnings
@override 下面的方法肯定覆盖了父类的方法
@Deprecated 下面的方法表示已经过时
@SuppressWarnings("deprecation") 忽视@Deprecation的警告
3.注解的分类
注解的注解就叫做元注解
4.自定义注解
5.解析注解
RetentionPolicy.SOURCE
RetentionPolicy.CLASS
RetentionPolicy.RUNTIME
6.注解实战
package com.myAnnotation;@Table("user")
public class Filter {
@Column("id")
private int id;
@Column("user_name")
private String userName;
@Column("nick_name")
private String nickName;
@Column("age")
private int age;
@Column("city")
private String city;
@Column("email")
private String email;
@Column("mobile")
private String mobile;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String name) {
this.userName = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Override
public String toString() {
return "Filter [> nickName
+ ", age=" + age + ", city=" + city + ", email=" + email
+ ", mobile=" + mobile + "]";
}
}
package com.myAnnotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
package com.myAnnotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String value();
}
package com.myAnnotation;import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
Filter f1 = new Filter();
f1.setId(10);//查询id为10的用户
Filter f2 = new Filter();
f2.setUserName("lucy");//模糊查询用户名为lucy的用户
Filter f3 = new Filter();
//查询邮箱为其中任意一个的
f3.setEmail("liu@sina.com,zh@163.com,2222@qq.com");
String sql1 = query(f1);
String sql2 = query(f2);
String sql3 = query(f3);
System.out.println(sql1);
System.out.println(sql2);
System.out.println(sql3);
}
//查询语句 可以用于各种语句
@SuppressWarnings("unchecked")
private static String query(Filter f){
StringBuilder sb = new StringBuilder();
//1.获取到class
@SuppressWarnings("rawtypes")
Class c = f.getClass();
//2.获取到table的名字
boolean texists = c.isAnnotationPresent(Table.class);
if(!texists){
return null;
}
Table t = (Table)c.getAnnotation(Table.class);
String tableName = t.value();
sb.append("SELECT * FROM ").append(tableName).append(" WHERE 1=1");
//3.遍历所有的字段
Field[] fArray = c.getDeclaredFields();
for(Field field:fArray){
//4.处理每个字段对应的sql
//4.1拿到字段名
boolean cexists = field.isAnnotationPresent(Column.class);
if(!cexists){
continue;
}
Column column = field.getAnnotation(Column.class);
String columnName = column.value();
//4.2拿到字段值
String fieldName = field.getName();
String getMethodName = "get"+ fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
Object fieldValue = null;
try {
Method getMethod = c.getMethod(getMethodName);
fieldValue = getMethod.invoke(f);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//4.3拼装sql
if(fieldValue==null||(fieldValue instanceof Integer)&&((Integer)fieldValue==0)){
continue;
}
sb.append(" AND ").append(columnName);
if(fieldValue instanceof String){
if(((String)fieldValue).contains(",")){
String[] values = ((String)fieldValue).split(",");
sb.append(" in(");
for(String value:values){
sb.append("'").append(value).append("'").append(",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(")");
}else{
sb.append(" = ").append("'").append(fieldValue).append("'");
}
}else if(fieldValue instanceof Integer){
sb.append(" = ").append(fieldValue);
}
}
return sb.toString();
}
}
以上是 我的java学习之路--Java注解专题 的全部内容, 来源链接: utcz.com/z/394028.html