处理Java中的CRUD操作等重复任务的最佳方法是什么

处理Java中的CRUD操作等重复任务的最佳方法是什么

本文介绍了处理Java中的CRUD操作等重复任务的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写类和方法以最大程度地减少重复执行的任务。

I'm trying to write classes and methods to minimize recurring tasks.

例如,我计划编写一个通用的DAO(数据库访问对象)类Java中的Java,可以通过填充inputdata来执行基本的操作。

For instance, I'm planning to write a generic DAO (Database Access Object) class in Java which can do the basic crud operations by accpeting the inputdata.

例如,以下是我刚开始编写的方法,该方法接受Bean作为参数并构造一个插入查询。

For example, the following is a method I just started writing which accepts a Bean as a parameter and constructs an insert query.

public boolean insert(Object object) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException, InvalidDataException {
String tableName = object.getClass().getSimpleName().toLowerCase() + "s";

Field[] fields = object.getClass().getDeclaredFields();

ArrayList<String> columnNames = new ArrayList<>();
for (Field field : fields) {
    String fieldName = field.getName();
    Method method = object.getClass().getMethod("get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1));
    if ("id".equalsIgnoreCase(fieldName)) {
    continue;
    }
    if (method.invoke(object) != null || "created".equalsIgnoreCase(fieldName) || "modified".equalsIgnoreCase(fieldName)) {
    columnNames.add(General.toUnderscore(fieldName));
    }
}

String sqlQuery = generateInsertSQLQuery(tableName, columnNames);
System.out.println(sqlQuery);

PreparedStatement ps = this.conn.prepareStatement(sqlQuery);


int index = 1;
for (Field field : fields) {
    String fieldName = field.getName();
    Method method = object.getClass().getMethod("get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1));
    if ("created".equalsIgnoreCase(fieldName) || "modified".equalsIgnoreCase(fieldName)) {
    ps.setDate(index++, new java.sql.Date((new java.util.Date()).getTime()));
    } else {
    if (method.invoke(object) != null) {
        if (field.getType() == String.class) {
        ps.setString(index++, (String) method.invoke(object));
        } else if (field.getType() == Integer.class) {
        ps.setInt(index++, (Integer) method.invoke(object));
        }  else if (field.getType() == Long.class) {
        ps.setLong(index++, (Long) method.invoke(object));
        } else if (field.getType() == java.util.Date.class) {
        java.sql.Date date = new java.sql.Date(((java.util.Date) method.invoke(object)).getTime());
        ps.setDate(index++, date);
        }
    }
    }
}
ps.executeUpdate();
return true;
}

bean是带有getter和setter的相当标准的类。我的问题是,这是处理Java中事情的正确方法吗?

The bean is a pretty standard class with getters and setters. My question is, is this a right approach to handle things in Java ?

是否有任何流行的资源已经实现了这一目标?

Are there any popular resources which already accomplished this ?

请为我提供宝贵的意见。

Kindly guide me with your valuable inputs.

推荐答案

我建议将Spring Framework与Spring Data JPA项目一起使用。

I would recommend the Spring Framework with the Spring Data JPA project.

您可以使用正确的JPA注释对任何JavaBean进行注释。然后创建一个扩展Spring CrudRepository的接口。进行一些配置并添加Hibernate或EclipseLink依赖项。

You can annotate any JavaBean with the correct JPA annotations. Then create an interface which extends the Spring CrudRepository. Some configuration and add the Hibernate or EclipseLink dependencies. Ready!

好的教程:

这篇关于处理Java中的CRUD操作等重复任务的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:21