为什么80%的码农都做不了架构师?>>>
最近做项目中有接触到反射的使用简单的做了一个注释
首先的想法是根据类中不为空的值生成sql
首先是三个注解
主键注解
package comments;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 主键* @author Administrator*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Key {
}
package comments;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 如果不和数据关联则设置此注解* @author Administrator**/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface notRecord {
}
package comments;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 设置表名* @author Administrator**/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Table {public String name();
}
然后是自定义异常类
package org;/*** 设置自定义异常* @author Administrator**/
public class NumException extends Exception {private String name;public NumException(String name){this.name=name;}public String toString(){return name;}
}
实体类
package org;import comments.Key;
import comments.Table;
import comments.notRecord;@Table(name = "student")
public class Student {@Key private String id;private String name;@notRecordprivate String sex;private int age;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
处理实体类生成sql的类。
package org;import java.lang.reflect.Field;import comments.Key;
import comments.Table;
import comments.notRecord;public class Processing {/*** 通过实体类生成 insert into sql语句* @param cl* @return * @throws IllegalArgumentException* @throws IllegalAccessException* @throws NumException*/public String save(Object cl) throws IllegalArgumentException, IllegalAccessException, NumException{String sql="insert into ";if(cl!=null){Field[] fiels=cl.getClass().getDeclaredFields();//获得反射对象集合boolean t=cl.getClass().isAnnotationPresent(Table.class);//获得类是否有注解if(t){Table tab=cl.getClass().getAnnotation(Table.class);sql+=tab.name();//获得表名String name ="";//记录字段名String value ="";//记录值名称boolean bl=false;//记录主键是否为空for(Field fl:fiels){//循环组装fl.setAccessible(true);//开启支私有变量的访问权限Object tobj=fl.get(cl);if(tobj!=null){if(fl.isAnnotationPresent(Key.class)){//判断是否存在主键bl=true;}if(!fl.isAnnotationPresent(notRecord.class)){name+=fl.getName()+",";value+="'"+tobj.toString()+"',";}}}if(bl){if(name.length()>0)name=name.substring(0,name.length()-1);if(value.length()>0)value=value.substring(0,value.length()-1);sql+="("+name+") values("+value+")";}elsethrow new NumException("未找到类主键 主键不能为空");}elsethrow new NumException("传入对象不是实体类");}elsethrow new NumException("传入对象不能为空");//抛出异常return sql;}/*** 传入对象更新* @param obj* @return * @throws IllegalArgumentException* @throws IllegalAccessException* @throws NumException*/public String update(Object obj) throws IllegalArgumentException, IllegalAccessException, NumException{String sql="update ";if(obj!=null){Field[] fiels=obj.getClass().getDeclaredFields();//获得反射对象集合boolean t=obj.getClass().isAnnotationPresent(Table.class);//获得类是否有注解if(t){Table tab=obj.getClass().getAnnotation(Table.class);sql+=tab.name()+" set ";//获得表名String wh ="";//记录字段名String k="";boolean bl=false;//记录主键是否为空for(Field fl:fiels){//循环组装fl.setAccessible(true);//开启支私有变量的访问权限Object tobj=fl.get(obj);if(tobj!=null){if(fl.isAnnotationPresent(Key.class)){//判断是否存在主键bl=true;k=fl.getName()+"='"+tobj.toString()+"' where ";}else{if(!fl.isAnnotationPresent(notRecord.class)){wh+=fl.getName()+"='"+tobj.toString()+"',";}}}}if(bl){if(wh.length()>0)wh=wh.substring(0,wh.length()-1);if(k.length()>0)k=k.substring(0,k.length()-1);sql+=k+wh;}elsethrow new NumException("未找到类主键 主键不能为空");}elsethrow new NumException("传入对象不是实体类");}elsethrow new NumException("传入对象不能为空");//抛出异常return sql;}
}
最后是测试类
package org;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import comments.Table;
import comments.Key;public class temp {public static void main(String[] aa) throws IllegalArgumentException, IllegalAccessException, NumException{Student stu=new Student();stu.setId("ccc");stu.setName("姓名");stu.setAge(18);stu.setSex("男");//stu=null;System.out.println(new Processing().save(stu));System.out.println(new Processing().update(stu));}
}
这里如果要套用着DAO用就好了。我只是简单的实现一下。欢迎拍砖。
输出结果
insert into student(id,name,age) values('ccc','姓名','18')
update student set id='ccc' where name='姓名',age='18'