1 @Target({ElementType.TYPE}) 2 @Retention(RetentionPolicy.RUNTIME) 3 public @interface Table { 4 String value(); 5 }
1 @Target({ElementType.FIELD}) 2 @Retention(RetentionPolicy.RUNTIME) 3 public @interface Column { 4 String value(); 5 }
1 @Table("user") 2 public class Filter { 3 @Column("id") 4 private int id; 5 @Column("username") 6 private String userName; 7 @Column("nickname") 8 private String nickName; 9 @Column("age") 10 private int age; 11 @Column("city") 12 private String city; 13 @Column("email") 14 private String email; 15 @Column("mobile") 16 private String mobile; 17 public int getId() { 18 return id; 19 } 20 public void setId(int id) { 21 this.id = id; 22 } 23 public String getUserName() { 24 return userName; 25 } 26 public void setUserName(String userName) { 27 this.userName = userName; 28 } 29 public String getNickName() { 30 return nickName; 31 } 32 public void setNickName(String nickName) { 33 this.nickName = nickName; 34 } 35 public int getAge() { 36 return age; 37 } 38 public void setAge(int age) { 39 this.age = age; 40 } 41 public String getCity() { 42 return city; 43 } 44 public void setCity(String city) { 45 this.city = city; 46 } 47 public String getEmail() { 48 return email; 49 } 50 public void setEmail(String email) { 51 this.email = email; 52 } 53 public String getMobile() { 54 return mobile; 55 } 56 public void setMobile(String mobile) { 57 this.mobile = mobile; 58 } 59 }
1 @Table("deapartment") 2 public class Filter2 { 3 @Column("id") 4 private int id; 5 @Column("name") 6 private String name; 7 @Column("amount") 8 private String amount; 9 @Column("age") 10 private int age; 11 @Column("city") 12 private String city; 13 public int getId() { 14 return id; 15 } 16 public void setId(int id) { 17 this.id = id; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public String getAmount() { 26 return amount; 27 } 28 public void setAmount(String amount) { 29 this.amount = amount; 30 } 31 public int getAge() { 32 return age; 33 } 34 public void setAge(int age) { 35 this.age = age; 36 } 37 public String getCity() { 38 return city; 39 } 40 public void setCity(String city) { 41 this.city = city; 42 } 43 44 }
1 public class Test { 2 public static void main(String[] args) { 3 Filter f1 = new Filter(); 4 f1.setId(10); 5 Filter f2 = new Filter(); 6 f2.setAge(45); 7 f2.setCity("china"); 8 f2.setUserName("lucy"); 9 Filter f3 = new Filter(); 10 f3.setEmail("liu@sh.com,dahod@1223.com"); 11 String sql1 = query(f1); 12 String sql2 = query(f2); 13 String sql3 = query(f3); 14 System.out.println(sql1); 15 System.out.println(sql2); 16 System.out.println(sql3); 17 18 Filter2 filter2 = new Filter2(); 19 filter2.setId(02); 20 filter2.setAge(15); 21 filter2.setAmount("14"); 22 filter2.setName("小明"); 23 System.out.println(query(filter2)); 24 } 25 @SuppressWarnings("unused") 26 private static String query(Object f){ 27 StringBuffer sb = new StringBuffer(); 28 //1.获取到class 29 Class c = f.getClass(); 30 //2.获取到table的名字 31 boolean exsits = c.isAnnotationPresent(Table.class); 32 if(!exsits){ 33 return null; 34 } 35 Table t = (Table) c.getAnnotation(Table.class); 36 String tableName = t.value(); 37 sb.append("select *from ").append(tableName).append(" where 1=1"); 38 //3.变量所有的字段 39 Field[] farray = c.getDeclaredFields(); 40 for(Field field :farray){ 41 //4.0处理每个字段对应的名字 42 //4.1拿到字段名 43 boolean fExist = field.isAnnotationPresent(Column.class); 44 if(!fExist){ 45 continue; 46 } 47 Column column = field.getAnnotation(Column.class); 48 String coiumnName = column.value(); 49 //4.2拿到字段的值 50 String filedName = field.getName(); 51 String getMethodName = "get"+filedName.substring(0,1).toUpperCase()+filedName.substring(1); 52 Object fieldValue = null; 53 try { 54 Method getMethod = c.getMethod(getMethodName); 55 fieldValue =getMethod.invoke(f); 56 } catch (Exception e) { 57 // TODO Auto-generated catch block 58 e.printStackTrace(); 59 } 60 //4.3拼装sql 61 if(fieldValue ==null|| 62 (fieldValue instanceof Integer &&(Integer)fieldValue==0)){ 63 continue; 64 } 65 sb.append(" and ").append(filedName); 66 if(fieldValue instanceof String){ 67 if(((String)fieldValue).contains(",")){ 68 String [] values = ((String)fieldValue).split(","); 69 sb.append(" in("); 70 for(String v:values){ 71 sb.append("'").append(v).append("'").append(","); 72 } 73 sb.deleteCharAt(sb.length()-1); 74 sb.append(")"); 75 }else{ 76 sb.append("=").append("'").append(fieldValue).append("'"); 77 } 78 }else if(fieldValue instanceof Integer){ 79 sb.append("=").append(fieldValue); 80 } 81 //sb.append(" and ").append(filedName).append("=").append(fieldValue); 82 } 83 return sb.toString(); 84 85 86 } 87 }
1 select *from user where 1=1 and id=10 2 select *from user where 1=1 and userName='lucy' and age=45 and city='china' 3 select *from user where 1=1 and email in('liu@sh.com','dahod@1223.com') 4 select *from deapartment where 1=1 and id=2 and name='小明' and amount='14' and age=15