java注解和反射制作dao基类的练习
首先是三个注解
主键注解
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;
@notRecord
private 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+")";
}else
throw new NumException("未找到类主键 主键不能为空");
}else
throw new NumException("传入对象不是实体类");
}else
throw 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;
}else
throw new NumException("未找到类主键 主键不能为空");
}else
throw new NumException("传入对象不是实体类");
}else
throw 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));
}
}