2015-11-06 161 views
3

我正在研究使用NamedParameterJdbcTemplate從MySQL數據庫獲取一些數據的方法。 listCurrentRecords應從數據庫中返回對象類型列表CustomerProductSalesOrderobjectType是從方法內傳遞的參數(1 || 2 || 3)定義的,並且它在類的前面定義爲公共變量。如何將類對象轉換爲通用對象類型?

public static final int TYPE_PRODUCT = 1; 
public static final int TYPE_CUSTOMER = 2; 
public static final int TYPE_SALESORDER = 3; 

該方法在下面提供。

public static List<Object> listCurrentRecords(int objectType) 
{ 

    // PRODUCT 
    if (objectType == 1){ 

    } 

    //CUSTOMER 
    else if (objectType == 2){ 

    } 

    // SALESORDER 
    else if (objectType == 3){ 

    } 

    return null; 
    // return new ArrayList<Object>(); 
} 

就是說,objectType == 2,則它將需要從使用getMyCustomer方法(同樣適用於ProductSalesOrder,他們會用他們的單獨的方法)如下所述Customer表抓住一些數據,

public static List<Customer> getMyCustomer(){ 

    return jdbc.query("select * from Customer", new RowMapper<Customer>() { 

     public Customer mapRow(ResultSet rs, int rowNum) throws SQLException { 

      Customer customer = new Customer(); 

      customer.setCustomerID(rs.getString("CustomerID")); 
      customer.setName(rs.getString("Name")); 
      customer.setAddress(rs.getNString("Address")); 
      customer.setPhone1(rs.getNString("Phone 1")); 
      customer.setPhone2(rs.getNString("Phone 2")); 

      customer.setCreditLimit(rs.getDouble("Credit Limit")); 
      customer.setCurrentCredit(rs.getDouble("Current Credit")); 

      return customer; 
     } 
    }); 

else if (objectType == 2){ }我想打電話給getMyCustomer方法,並在那裏得到List<Customer>。但是,方法listCurrentRecords的返回類型是List<Object>。如何轉換成List<Customer>List<Object>。我提供的僞代碼如下,

// customer 
    else if (objectType == 2){ 

     List<Customer> myCustomer = getMyCustomer(); 
     // how to convert ***myCustomer*** to List<Object> ? 
    } 

我很感謝如何在Java中正確編寫它的一些幫助。

回答

2

由於您想要Object類型(記住泛型是編譯時類型檢查功能),因此您可以使用addAll或使用第二個List的構造函數。像

List<Object> myCustomer = new ArrayList<>(getMyCustomer()); 

List<Object> al = new ArrayList<>(); 
// ... 
al.addAll(getMyCustomer()); 
+0

對我來說,過去的解決方案工作。我仍在研究這個項目,稍後我會提供更多的反饋意見。 –

1

我可能會做

List<Object> myCustomer = new ArrayList<>(getMyCustomer()); 

這樣通過遍歷原來創建一個新的列表,所以可能比較緩慢,這取決於的大小名單。

如果這是一個問題,另一種是做

List<Object> myCustomer = Collections.unmodifiableList(getMyCustomer()); 

這個版本的作品通過包裝原始的列表。如果你這樣做,你將無法調用任何修改列表的方法(addset,remove,clear等)。

+0

我不知道原因,這兩個解決方案都不適合我。我使用了較早的答案如下, List al = new ArrayList <>(); al.addAll(getMyCustomer()); –

+1

哦,好的。對不起,我的回答沒有幫助。我很高興你找到了你的答案。 –

+0

沒關係,我仍然在學習如何用Java編寫完整的應用程序。其實,之前的答案也提供了你的第一個答案。我正在使用Java 1.7它可能是該版本的問題? –

1

您可以從中構建你的List<Customer>

listCurrentRecords(int objectType)List<Object>基地其他答案考慮分開另一種替代的方法是你的控制之下,無論如何,如果有真正需要返回List<Object>沒有理由,你可以簡單地將其更改爲public static List<?> listCurrentRecords(int objectType)

事實上,在返回類型中使用通配符並不是一個好主意。讓我們更進一步,爲什麼不使listCurrentRecords返回正確的列表呢?例如,不是傳入整數,而是傳入所需數據類型的Class,例如,

public static <T> List<T> listCurrentRecords(Class<T> dataType) 

使主叫方可以接受的適當列表,例如,通過Customer

List<Customer> = listCurrentRecords(Customer.class) 
+0

有點偏題...你真的想繼續使用這樣的設計嗎?它看起來不正確。一種超級方法,可以爲您提供不同無關實體的「當前記錄」嗎?... –

+0

感謝您的回答。這些方法在我正在處理的應用程序中定義,但是,我會盡快測試您的解決方案。 –

相關問題