首先新建2个注解类
package cc.openkit.kitTest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
package cc.openkit.kitTest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String value();
}
对象类
package cc.openkit.kitTest;
@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;
}
}
获取注解
package cc.openkit.kitTest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
Filter f1 = new Filter();
f1.setId(10);// 查询id为10的用户
Filter f2 = new Filter();
f1.setUserName("lucy");// 根据用户查询
Filter f3 = new Filter();
f1.setEmail("liu@sina.com,zh@163.com,222@qq.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.setName("你好");
System.out.println(query(filter2));
}
private static String query(Object f) {
StringBuffer sb = new StringBuffer();
// 1. 获取到Class
Class c = f.getClass();
// 2. 获取table的名字
boolean exists = c.isAnnotationPresent(Table.class); // 判断传来的是不是Table对象
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");
// 遍历所有的字段
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 columnName = column.value();
// 4.2. 拿到字段的值
// 反射调用get取出字段名
String filedName = field.getName();
String getMethodName = "get"+filedName.substring(0,1).toUpperCase()+filedName.substring(1);
Object filedValue = null;
try {
Method getMethod = c.getMethod(getMethodName);
filedValue = getMethod.invoke(f);
} catch (Exception e) {
e.printStackTrace();
}
// 4.3. 拼接sql
if(filedValue==null ||
(filedValue instanceof Integer && (Integer)filedValue==0)){
// 判断如果是 String 类型 == null,或者Int 类型==0,就不做处理
continue;
}
sb.append(" and ").append(filedName);
if(filedValue instanceof String){
// 如果email
if(((String) filedValue).contains(",")){
String[] values = ((String) filedValue).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(filedValue).append("'");
}
}else if(filedValue instanceof Integer){
sb.append("=").append(filedValue);
}
}
return String.valueOf(sb);
}
}
转载原创文章请注明,转载自:
ben -
java利用注解写的一个测试demo
(https://www.benhailong.com/?p=326)
「如果你觉得对你有用,欢迎点击下方按钮对我打赏」
支付宝支付
微信支付