java反射获取注解并拼接sql语句

先建两个注解 分别为 Table 和 Column

package com.hk.test;

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 com.hk.test;

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();
}

然后新建一个 bean 类 Filter.java (本类可根据自己需求更改,只要样式不变就好)

package com.hk.test;
@Table("user")
public class Filter {
@Column("id")
private int id;
@Column("user_name")
private String userName;
@Column("age")
private int age;
@Column("email")
private String email;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} }

然后测试类

package com.hk.test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
*
* @author hk_z
* @deprecated 对类进行反射 获取注解和字段名 拼接成sql语句
* @version 0.1
*/
public class Test {
public static void main(String[] args) {
Filter f1 = new Filter();
f1.setId(10); Filter f2 = new Filter();
f2.setUserName("lucy"); //模糊查询 Filter f3 = new Filter();
f3.setEmail("zhangsan@qq.com,lisi@sina.com.cn"); //任意其一 String sql1 =query(f1);
System.out.println(sql1);
String sql2 =query(f2);
System.out.println(sql2);
String sql3 =query(f3);
System.out.println(sql3);
} private static String query(Filter f1) {
// TODO Auto-generated method stub
StringBuilder sb =new StringBuilder();
//1.获取到class
Class c = f1.getClass();
//2.获取到table 的名字
boolean exists = c.isAnnotationPresent(Table.class); //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");
//3.遍历所有的字段
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拿到字段的值
String filedName = field.getName(); //变量的名字
String getMethodName = "get"+filedName.substring(0,1).toUpperCase()+filedName.substring(1);
Object fieldValue = null;
//拼装get方法 获取返回值 ;
try {
Method getMethod = c.getMethod(getMethodName); //反射
fieldValue = getMethod.invoke(f1); //反射调用 } 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);
String [] val=null;
if(fieldValue instanceof String){ //字串型并且有逗号 进行拆分
if(((String)fieldValue).contains(",")){
val = ((String) fieldValue).split(",");
sb.append(" in(");
for(String v:val){
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();
}
}
上一篇:[SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端


下一篇:[MySQL Reference Manual] 7 备份和恢复