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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值