#107 Dao example JavaDoc
This commit is contained in:
parent
a7d25e0485
commit
3526d96e37
@ -1,47 +1,56 @@
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
|
||||
* interacting with the data. The below example demonstrates basic operations(CRUD): select, add, update, and delete.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
|
||||
|
||||
Customer customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.addCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.updateCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customerDao.deleteCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
}
|
||||
|
||||
public static List<Customer> generateSampleCustomers() {
|
||||
Customer customer1 = new Customer(1, "Adam", "Adamson");
|
||||
Customer customer2 = new Customer(2, "Bob", "Bobson");
|
||||
Customer customer3 = new Customer(3, "Carl", "Carlson");
|
||||
|
||||
List<Customer> customers = new ArrayList<Customer>();
|
||||
customers.add(customer1);
|
||||
customers.add(customer2);
|
||||
customers.add(customer3);
|
||||
return customers;
|
||||
}
|
||||
}
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
|
||||
* interacting with the data. The below example demonstrates basic operations(CRUD): select, add, update, and delete.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
|
||||
|
||||
Customer customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.addCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.updateCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customerDao.deleteCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate customers
|
||||
* @return list of customers
|
||||
*/
|
||||
public static List<Customer> generateSampleCustomers() {
|
||||
Customer customer1 = new Customer(1, "Adam", "Adamson");
|
||||
Customer customer2 = new Customer(2, "Bob", "Bobson");
|
||||
Customer customer3 = new Customer(3, "Carl", "Carlson");
|
||||
|
||||
List<Customer> customers = new ArrayList<Customer>();
|
||||
customers.add(customer1);
|
||||
customers.add(customer2);
|
||||
customers.add(customer3);
|
||||
return customers;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,12 @@
|
||||
package com.iluwatar.dao;
|
||||
|
||||
/**
|
||||
*
|
||||
* Customer
|
||||
*
|
||||
*/
|
||||
public class Customer {
|
||||
|
||||
private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
@ -2,7 +2,13 @@ package com.iluwatar.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* CustomerDao
|
||||
*
|
||||
*/
|
||||
public interface CustomerDao {
|
||||
|
||||
public List<Customer> getAllCustomers();
|
||||
public Customer getCusterById(int id);
|
||||
public void addCustomer(Customer customer);
|
||||
|
@ -3,11 +3,13 @@ package com.iluwatar.dao;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* The data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism.
|
||||
* By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database.
|
||||
* This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of
|
||||
* domain-specific objects and data types (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
|
||||
* database schema, etc.
|
||||
*
|
||||
*/
|
||||
public class CustomerDaoImpl implements CustomerDao {
|
||||
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.dao.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.dao.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user