直接上代码
package com.guoxinet.o2o.annotation; 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 @Documented public @interface Column { String value(); }
package com.guoxinet.o2o.annotation; 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 @Documented public @interface Table { String value(); }
package com.guoxinet.o2o.annotation; @Table("tb_user") public class User { @Column("user_id") private int userId; @Column("user_name") private String userName; @Column("address") private String address; @Column("email") private String email; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
package com.guoxinet.o2o.annotation; @Table("tb_department") public class DepartMent { @Column("department_id") private int departmentId; @Column("name") private String name; public int getDepartmentId() { return departmentId; } public void setDepartmentId(int departmentId) { this.departmentId = departmentId; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.guoxinet.o2o.annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Client { public static void main(String[] args) { User user = new User(); user.setUserId(1); user.setEmail("3964412@qq.com,yafeng@yahu.com,lifeng@guoxinet.com"); getSql(user); DepartMent dm = new DepartMent(); dm.setDepartmentId(100); dm.setName("技术部"); getSql(dm); } public static String getSql(Object ob){ StringBuilder sb = new StringBuilder(); //获取class @SuppressWarnings("rawtypes") Class c = ob.getClass(); //获取table的名字 @SuppressWarnings("unchecked") boolean isExist = c.isAnnotationPresent(Table.class); if(!isExist){ return ""; } Table table = (Table)c.getAnnotation(Table.class); String tableName = table.value(); sb.append("select * from ").append(tableName).append(" where 1=1 "); //遍历属性 Field[] fArray =c.getDeclaredFields(); for(Field field: fArray){ boolean fisExist = field.isAnnotationPresent(Column.class); if(!fisExist){ continue; } Column column = (Column)field.getAnnotation(Column.class); //列名 String columnName = column.value(); //得到属性的值 String methodName = "get" +field.getName().substring(0,1).toUpperCase() + field.getName().substring(1); Object fieldValue = null; try { Method method = c.getMethod(methodName); fieldValue = method.invoke(ob); if(null == fieldValue || (fieldValue instanceof Integer && 0 == (Integer)fieldValue) ){ System.out.println(fieldValue); continue; } sb.append(" and ").append(columnName); if(fieldValue instanceof String){ if(((String) fieldValue).contains(",")){ String[] sArray = ((String) fieldValue).split(","); sb.append(" in ").append("("); for(String s : sArray){ sb.append("'").append(s).append("',"); } sb.deleteCharAt(sb.length()-1); sb.append(")"); }else{ sb.append(" = "); sb.append("'").append(fieldValue).append("'"); } }else{ sb.append(" = ").append(fieldValue); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } sb.append(";"); System.out.println(sb.toString()); return null; } }