Setting up Spring-DATA MongoDB & Spring MVC Project in Intellij

 

Appli­ca­tion Stacks we are going to set­up -

  • Spring Frame­work
  • Spring MVC
  • Spring-Data Mon­goDB
  • Maven

Assum­ing that you have already installed Mon­goDB & Have added a serv­er to your IDE.

Open Intel­lij IDE and click on “New Project”

It will dis­play you a list of frame­works to inte­grate choose only “Spring MVC” There.

Next screen it will ask you for “Cre­ate Project From Tem­plate” check that and click on “Spring MVC”, Click Next.

Then it will ask you appli­ca­tion name & serv­er and base pack­age, fill them accord­ing­ly.

This will cre­ate a basic tem­plate appli­ca­tion in your IDE, now we have to add library for Spring-Data.

Open Pom.xml file and add the fol­low­ing depen­den­cy there -

 

Now right click on project and click on Maven->Download Source, it will down­load the jar files for spring-data-mon­godb.

Now to con­fig­ure spring-data-mon­godb cre­ate new class named “config.MongoConfiguration” , and in this class fol­low the below code -

 

repos­i­to­ry baseP­a­ck­age may show warn­ing for now, but ignore that we are going to cre­ate that in upcom­ming steps.

 

Now lets cre­ate doc­u­ment Enti­ties-

Doc­u­ments are sim­i­lar to table in mysql, so cre­ate a class in doc­u­ment pack­age named User

 

Now lets cre­ate a repos­i­to­ry for this doc­u­ment-

cre­ate a Inter­face  User­Repos­i­to­ry in pack­age repos­i­to­ry such as -

 

It is extend­ing Mon­goRepos­i­to­ry and in gener­ics we are pass­ing two para­me­ter, one is our Entity/Document class and oth­er is our @Id field type so it is User,String

Thats it now lets use this in our con­troller -

Open your Hel­loCon­troller class which is cre­at­ed by intel­lij and use repo­sio­ry such as -

Now if you will see in your mon­godb data­base the record is insert­ed suc­cess­ful­ly

You can per­form oth­er actions such as -

vivek

Web Developer & Server Admin, Skilled in Java , PHP , LAMP, Tomcat, Mongo DB & SQL. Available for freelancing project or Open Source Contribution, Feel free to contact me at contact@viveksoni.net .

You may also like...

  • ErayT

    Hi,

    I have found this arti­cle very clear and basic. It is the best tuto­r­i­al for me. How­ev­er, I am hav­ing some trou­ble although I have under­stood most of the process. I hope you may help me with this prob­lem. I cre­at­ed a new mvc project and run it. It has been served well. After I added Mon­go fea­ture I got these errors:

    ERROR: org.springframework.web.servlet.DispatcherServlet — Con­text ini­tial­iza­tion failed
    java.lang.IllegalArgumentException: Resource­Loader must not be null!

    SEVERE: StandardWrapper.Throwable
    java.lang.IllegalArgumentException: Resource­Loader must not be null!

    SEVERE: Allo­cate excep­tion for servlet appServlet
    java.lang.IllegalArgumentException: Resource­Loader must not be null!

    I can­not relate the prob­lem with any­thing. Do you have any idea?

    • are you still strug­gling with error?
      sor­ry i was not in touch blog since last year