Git Product home page Git Product logo

demo-dao-jdbc's Introduction

Padrão de projeto DAO (Data Access Object)

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

image

Creating project and git repository

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

  • 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
    

Department entity class

Entity class checklist:

  • Attributes
  • Constructors
  • Getters/Setters
  • hashCode and equals
  • toString
  • implements Serializable

image

Seller entity class

Entity class checklist:

  • Attributes
  • Constructors
  • Getters/Setters
  • hashCode and equals
  • toString
  • implements Serializable

DepartmentDao and SellerDao interfaces

image

SellerDaoJDBC and DaoFactory

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.

findById implementation

SQL Query:

SELECT seller.*,department.Name as DepName
FROM seller INNER JOIN department
ON seller.DepartmentId = department.Id
WHERE seller.Id = ?

image

Reusing instantiation

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;
}

findByDepartment implementation

SQL Query:

SELECT seller.*,department.Name as DepName
FROM seller INNER JOIN department
ON seller.DepartmentId = department.Id
WHERE DepartmentId = ?
ORDER BY Name

image

findAll implementation

SQL Query:

SELECT seller.*,department.Name as DepName
FROM seller INNER JOIN department
ON seller.DepartmentId = department.Id
ORDER BY Name

insert implementation

SQL Query:

INSERT INTO seller
(Name, Email, BirthDate, BaseSalary, DepartmentId)
VALUES
(?, ?, ?, ?, ?)

update implementation

SQL Query:

UPDATE seller
SET Name = ?, Email = ?, BirthDate = ?, BaseSalary = ?, DepartmentId = ?
WHERE Id = ?

delete implementation

SQL Query:

DELETE FROM seller
WHERE Id = ?

DepartmentDao implementation and test

Checklist:

  • DepartmentDaoJDBC
  • DaoFactory
  • Program2

demo-dao-jdbc's People

Contributors

edufreitass avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.