java dao 反射_java注解和反射制作dao基类的练习

最近做项目中有接触到反射的使用简单的做了一个注释

首先的想法是根据类中不为空的值生成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;

@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));

}

}

这里如果要套用着DAO用就好了。我只是简单的实现一下。欢迎拍砖。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值