Referências: https://www.devmedia.com.br/dao-pattern-persistencia-de-dados-utilizando-o-padrao-dao/30999 https://www.oracle.com/technetwork/java/dataaccessobject-138824.html
Ideia geral do padrão DAO:
- Para cada entidade, haverá um objeto responsável por fazer acesso a dados relacionado a esta
entidade. Por exemplo:
- Cliente: ClienteDao
- Produto: ProdutoDao
- Pedido: PedidoDao
- Cada DAO será definido por uma interface.
- A injeção de dependência pode ser feita por meio do padrão de projeto Factory
Project: http://github.com/acenelio/demo-dao-jdbc
Checklist:
-
Github: create a new project
- NOTE: choose .gitignore type as Java
-
Eclipse: create new java project with MySQLConnector library
- Copy db package and db.properties from: https://github.com/acenelio/jdbc5
-
Create local repository and push to Github:
git init git remote add origin https://github.com/acenelio/jdbc-dao-demo.git git pull origin master git add . git commit -m "Project created" git push -u origin master
Entity class checklist:
- Attributes
- Constructors
- Getters/Setters
- hashCode and equals
- toString
- implements Serializable
Entity class checklist:
- Attributes
- Constructors
- Getters/Setters
- hashCode and equals
- toString
- implements Serializable
DaoFactory
- Essa classe expõe um método que retorna o tipo da interface, mas internamente ela vai instanciar uma implementação, é uma forma de não expor a implementação.
DepartmentDao e SellerDao
- As interfaces são uma forma de deixar sua classe dependente apenas da interface e não de uma implementação. Desta forma, você pode no futuro trocar a implementação sem se preocupar com impactos em outras partes do código, desde que a nova implementação atenda à interface.
SQL Query:
SELECT seller.*,department.Name as DepName
FROM seller INNER JOIN department
ON seller.DepartmentId = department.Id
WHERE seller.Id = ?
private Seller instantiateSeller(ResultSet rs, Department dep) throws SQLException {
Seller obj = new Seller();
obj.setId(rs.getInt("Id"));
obj.setName(rs.getString("Name"));
obj.setEmail(rs.getString("Email"));
obj.setBaseSalary(rs.getDouble("BaseSalary"));
obj.setBirthDate(rs.getDate("BirthDate"));
obj.setDepartment(dep);
return obj;
}
private Department instantiateDepartment(ResultSet rs) throws SQLException {
Department dep = new Department();
dep.setId(rs.getInt("DepartmentId"));
dep.setName(rs.getString("DepName"));
return dep;
}
SQL Query:
SELECT seller.*,department.Name as DepName
FROM seller INNER JOIN department
ON seller.DepartmentId = department.Id
WHERE DepartmentId = ?
ORDER BY Name
SQL Query:
SELECT seller.*,department.Name as DepName
FROM seller INNER JOIN department
ON seller.DepartmentId = department.Id
ORDER BY Name
SQL Query:
INSERT INTO seller
(Name, Email, BirthDate, BaseSalary, DepartmentId)
VALUES
(?, ?, ?, ?, ?)
SQL Query:
UPDATE seller
SET Name = ?, Email = ?, BirthDate = ?, BaseSalary = ?, DepartmentId = ?
WHERE Id = ?
SQL Query:
DELETE FROM seller
WHERE Id = ?
Checklist:
- DepartmentDaoJDBC
- DaoFactory
- Program2