反射实现增删改查(DAO层)——删除数据

先贴出代码,后续补充自己的思路、配置文件、使用方式:
/**
     * 
     *  删除数据
     */
    @Override
    public void deleteObject(List<Map<String, Object>> params, String tableName) {
        StringBuilder sql = new StringBuilder(
                "DELETE FROM order_info WHERE 1=1 ");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBConnection.getConnection();
            if (params != null && params.size() > 0) {
                for (int i = 0; i < params.size(); i++) {
                    Map<String, Object> map = params.get(i);
                    sql.append(" AND  " + map.get("name") + " "
                            + map.get("rela") + " " + map.get("value") + " ");
                }
            }
            connection = DBConnection.getConnection();
            preparedStatement = connection.prepareStatement(sql.toString());
            preparedStatement.executeLargeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败!");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            DBConnection.close(connection, preparedStatement, null);
        }

    }

猜你喜欢

转载自www.cnblogs.com/caoleiCoding/p/9061931.html