@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
表名注解
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String value();
}
字段注解
@Table("user")
public class Filter {
@Column("id")
private int id;
@Column("username")
private String userName;
@Column("nickname")
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;
}
}
一个表单类
@Table("deapartment")
public class Filter2 {
@Column("id")
private int id;
@Column("name")
private String name;
@Column("amount")
private String amount;
@Column("age")
private int age;
@Column("city")
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
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 class Test {
public static void main(String[] args) {
Filter f1 = new Filter();
f1.setId(10);
Filter f2 = new Filter();
f2.setAge(45);
f2.setCity("china");
f2.setUserName("lucy");
Filter f3 = new Filter();
f3.setEmail("liu@sh.com,dahod@1223.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); Filter2 filter2 = new Filter2();
filter2.setId(02);
filter2.setAge(15);
filter2.setAmount("14");
filter2.setName("小明");
System.out.println(query(filter2));
}
@SuppressWarnings("unused")
private static String query(Object f){
StringBuffer sb = new StringBuffer();
//1.获取到class
Class c = f.getClass();
//2.获取到table的名字
boolean exsits = c.isAnnotationPresent(Table.class);
if(!exsits){
return null;
}
Table t = (Table) c.getAnnotation(Table.class);
String tableName = t.value();
sb.append("select *from ").append(tableName).append(" where 1=1");
//3.变量所有的字段
Field[] farray = c.getDeclaredFields();
for(Field field :farray){
//4.0处理每个字段对应的名字
//4.1拿到字段名
boolean fExist = field.isAnnotationPresent(Column.class);
if(!fExist){
continue;
}
Column column = field.getAnnotation(Column.class);
String coiumnName = column.value();
//4.2拿到字段的值
String filedName = field.getName();
String getMethodName = "get"+filedName.substring(0,1).toUpperCase()+filedName.substring(1);
Object fieldValue = null;
try {
Method getMethod = c.getMethod(getMethodName);
fieldValue =getMethod.invoke(f);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//4.3拼装sql
if(fieldValue ==null||
(fieldValue instanceof Integer &&(Integer)fieldValue==0)){
continue;
}
sb.append(" and ").append(filedName);
if(fieldValue instanceof String){
if(((String)fieldValue).contains(",")){
String [] values = ((String)fieldValue).split(",");
sb.append(" in(");
for(String v:values){
sb.append("'").append(v).append("'").append(",");
}
sb.deleteCharAt(sb.length()-1);
sb.append(")");
}else{
sb.append("=").append("'").append(fieldValue).append("'");
}
}else if(fieldValue instanceof Integer){
sb.append("=").append(fieldValue);
}
//sb.append(" and ").append(filedName).append("=").append(fieldValue);
}
return sb.toString(); }
}
测试类
select *from user where 1=1 and id=10
select *from user where 1=1 and userName='lucy' and age=45 and city='china'
select *from user where 1=1 and email in('liu@sh.com','dahod@1223.com')
select *from deapartment where 1=1 and id=2 and name='小明' and amount='14' and age=15
输出结果