Springboot+mongodb Example

Source: Internet
Author: User
Tags bind mongodb mongodb example
First, the environment

1. Maven 3.3.9

2. Eclipse

3, Springboot 1.5.6

4, Mongo 3.4.6 second, the development

1, a new Springboot project, the introduction of 1.5.6 dependency.

2, the project structure is as follows

3. Add the following dependencies in the Pom

<!--Spring-boot-starter-data-mongodb--
        <dependency>
            <groupId> Org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-data-mongodb</ Artifactid>
        </dependency>

4. Modify the APPLICATION.YML configuration file

Spring:
  data:
    MongoDB:
      uri:mongodb://localhost:27017/boot_mongo
  Application:
    Name: Mongoweb
server:
  port:8888

5, writing entity classes User.java

Package Com.mongo.mongoweb.model;

Import java.io.Serializable;

Import Org.springframework.data.annotation.Id; /** * User entity class * * Author: Kehaojie July 29, 2017 * * public class User implements Serializable {/** * */pri

    Vate static final Long serialversionuid = 1L;
    @id this annotation to correspond to MONGO's _id this field @Id private String _id;
    private int id;
    private String name;

    private int age;
        Public User (string _id, int ID, string name, Int. age) {this._id = _id;
        This.id = ID;
        THIS.name = name;
    This.age = age;
        } public User (int ID, String name, Int. age) {this.id = ID;
        THIS.name = name;
    This.age = age;
    } public int getId () {return id;
    } public void setId (int id) {this.id = ID;
    } public String get_id () {return _id;
    } public void set_id (String _id) {this._id = _id;
    } public String GetName () {return name;

    }public void SetName (String name) {this.name = name;
    Public User () {super ();
    } public int Getage () {return age;
    public void Setage (int.) {this.age = age; } public String toString () {return ' User [_id= + _id + ', id= "+ ID +", name= "+ name +", age= "+ Age +"
    ]";
 }

}

6, write Userdao

Package Com.mongo.mongoweb.dao;

Import java.util.List;

Import org.springframework.data.domain.Pageable;

Import Com.mongo.mongoweb.model.User;

/**
 * Userdao Interface Definition
 * 
 * Author: Kehaojie July 30, 2017 * * Public
interface Userdao {

    list<user> FindAll ();

    User GetUser (Integer ID);

    void update (user user);

    void Insert (user user);

    void Insertall (list<user> users);

    void remove (Integer ID);

    List<user> findbypage (user user, pageable pageable);

}

7, write Userdaoimpl

Package Com.mongo.mongoweb.daoimpl;

Import java.util.List;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.data.domain.Pageable;
Import Org.springframework.data.mongodb.core.MongoTemplate;
Import Org.springframework.data.mongodb.core.query.Criteria;
Import Org.springframework.data.mongodb.core.query.Query;
Import Org.springframework.data.mongodb.core.query.Update;

Import Org.springframework.stereotype.Repository;
Import Com.mongo.mongoweb.dao.UserDao;

Import Com.mongo.mongoweb.model.User;
     /** * Interface Implementation * * Author: Kehaojie July 30, 2017 */@Repository ("Userdao") public class Userdaoimpl implements Userdao {/**

    * Automatically injected by springboot, the default configuration will produce mongotemplate this bean */@Autowired private mongotemplate mongotemplate; /** * Find all */@Override public list<user> FindAll () {return Mongotemplate.findall (user.cl
    );


}/** * Gets the object by ID */@Override public User getUser (Integer ID) {
        Return Mongotemplate.findone (New Query (Criteria.where ("id"). is (ID)), user.class);
    }/** * Insert a user */@Override public void Insert (user user) {mongotemplate.insert (user); /** * Delete a user by ID */@Override public void Remove (Integer id) {criteria = Crit  
        Eria.where ("id"). is (ID);
        Query query = new query (criteria);
    Mongotemplate.remove (Query,user.class); }/** * Page Lookup * * User representative filter Condition * * pageable for paging bean */@Override public list<
        User> findbypage (user user, pageable pageable) {query query = new query (); if (user! = null && user.getname ()! = NULL) {//Fuzzy query = ' new query ' (Criteria.where ("Nam
        E "). Regex (" ^ "+ user.getname ()));
        } list<user> List = Mongotemplate.find (Query.with (pageable), user.class);
    return list; }/** * Updated by ID */@Override
    public void update (user user) {criteria = Criteria.where ("id"). is (User.getid ());
        Query query = new query (criteria);
        Update update = update.update ("name", User.getname ()). Set ("Age", User.getage ());
    Mongotemplate.updatemulti (query, update, user.class); }/** * Insert a collection */@Override public void Insertall (list<user> users) {MONGOTEMPLATE.I
    Nsertall (users);
 }

}

8, write UserService

Package com.mongo.mongoweb.service;

Import java.util.List;

Import org.springframework.data.domain.Pageable;

Import Com.mongo.mongoweb.model.User;

Public interface UserService {

    list<user> findAll ();

    User GetUser (Integer ID);

    void update (user user);

    void Insert (user user);

    void Insertall (list<user> users);

    void remove (Integer ID);

    List<user> findbypage (User user,pageable pageable);

9, write Userserviceimpl

Package Com.mongo.mongoweb.serviceimpl;

Import java.util.List;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.data.domain.Pageable;

Import Org.springframework.stereotype.Service;
Import Com.mongo.mongoweb.dao.UserDao;
Import Com.mongo.mongoweb.model.User;

Import Com.mongo.mongoweb.service.UserService;

    @Service ("UserService") public class Userserviceimpl implements userservice{@Autowired private Userdao Userdao;
    @Override public list<user> FindAll () {return userdao.findall ();
    } @Override Public User getUser (Integer ID) {return userdao.getuser (ID);
    } @Override public void update (user user) {userdao.update (user);
    } @Override public void Insert (user user) {userdao.insert (user);
    } @Override public void Insertall (list<user> users) {Userdao.insertall (users);
    } @Override public void remove (Integer ID) {    Userdao.remove (ID); } @Override Public list<user> findbypage (user user, pageable pageable) {return userdao.findbypage (
    User, pageable);
 }


}

10, writing Usercontroller controller

Package Com.mongo.mongoweb.controller;
Import java.util.ArrayList;

Import java.util.List;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.data.domain.PageRequest;
Import org.springframework.data.domain.Pageable;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RestController;
Import Com.mongo.mongoweb.model.User;

Import Com.mongo.mongoweb.service.UserService; /** * User Controller * * Author: Kehaojie July 30, 2017 */@RestController @RequestMapping ("/user") public class Usercontroller {@

    autowired private UserService UserService;
    @RequestMapping ("/get/{id}") Public User getUser (@PathVariable int id) {return userservice.getuser (ID);
        } @RequestMapping ("/delete/{id}") Public String Delete (@PathVariable int id) {userservice.remove (ID);
    Return "Delete sucess"; } @RequestMappING ("/add") public String Insert () {User User =new User (16, "" +16, 16);
        Userservice.insert (user);
    return "sucess"; 
        } @RequestMapping ("/insert") public String Insertall () {list<user> List = new arraylist<> ();
        for (int i = ten; I < i++) {List.add (new User (I, "" + I, i));
        } userservice.insertall (list);
    return "sucess";
    } @RequestMapping ("/find/all") public list<user> Find () {return userservice.findall ();
        } @RequestMapping ("/find/{start}") Public list<user> findbypage (@PathVariable int start,user User) {
        pageable pageable=new pagerequest (Start, 2);
    return userservice.findbypage (user, pageable); } @RequestMapping ("/update/{id}") Public String update (@PathVariable int id) {User user =new User (ID, ""
        +1, 1);
        Userservice.update (user);
    return "sucess";
 }
}

11, and then start the operation can, source address:

Https://git.oschina.net/lgr123/springbootMongoDB.git
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.