Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Spring Data - Lightning Talk

Intro to Spring Data - Lightning Talk

A quick intro to the power of Spring Data

43e9a9d63f7f4f9891c9fcd592b89cfa?s=128

Greg Turnquist

October 06, 2015
Tweet

Transcript

  1. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Introduction to Spring Data Greg Turnquist @gregturn gturnquist@pivotal.io github.com/gregturn
  2. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ GregLTurnquist.com/list
  3. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
  4. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ It’s 2015
  5. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ It’s 2015 Why are we still writing stuff like…
  6. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ It’s 2015 Why are we still writing stuff like… select * from person where person.first_name = %1
  7. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ With Spring Data, you don’t have to!
  8. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
  9. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SQL
  10. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SQL JPL HQL EJBQL CQL CQRS Mongo Query Language ????
  11. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Declaring Repositories 5 public interface PersonRepository extends CrudRepository<Person, Long> { // This interface and a “Person” class is // ALL YOU NEED! }
  12. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Writing Queries 6 public interface PersonRepository extends CrudRepository<Person, Long> { List<Person> findByFirstname(String f) List<Person> findByFirstnameAndLastname(String f, String l) List<Person> findByFirstnameAndAddressZipcode(String f, Zipcode z) List<Person> findTop10ByFirstname(…) // or findFirst10ByFirstname List<Person> findDistinctPeopleByFirstname(…) List<Person> findByFirstnameAndLastnameAllIgnoreCase(…) List<Person> findByFirstnameOrderByLastnameAsc(…) List<Person> findByLastnameIsNull(…) }
  13. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Why stop there? 7 public interface CoolPersonRepo extends CrudRepository<Person, Long> { Stream<Person> findByLastname(String lastname) @Async Future<Person> findByLastname(…) @Async CompletableFuture<Person> findByLastname(…) @Async ListenableFuture<Person> findByLastname(…) }
  14. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Paging and Sorting 8 public interface CoolPersonRepo extends CrudRepository<Person, Long> { Page<Person> findAll(Pageable p) Page<Person> findByFirstname(String f, Pageable p) List<Person> findAll(Sort s) List<Person> findByFirstname(String f, Sort s) Page<Person> findByFirstname(String f, Pageable p, Sort s) }
  15. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Not quite enough? 9 public interface CoolPersonRepo extends CrudRepository<Person, Long> { @Query("select u from User u where u.emailAddress = ?1”) User myCoolCustomQuery(String e) @Query("select u from User u where u.firstname = :firstname”) User findByFirstname(@Param("firstname") String f) @Query("select u from #{#entityName} u where u.lastname = ?1”) List<User> findByLastname(String lastname) List<Person> findAll(Specification spec) }
  16. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What else? § Repository solutions are VERY handy, but not always enough. § Each data store has a custom template • JpaTemplate • MongoTemplate • Neo4jTemplate • RedisTemplate • ….and so forth 10
  17. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Nice JPA stuff, but what about MongoDB, …? § Spring Data supports • JPA • MongoDB • Neo4j • GemFire a.k.a. Apache Geode • Cassandra • Couchbase • Apache Solr • Elasticsearch • Amazon DynamoDB • (Redis is coming) • ….and ANY repository-based data store 11
  18. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Check it out! 12 Visit http://projects.spring.io/spring-data/
  19. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Introduction to Spring Data Greg Turnquist @gregturn gturnquist@pivotal.io github.com/gregturn