一、需求
1.有一张用户表,字段包括用户ID,用户名,昵称,年龄,性别,所在城市,邮箱和手机号;
2.方便的对每个字段的或字段的组合条件进行检索,并打印出sql语句。
二、代码
1.用户表的实体类
1 package com.anndemo.entity; 2 3 import com.anndemo.ann.Column; 4 import com.anndemo.ann.Table; 5 6 /** 7 * 用户表的实体类 8 * @author chen 9 * 10 */ 11 @Table("user") 12 public class User { 13 14 @Column("id") 15 private int id; 16 17 @Column("user_name") 18 private String userName; 19 20 @Column("nick_name") 21 private String nickName; 22 23 @Column("age") 24 private int age; 25 26 @Column("city") 27 private String city; 28 29 @Column("email") 30 private String email; 31 32 @Column("mobile") 33 private String mobile; 34 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 public String getUserName() { 42 return userName; 43 } 44 public void setUserName(String userName) { 45 this.userName = userName; 46 } 47 public String getNickName() { 48 return nickName; 49 } 50 public void setNickName(String nickName) { 51 this.nickName = nickName; 52 } 53 public int getAge() { 54 return age; 55 } 56 public void setAge(int age) { 57 this.age = age; 58 } 59 public String getCity() { 60 return city; 61 } 62 public void setCity(String city) { 63 this.city = city; 64 } 65 public String getEmail() { 66 return email; 67 } 68 public void setEmail(String email) { 69 this.email = email; 70 } 71 public String getMobile() { 72 return mobile; 73 } 74 public void setMobile(String mobile) { 75 this.mobile = mobile; 76 } 77 78 }
User.java
2.表注解类
1 package com.anndemo.ann; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 /** 9 * 对应数据表的注解 10 * @author chen 11 * 12 */ 13 @Target({ElementType.TYPE}) 14 @Retention(RetentionPolicy.RUNTIME) 15 public @interface Table { 16 17 String value(); 18 19 }
Table.java
3.字段注解类
1 package com.anndemo.ann; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 /** 9 * 对应数据表字段的注解 10 * @author chen 11 * 12 */ 13 @Target({ElementType.FIELD}) 14 @Retention(RetentionPolicy.RUNTIME) 15 public @interface Column { 16 17 String value(); 18 19 }
Column.java
4.查询实现类
1 package com.anndemo.action; 2 3 import java.lang.reflect.Field; 4 import java.lang.reflect.Method; 5 6 import com.anndemo.ann.Column; 7 import com.anndemo.ann.Table; 8 import com.anndemo.entity.User; 9 10 /** 11 * 查询操作类 12 * @author chen 13 * 14 */ 15 public class QueryAction { 16 17 public static void main(String[] args) { 18 19 User u1 = new User(); 20 u1.setId(10);//查询id为10的用户 21 22 User u2 = new User(); 23 u2.setUserName("Lucy");//模糊查询用户名为Lucy的用户 24 25 User u3 = new User(); 26 u3.setEmail("[email protected], [email protected], [email protected]");//查询邮箱为其中任意一个的用户 27 28 String sql1 = query(u1); 29 String sql2 = query(u2); 30 String sql3 = query(u3); 31 32 System.out.println(sql1); 33 System.out.println(sql2); 34 System.out.println(sql3); 35 36 } 37 38 private static String query(User u) { 39 40 StringBuilder sb = new StringBuilder();//字符串变量(非线程安全) 41 //1.获取class 42 Class<?> c = u.getClass(); 43 //2.获取到table的名字 44 boolean exists = c.isAnnotationPresent(Table.class);//检查User类中是否存在名称为Table的注解 45 if(!exists){ 46 return null; 47 } 48 Table t = c.getAnnotation(Table.class);//获取Table注解 49 String tableName = t.value();//获取注解的属性值,即表名 50 sb.append("select * from ").append(tableName).append(" where 1 = 1");//拼装sql 51 //3.遍历所有字段 52 Field[] arrays = c.getDeclaredFields();//遍历User类中的所有属性 53 for(Field array: arrays){ 54 //4.处理每个字段对应的sql 55 //4.1拿到字段名 56 boolean fExists = array.isAnnotationPresent(Column.class);//判断属性上是否存在名称为Column的注解 57 if(!fExists){ 58 continue; 59 } 60 Column column = array.getAnnotation(Column.class);//获取Column注解 61 String columnValue = column.value();//获取注解的属性值,即表中的字段名 62 //4.2拿到字段值 63 String fieldName = array.getName();//获取属性名 64 String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + 65 fieldName.substring(1);//获取该字段的getter方法名 66 Object fieldValue = null; 67 try { 68 Method getMethod = c.getMethod(getMethodName);//获取该字段的getter方法 69 fieldValue = (Object) getMethod.invoke(u);//通过反射获取getter方法的值 70 } catch (Exception e) { 71 e.printStackTrace(); 72 } 73 //4.3拼装sql 74 if(fieldValue == null || (fieldValue instanceof Integer && (Integer)fieldValue == 0)){ 75 continue; 76 } 77 sb.append(" and ").append(columnValue); 78 if(fieldValue instanceof String){ 79 if(((String) fieldValue).contains(",")){ 80 String []values = ((String) fieldValue).split(","); 81 sb.append(" in("); 82 for(String value: values){ 83 sb.append("‘").append(value).append("‘,"); 84 } 85 sb.deleteCharAt(sb.length() - 1); 86 sb.append(")"); 87 }else{ 88 sb.append(" = ‘").append(fieldValue).append("‘"); 89 } 90 }else if(fieldValue instanceof Integer){ 91 sb.append(" = ").append(fieldValue); 92 } 93 } 94 return sb.toString(); 95 96 } 97 98 }
QueryAction.java
自定义注解基础知识参考:自定义注解(http://www.cnblogs.com/jinjiyese/p/4817645.html)
参考:慕课网(http://www.imooc.com/learn/456)
时间: 2024-10-10 08:44:21