1.需求描述
有两张表(用户表和部门表),字段包括用户ID、用户名、年龄、邮箱、部门领导和部门人数等字段,现在需要对每个字段或字段的组合条件进行检索,并打印SQL。
2.代码示例
(1).表定义
@Table("user")
public class User {
@Column("id")
private int id;
@Column("user_name")
private String userName;
、、、、、
@Column("age")
private int age;
@Column("email")
private String email;
//get和set方法
}
@Table("department")
public class Department {
@Column("id")
private int id;
@Column("name")
private String name;
@Column("leader_name")
private String leader_name;
@Column("amount")
private int amount;
//get和set方法
}
(2).自定义Table和Column注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String value();
}
(3).测试代码
public class App {
public static void main(String[] args) {
//查询id为10的用户
User query1 = new User();
query1.setId(10);
//查询name为lucy的用户
User query2 = new User();
query2.setUserName("lucy");
query2.setAge(18);
//查询邮箱为12******21@qq.com的用户
User query3 = new User();
query3.setEmail("12******21@qq.com,188****7495@163.com");
System.out.println(query(query1));
System.out.println(query(query2));
System.out.println(query(query3));
Department department = new Department();
department.setName("技术部");
department.setAmount(10);
System.out.println(query(department));
}
private static String query(Object object) {
StringBuilder sb = new StringBuilder();
//1.获取到class
Class objectClass = object.getClass();
//2.获取到table的名字
boolean tableIsExists = objectClass.isAnnotationPresent(Table.class);
if (!tableIsExists) {
return null;
}
Table table = (Table) objectClass.getAnnotation(Table.class);
String tableName = table.value();
sb.append("select * from ").append(tableName).append(" where 1=1");
//3.遍历所有字段,处理每个字段对应的sql
Field[] fields = objectClass.getDeclaredFields();
for (Field field : fields) {
//4.1拿到字段名
boolean fieldIsExists = field.isAnnotationPresent(Column.class);
if (!fieldIsExists) {
continue;
}
//4.2拿到字段的值
String filedName = field.getName();
String getMethodName = "get" + filedName.substring(0, 1).toUpperCase() + filedName.substring(1);
Object fieldValue = null;
try {
Method getMethod = objectClass.getMethod(getMethodName);
fieldValue = getMethod.invoke(object, null);
} catch (Exception e) {
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);
}
}
return sb.toString();
}
}
select * from user where 1=1 and id=10
select * from user where 1=1 and userName='lucy' and age=18
select * from user where 1=1 and email in('12******21@qq.com','188****7495@163.com')
select * from department where 1=1 and name='技术部' and amount=10