使用到的两个注解类:
<span style="font-size:18px;">package dao;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by raid on 2016/5/14.
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
</span>
package dao;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by raid on 2016/5/14.
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String value();
}
package dao;
/**
* Created by raid on 2016/5/14.
*/
@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 userName) {
this.userName = userName;
}
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;
}
}
demo类:
package dao;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by raid on 2016/5/14.
*/
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");
Filter f3 = new Filter();
f3.setEmail("123@12.com,456@45.com,789@78.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);;
}
private static String query(Filter f) {
StringBuilder sb = new StringBuilder();
//1.获取到class
Class c = f.getClass();
//2.获取到table的名字
boolean exists = c.isAnnotationPresent(Table.class);
if (!exists) {
return null;
}
Table t = (Table) c.getAnnotation(Table.class);
String tableName = t.value();
sb.append("select * from ").append(tableName).append(" where 1=1");
//2.遍历所有的字段
Field[] fArray = c.getDeclaredFields();
for (Field field : fArray) {
//4.处理每个字段对应的sql
//4.1拿到字段名
boolean fExists = field.isAnnotationPresent(Column.class);
if (!fExists) {
continue;
}
Column column = field.getAnnotation(Column.class);
String columName = 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 (Exception e) {
e.printStackTrace();
}
//4.3拼装sql
if (fieldValue == null || (fieldValue instanceof Integer && (Integer)fieldValue == 0)) {
continue;
}
if (fieldValue instanceof String) {
if (((String)fieldValue).contains(",")) {
String[] values = ((String)fieldValue).split(",");
fieldValue = " in(";
for (String v : values) {
fieldValue += "'" + v + "'" + ",";
}
fieldValue = ((String)fieldValue).substring(0, ((String) fieldValue).length()-1) + ")";
} else {
fieldValue = "'" + fieldValue + "'";
}
}
sb.append(" and ").append(fieldName).append("=").append(fieldValue);
}
return sb.toString();
}
}