Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

JDBC - JPA - Spring Data

1.883 visualizaciones

Publicado el

A small presentation for a brownbag about Spring Data and all of previous technology iterations.

Publicado en: Software
  • Sé el primero en comentar

JDBC - JPA - Spring Data

  1. 1. JDBC JPA Spring Data
  2. 2. The JDBC API makes it possible to do three things: • Establish a connection with a database or access any tabular data source • Send SQL statements • Process the results JDBC (1)
  3. 3. Connection conn = null; Statement stmt = null; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/EMP", "username", "password"); stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id, first FROM Employees"); while (rs.next()) { System.out.println("ID: " + rs.getInt("id") + ", First: " + rs.getString("first")); } rs.close(); stmt.close(); conn.close(); JDBC (2)
  4. 4. JPA(1) Includes: • the API itself, defined in the javax.persistence package • the Java Persistence Query Language (JPQL) • object/relational metadata DAO -> Persistance
  5. 5. JPA(2) @Entity @Table(name = "orders") public class Order { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private long id; @Column(name = "date") private Date date; @ManyToOne @JoinColumn(name = "product_id") private Product product; }
  6. 6. JPA(3) Product product = new Product(); Order order = new Order(); order.setProduct(product); entityManager.persist(product); entityManager.persist(order); Product dbProduct = entityManager.find(Product.class, product.getId()); String name = "InitialName"; String query = "SELECT p FROM Product p WHERE p.name LIKE :productName"; Product product = entityManager.createQuery(query, Product.class) .setParameter("productName", name) .getSingleResult();
  7. 7. Spring Data CrudRepository PagingAndSortingRepository JPARepository Spring Data JPA CassandraRepository Spring Data Cassandra MongoRepository Spring Data MongoDB Spring Data ... JPA JDBC DataStax Java Driver Mongo Java Driver RDBMS Cassandra MongoDB Spring Data(1)
  8. 8. Spring Data(2) public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { <S extends T> S save(S entity); T findOne(ID primaryKey); Iterable<T> findAll(); Long count(); void delete(T entity); boolean exists(ID primaryKey); // … more functionality omitted. }
  9. 9. Spring Data(3) public interface PersonRepository extends Repository<User, Long> { … } List<Person> findByLastname(String lastname); public class SomeClient { @Autowired private PersonRepository repository; public void doSomething() { List<Person> persons = repository.findByLastname("Matthews"); } }
  10. 10. Spring Data(4) interface UserRepositoryCustom { public void someCustomMethod(User user); } class UserRepositoryImpl implements UserRepositoryCustom { public void someCustomMethod(User user) { // Your custom implementation } } public interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom { // Declare query methods here }
  11. 11. Spring Data(5) Spring Data JPA @Query("select u from User u where u.emailAddress = ?1") User findByEmailAddress(String emailAddress); @Query("select u from User u") Stream<User> findAllByCustomQueryAndStream(); Spring Data REST Expose DB through REST (JSON in HAL notation)

×