自学内容网 自学内容网

ORM框架

1.通用的添加功能

public int insert(T t) throws Exception{
        StringBuffer sql=new StringBuffer("insert into ");
        Class<?> aClass = t.getClass();
        TableName annotation = aClass.getAnnotation(TableName.class);
        String name = annotation.name();
        sql.append(name);
        System.out.println(sql);
        Field[] declaredFields = aClass.getDeclaredFields();
        ArrayList<String> tableName=new ArrayList<>();
        ArrayList<String> tableValue=new ArrayList<>();
        for (Field field:declaredFields){
            TableId annotation1 = field.getAnnotation(TableId.class);
            TableFiled annotation2 = field.getAnnotation(TableFiled.class);
            if (annotation1!=null){
                continue;
            }
            String name1 = field.getName();
            if (annotation2!=null){
                name1=annotation2.name();
            }
            field.setAccessible(true);
            tableName.add(name1);
            tableValue.add("'"+field.get(t)+"'");
        }
        String replace = tableName.toString().replace("[", "(").replace("]", ")");
        String replace1 = tableValue.toString().replace("[", "(").replace("]", ")");
        sql.append(replace).append(" values ").append(replace1);
        System.out.println(sql);
        Connection getconnection = DBUtil.getconnection();
        PreparedStatement preparedStatement = getconnection.prepareStatement(sql.toString());
        int i = preparedStatement.executeUpdate();
        DBUtil.closeAll(getconnection,preparedStatement,null);
        return i;
    }

2.通用的修改功能

public int update(T t) throws Exception{
        StringBuffer sql=new StringBuffer("update ");
        Class<?> aClass = t.getClass();
        TableName annotation = aClass.getAnnotation(TableName.class);
        String name = annotation.name();
        sql.append(name+" set ");
        Field[] declaredFields = aClass.getDeclaredFields();
        int cc=0;
        String Where="where ";
        for (Field field:declaredFields){
            field.setAccessible(true);
            String names="";
            String values="";
            TableId tableId = field.getAnnotation(TableId.class);
            TableFiled tableFiled = field.getAnnotation(TableFiled.class);
            if (tableId!=null){
                names=field.getName();
                String value = tableId.value();
                if (!value.equals("")){
                    names=value;
                }
                values="'"+field.get(t)+"'";
                Where+=names+" = "+ values;
                continue;
            }
            names=field.getName();
            if (tableFiled!=null){
                names=tableFiled.name();
            }
            values="'"+field.get(t)+"'";
            if (cc==declaredFields.length-2){
                sql.append(names).append(" = ").append(values);
                break;
            }
            sql.append(name).append(" = ").append(values).append(",");
            cc++;
        }
        sql.append(Where);
        System.out.println(sql);
        Connection getconnection = DBUtil.getconnection();
        PreparedStatement ps = getconnection.prepareStatement(sql.toString());
        int i = ps.executeUpdate();
        DBUtil.closeAll(getconnection,ps,null);
        return i;
    }

3.通用的删除功能

 public int delete(Object key) throws Exception{
        StringBuffer sql=new StringBuffer("delete from ");
        //实体类的反射类
        String simpleName = clazz.getSimpleName();
        TableName annotation = clazz.getAnnotation(TableName.class);
        //获取表明
        if (annotation!=null){
            simpleName = annotation.name();
        }
        sql.append(simpleName+" where ");
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field:declaredFields){
            field.setAccessible(true);
            TableId annotation1 = field.getAnnotation(TableId.class);
            if (annotation1!=null){
                String name = field.getName();
                String value = annotation1.value();
                if (!value.equals("")){
                    name=value;
                }
                sql.append(name+"='"+ key+"'");
                break;
            }
        }
        System.out.println(sql);
        Connection getconnection = DBUtil.getconnection();
        PreparedStatement preparedStatement = getconnection.prepareStatement(sql.toString());
        int i = preparedStatement.executeUpdate();
        DBUtil.closeAll(getconnection,preparedStatement,null);
        return i;
    }

4.通用的根据主键获取功能

public T selectById(Object key) throws Exception{
        StringBuffer sql=new StringBuffer("select * from ");
        TableName annotation = clazz.getAnnotation(TableName.class);
        String name = clazz.getName();
        if (annotation!=null){
            name = annotation.name();
        }
        sql.append(name+" where ");
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field:declaredFields){
            field.setAccessible(true);
            TableId annotation1 = field.getAnnotation(TableId.class);
            if (annotation1!=null){
                String name1 = field.getName();
                String value = annotation1.value();
                if (!value.equals("")){
                    name1=value;
                }
                sql.append(name1+"="+"'"+key+"'");
            }
        }
        Connection getconnection = DBUtil.getconnection();
        PreparedStatement preparedStatement = getconnection.prepareStatement(sql.toString());
        ResultSet resultSet = preparedStatement.executeQuery();
        T t=null;
        while (resultSet.next()){
            t = (T) clazz.newInstance();
            for (Field field:declaredFields){
                field.setAccessible(true);
                String column = field.getName();
                TableFiled annotation1 = field.getAnnotation(TableFiled.class);
                TableId annotation2 = field.getAnnotation(TableId.class);
                if (annotation2!=null){
                    String value = annotation2.value();
                    if (!value.equals("")){
                        column=value;
                    }
                }
                if (annotation1!=null){
                    column=annotation1.name();
                }
                Object object = resultSet.getObject(column);
                field.set(t,object);
            }

        }
        return t;
    }

5.通用的获取说有功能

 public List<T> selectAll() throws Exception{
        ArrayList<T> list=new ArrayList<>();
        StringBuffer sql=new StringBuffer("select * from ");
        TableName annotation = clazz.getAnnotation(TableName.class);
        if (annotation!=null){
            String name = annotation.name();
            String simpleName = clazz.getSimpleName();
            if (!name.equals("")){
                simpleName=name;
            }
            sql.append(simpleName);
        }
        Connection getconnection = DBUtil.getconnection();
        PreparedStatement preparedStatement = getconnection.prepareStatement(sql.toString());
        ResultSet resultSet = preparedStatement.executeQuery();
        T t=null;
        Field[] declaredFields = clazz.getDeclaredFields();
        while (resultSet.next()){
            t = (T) clazz.newInstance();
            for (Field field:declaredFields){
                field.setAccessible(true);
                String name = field.getName();
                TableId annotation1 = field.getAnnotation(TableId.class);
                TableName annotation2 = field.getAnnotation(TableName.class);
                if (annotation2!=null){
                   name=annotation2.name();
                }
                if (annotation1!=null&&!annotation1.value().equals("")){
                    name=annotation1.value();
                }
                Object object = resultSet.getObject(name);
                field.set(t,object);
            }
            list.add(t);
        }
        return list;
    }

6.自定义注解

该注解 表明了 该属性 在数据库中的字段名

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableFiled {
    String name();
}

 该注解 表明了 该属性 是主键

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableId {
    String value() default "";
}

该注解 表明了 该对象 在数据库中的表名

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableName {
    String name();
}

 


原文地址:https://blog.csdn.net/weixin_65127444/article/details/137540469

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!