Hands-on NoSQL <br />MongoDB & Spring Integration<br />http://automateddeveloper.blogspot.com<br />
<ul><li>I’m a professional Software Engineer based in the UK.
Neither I, nor this presentation, are in any way affiliated with SpringSource or MongoDB
Still want more?</li></ul>http://automateddeveloper.blogspot.com<br />http://www.thirdmindmedia.co.uk<br />info@thirdmindm...
What is NoSQL?<br /><ul><li>NoSQL means “Not Only SQL”
The NoSQL umbrella covers a range of non-relational databases, including key-value, document , wide column store and graph...
Many leading web 2.0 sites now use NoSQL implementations:</li></ul>Facebook – Cassandra (originally developed in-house and...
In the News<br />4,850%<br />Percent increase in NoSQL jobs since November 2009 as reported by SimplyHired.com<br />$14mil...
MongoDB<br /><ul><li>An Open Source Document oriented database
Dynamic schemas, storing data in JSON style documents that allows for addition of data on the fly
Supports indexing of documents
Auto-sharding supported, providing full horizontal scaling</li></li></ul><li>MongoDB & Spring<br /><ul><li>Core Spring and...
As more and more web sites need to be able to handle huge amounts of data, there will be an increased need to be able to b...
Getting Started<br /><ul><li>This overview does not cover details of setting up a Spring MVC app, and will assume a workin...
Only the configuration to connect an existing Spring MVC app will be covered in detail
Before proceeding, install MongoDB – full details are available: http://www.mongodb.org/display/DOCS/Quickstart
If you plan to follow the walkthrough with the source code, it is provided as an Eclipse/Maven project and uses the M2Ecli...
Using Spring Data we can model ourdomain using POJOsand annotations.</li></li></ul><li>Domain Objects<br /><ul><li>@Docume...
@Id on any member variable to be used as an identifier for the object (must be String/int/long)
The @Id field must either be named “id”, or the annotation used in combination with a MappingConverter</li></li></ul><li>D...
In our Resume example, only our top level “Resume” object needs to be annotated and we will only be persisting/retrieving ...
Upcoming SlideShare
Loading in...5
×

MongoDB & Spring MVC Integration

22,938

Published on

Introduction to NoSQL and MongoDB along with step by step walkthrough on how to integrate MongoDB with the Spring MVC framework. Full source code available for example project from http://automateddeveloper.blogspot.com

Published in: Technology, Education
1 Comment
18 Likes
Statistics
Notes
  • nice one. application development made easy, no pain of object to relation mapping.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
22,938
On Slideshare
0
From Embeds
0
Number of Embeds
30
Actions
Shares
0
Downloads
379
Comments
1
Likes
18
Embeds 0
No embeds

No notes for slide

Transcript of "MongoDB & Spring MVC Integration"

  1. 1. Hands-on NoSQL <br />MongoDB & Spring Integration<br />http://automateddeveloper.blogspot.com<br />
  2. 2. <ul><li>I’m a professional Software Engineer based in the UK.
  3. 3. Neither I, nor this presentation, are in any way affiliated with SpringSource or MongoDB
  4. 4. Still want more?</li></ul>http://automateddeveloper.blogspot.com<br />http://www.thirdmindmedia.co.uk<br />info@thirdmindmedia.co.uk<br />
  5. 5. What is NoSQL?<br /><ul><li>NoSQL means “Not Only SQL”
  6. 6. The NoSQL umbrella covers a range of non-relational databases, including key-value, document , wide column store and graph based databases</li></li></ul><li>Why NoSQL?<br /><ul><li>NoSQL’s recent increase in popularity being largely driven by the explosion in amount of data that is now being created/captured on the web
  7. 7. Many leading web 2.0 sites now use NoSQL implementations:</li></ul>Facebook – Cassandra (originally developed in-house and later open-sourced<br />Twitter – Hadoop & Cassandra<br />Google – BigTable<br />Digg – Cassandra<br />FourSquare - MongoDB<br />
  8. 8. In the News<br />4,850%<br />Percent increase in NoSQL jobs since November 2009 as reported by SimplyHired.com<br />$14million<br />Amount raised in Series C Funding last week by CouchBase for further investment in to NoSQL solutions(Aug2011)<br />8TB<br />Amount of data generated everyday in Twitter, and needs to be supported by their NoSQL solution (figure from 2010)<br />
  9. 9. MongoDB<br /><ul><li>An Open Source Document oriented database
  10. 10. Dynamic schemas, storing data in JSON style documents that allows for addition of data on the fly
  11. 11. Supports indexing of documents
  12. 12. Auto-sharding supported, providing full horizontal scaling</li></li></ul><li>MongoDB & Spring<br /><ul><li>Core Spring and the SpringMVC framework areenterprise standard Javalibraries for building professional web sites
  13. 13. As more and more web sites need to be able to handle huge amounts of data, there will be an increased need to be able to build professional sites incorporating NoSQL solutions</li></li></ul><li>How Spring & MongoDB Fit Together<br /><ul><li>Spring enables complete configuration of the Controller, Service, DAO Layer and facilitates connection to Mongo DB</li></ul>Servlet Engine (e.g. Tomcat)<br />Controller Layer<br />Service Layer<br />DAO Layer<br />Request/Response<br />Front Controller (Spring Dispatcher)<br />iRepository<br />Mongo DB<br />Controller<br />Mongo Repository<br />
  14. 14. Getting Started<br /><ul><li>This overview does not cover details of setting up a Spring MVC app, and will assume a working knowledge of the framework
  15. 15. Only the configuration to connect an existing Spring MVC app will be covered in detail
  16. 16. Before proceeding, install MongoDB – full details are available: http://www.mongodb.org/display/DOCS/Quickstart
  17. 17. If you plan to follow the walkthrough with the source code, it is provided as an Eclipse/Maven project and uses the M2Eclipse plugin</li></li></ul><li>Domain Objects<br /><ul><li>Similar to using JPA(Hibernate, etc) the first step is to model the underlying Domain objects you want to use to model the applications underlying meta-data.
  18. 18. Using Spring Data we can model ourdomain using POJOsand annotations.</li></li></ul><li>Domain Objects<br /><ul><li>@Document annotation on the root Document object
  19. 19. @Id on any member variable to be used as an identifier for the object (must be String/int/long)
  20. 20. The @Id field must either be named “id”, or the annotation used in combination with a MappingConverter</li></li></ul><li>Domain Objects<br /><ul><li>Unlike traditional JPA models, Document based models will often have a root object, and will often contain nested objects, which do not need to be annotated
  21. 21. In our Resume example, only our top level “Resume” object needs to be annotated and we will only be persisting/retrieving at this level (Document based DB makes sense for this model, as a “ResumePage” only makes sense within the context of a “Resume” and not as an object in itself)
  22. 22. Using JPA, all three objects would be annotated “@Entity” and all could be queried as independent objects</li></ul>@Document public class Resume@Idprivate String id;<br />public class ResumePage<br />public class PageSection<br />
  23. 23. Data Access – The Repository<br /><ul><li>Core CRUD functionality can be achieved out-of-the-box by extending the “MongoRepository” class
  24. 24. This provides save(), findAll(), findOne() and delete() methods that can be used on our @Document object (in our case “Resume.java”)</li></li></ul><li>Data Access – The Repository<br /><ul><li>Our Repository interface can easily be extended to include custom methods, and the MongoTemplate class provides an API to create custom queries:mongoTemplate.find(“COLLECTION_NAME”, new Query(Criteria.where(“name”).is(searchTerm)), Resume.class);
  25. 25. Criteria allows querying by any field name in a MongoDB Document</li></li></ul><li>Service Layer<br /><ul><li>The majority of the MongoDB implementation is abstracted to the Data Access layer through the MongoRepository
  26. 26. There is no real change in the Service or Controller Layers to the normal Spring MVC approach
  27. 27. In the Service class we can just auto-wire the Repository Interface in to the class just like we would normally with the DAOs</li></li></ul><li>Configuration<br /><!-- Mongo Configuration --><br /><mongo:repositories base-package="com.tmm.nosql.mongodb.repo" /><br /><bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"><br /> <constructor-arg ref="mongoDbFactory" /><br /></bean><br /><!-- local config --><br /><mongo:db-factory id="mongoDbFactory" dbname="resume_db“<br /> host="localhost" port="27017"/><br />1. Define package location for the Repository<br />2. Define MongoTemplate bean <br />3. Define the Mongo DB Factory with the host name, port, dbname (can also provide username and password if needed)<br />
  28. 28. Deploying to CloudFoundry<br /><ul><li>Sign up for your free CloudFoundry account (currently in Beta, so it may take time for your account details) at http://www.cloudfoundry.com/
  29. 29. Install the latest STS Eclipse build and CloudFoundry extensions (full details here: http://blog.springsource.com/2011/04/13/using-cloud-foundry-from-sts/</li></li></ul><li>App Config for CloudFoundry<br /><ul><li>A change to your application config is needed to support access to CloudFoundry MongoDB service
  30. 30. Notice the only change is the “mongoDbFactory” bean is being instantiated with no constructor arguments – this will all be injected by Cloud Foundry</li></li></ul><li>Configuring CloudFoundry<br />In the Servers tab, double-click the deployed application to open the Applications tab<br />In the Services section of the Applications tab, select the “Add Service” button, select MongoDB and name the service appropriately<br />Once the MongoDB service has been created, click and drag it in to the “Application Services” tab<br />
  31. 31. Thoughts<br /><ul><li>NoSQL is growing in popularity but is not intended to be a replacement for RDBMS
  32. 32. NoSQL should be considered as another tool in the arsenal – RDBMS is not going anywhere!
  33. 33. Database technology choice should be driven by the requirements of the problem – Document oriented have specific applications</li></li></ul><li>Appendix<br /><ul><li>Full source code for the application is available from http://automateddeveloper.blogspot.com
  34. 34. Feel free to drop me an email or come by to the blog and leave a comment if you have any questions or feedback</li>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×