Giter Club home page Giter Club logo

java-spring-data-mongo's Introduction

Java-Spring-Data-Mongo wiki!

This is the example of how java interact with mongoDB without using Spring boot!

Dependency

  • Spring data mongo 1.6.3.RELEASE

How To Use

You Must Understand

  • maven, used for Object Mapping (Dependency)
  • mongoDB, Non-relational Database
  • simple DAO (Data Access Object) Pattern
  • Spring data mongo

Description

This is just a simple application on how java interact with mongoDB. The basic concept of spring data (mongo) are: (we talk in this application context)

  • the model itself (Person.java)
  • the repository which is Spring DAO for accessing the mongoDB
  • the Service (RegistrationBean.Java) in this case we use @component rather than @service
  • XML as the "descriptor" on how java using spring data (SpringContext.xml)

This application could use mongoDB in 2 ways, using MongoTemplate or Repository concept. This application will work by reading the SpringContext as it will initialize the connection and database name for the mongoDB. The MongoTemplate will works automatically as it is the standart method to use the mongoDB using insert, update, delete, etc. The Repository concept will work by using the MongoRepository interface (DAO) and a service to access the method. there is 2 ways to determine on how to use this concept, first using the manual method to determine where the repository located and referenced those repository to the bean and call it at runtime or (the nice way!) using @autowired and it will be handled automatically.

How it looks on the manual method:

	<mongo:repositories base-package="com.mongotest2.repositories" >
		<mongo:repository id="personRepository" mongo-template-ref="mongoTemplate"/>
	</mongo:repositories>
	
	<bean id="registrationBean" class="com.mongotest2.beans.RegistrationBean">
		<property name="repository" ref="personRepository" />
	</bean>
  • first you should define the bean
  • define the repository and its id
  • connect to the property name in service (registrationBean.java)

And for the automatic method:

	<mongo:repositories base-package="com.mongotest2.repositories" >
	</mongo:repositories>
	
	<context:component-scan base-package="com.mongotest2.beans">
	</context:component-scan>
  • Only need to define the component-scan and the @autowired do the rest.

At runtime (TestUnit.java) this application will read the XML config (SpringContext.xml), getBean method will call the bean and its done.

do not f*****g ever forget to add constructor even though its empty to the model!!

Any question or improvement for this explanation is welcome :)

java-spring-data-mongo's People

Contributors

afbrilian 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.