Bootstrap

insert update

KeyTable

@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface KeyTable {
    String value();

}

TableField

//加到属性上
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableField {
    String value();
}

TableName

//运行时执行
@Retention(value = RetentionPolicy.RUNTIME)
//只能加载在类上
@Target(ElementType.TYPE)
public @interface TableName {
    String value();//表明
}

BaseDao

package com.aaa.util;

import com.aaa.annotation.KeyTable;
import com.aaa.annotation.TableField;
import com.aaa.annotation.TableName;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;

public class BaseDao<T> {
    //增加
    public int insert(T t){
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            StringBuffer sb = new StringBuffer("insert into ");
            //获取表明
            Class<?> aClass = t.getClass();
            TableName annotation = aClass.getAnnotation(TableName.class);
            String value = "";
            if (annotation != null) {
                value = annotation.value();
            } else {
                value = aClass.getSimpleName();
            }


            sb.append(value);
            //获取所有反射类的属性对象
            Field[] declaredFields = aClass.getDeclaredFields();
            //创建连个集合
            ArrayList<String> columns = new ArrayList<String>();//存放所有列名
            ArrayList<String> values = new ArrayList<String>();//存放所有列值
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                TableField tableField = declaredField.getAnnotation(TableField.class);
                if (tableField != null) {
                    columns.add(tableField.value());
                } else {
                    columns.add(declaredField.getName());
                }

                values.add("'" + (declaredField.get(t)) + "'");
            }
            sb.append(columns.toString().replace("[", "(").replace("]", ")"));
            sb.append(" values ");
            sb.append(values.toString().replace("[", "(").replace("]", ")"));

             conn = DBUtils.getConn();
             ps = conn.prepareStatement(sb.toString());
            int i = ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.closeAll(null,ps,conn);
        }
        return 0;
    }
    //修改
    public int update(T t){
        Connection connection=null;
        PreparedStatement ps=null;
        try {
            StringBuffer sb = new StringBuffer("update ");
            //获取表明
            Class<?> aClass = t.getClass();
            TableName annotation = aClass.getAnnotation(TableName.class);
            String value = "";
            if (annotation != null) {
                value = annotation.value();
            } else {
                value = aClass.getSimpleName();
            }
            sb.append(value+" set ");
            //获取所有反射类的属性对象
            Field[] declaredFields = aClass.getDeclaredFields();
            //a=id   b=id的值
            String a = null;
            Object b = null;
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                KeyTable annotation1 = declaredField.getAnnotation(KeyTable.class);
                if(annotation1!=null){
                    a = annotation1.value();
                    b = declaredField.get(t);

                }else {
                    TableField tableField = declaredField.getAnnotation(TableField.class);
                    if (tableField != null) {
                        sb.append(tableField.value()+"=");
                    } else {
                        sb.append(declaredField.getName()+"=");
                    }

                    sb.append("'" + (declaredField.get(t)) + "',");
                }
            }
            sb=new StringBuffer(sb.substring(0,sb.lastIndexOf(",")));
            sb.append(" where ");
            sb.append(a+" = "+b);
             connection = DBUtils.getConn();
             ps = connection.prepareStatement(sb.toString());
            int i = ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.closeAll(null,ps,connection);
        }
        return 0;
    }
}
;