利用Java注解实现一个简单的SQL语句生成
User类:对应数据库user表
package cn.guyouda.annotation;
@Table("user")
public class User {
@Column("name")
private String name;
@Column("password")
private String password;
@Column("age")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
注解Table:
package cn.guyouda.annotation;
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();
}
注解Column
package cn.guyouda.annotation;
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();
}
SQLTest测试类:
package cn.guyouda.annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; public class SQLTest { public static void main(String[] args) { User user1 = new User(); user1.setName("AAA"); User user2 = new User(); user2.setName("BBB"); user2.setPassword("bbb"); User user3 = new User(); user3.setName("CCC"); user3.setPassword("ccc"); user3.setAge(21); String sql1 = createSQL(user1); String sql2 = createSQL(user2); String sql3 = createSQL(user3); System.out.println(sql1); System.out.println(sql2); System.out.println(sql3); } /** * 解析注解并生成SQL语句 * @param user * @return */ private static String createSQL(User user){ StringBuffer sb = new StringBuffer(); Class c = user.getClass(); /** * 判断是否有类注解 */ boolean isExist = c.isAnnotationPresent(Table.class); if(!isExist){ return null; } /** * 获取类注解内容 * 即获取表名 */ Table t = (Table)c.getAnnotation(Table.class); String tableName = t.value(); sb.append("select * from ").append(tableName).append(" where 1=1"); //获取参数注解 Field[] field = c.getDeclaredFields(); for(Field f : field){ boolean fieldExist = f.isAnnotationPresent(Column.class); //只针对自己定义的注解进行处理 if(!fieldExist){ continue; } //获取注解值和参数名 Column col = f.getAnnotation(Column.class); String colName = col.value(); String fieldName = f.getName(); //获取方法名,利用反射方式调用方法 String methodName = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); Object fieldValue = null; try { Method method = c.getMethod(methodName); fieldValue = method.invoke(user); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //过滤不符合要求条件 if(fieldValue==null || (fieldValue instanceof Integer)&&(Integer)fieldValue==0){ continue; } //条件拼接 sb.append(" and ").append(colName); if(fieldValue instanceof String){ sb.append("='").append(fieldValue).append("'"); }else if(fieldValue instanceof Integer){ sb.append("=").append(fieldValue); } } return sb.toString(); } }
结果: