Start MONGO mirroring
Docker run--name mongo-container-d-p MONGO
Connect to inside the container
Docker exec-it EB sh
Input: MONGO
Input: Show DBS
Input: Db.stats ()
Download MONGO client: Https://robomongo.org/download
Press CTRL + C and exit to exit the container, enter: Docker PS
Here the auto-mapped port is 32768, open Robo 3T, enter the address, click Test
You can see the library on the left.
Create a new Spring-boot project and add a pom reference
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency& Gt <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-hateoas</artif Actid> </dependency> <!--https://Mvnrepository.com/artifact/org.pegdown/pegdown --<dependency> <groupId>org.pegdown</groupId> <artifactid>pegdown</artifact Id> <version>1.6.0</version> <scope>test</scope> </dependency> <!--https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactid>jacks On-annotations</artifactid> <version>2.9.3</version> </dependency> <dependency> <GROUPID>ORG.SPRINGFRAMEWORK.BOOT&L T;/groupid> <artifactId>spring-boot-starter-test</artifactId> <scope>test</s Cope> </dependency> </dependencies>
View Code
Define User
Package Org.myshsky.mongodemo;import Org.springframework.data.annotation.id;import Org.springframework.data.annotation.persistenceconstructor;import Org.springframework.data.mongodb.core.index.indexed;import Org.springframework.data.mongodb.core.mapping.document;import Javax.validation.constraints.notnull;import Java.util.date;import java.util.hashset;import Java.util.Set; @Document (Collection="User") Public classUser { PublicString getUserId () {returnuserId; } Public voidSetuserid (String userId) { This. UserId =userId; } PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getemail () {returnemail; } Public voidsetemail (String email) { This. email =email; } PublicDate getregistrationdate () {returnregistrationdate; } Public voidsetregistrationdate (Date registrationdate) { This. registrationdate =registrationdate; } PublicSet<string>GetRoles () {returnroles; } Public voidSetroles (set<string>roles) { This. roles =roles; } @IdPrivateString userId; @NotNull @Indexed (Unique=true) PrivateString username; @NotNullPrivateString password; @NotNullPrivateString name; @NotNullPrivateString Email; @NotNullPrivateDate registrationdate=NewDate (); PrivateSet<string> roles=NewHashset<>(); @PersistenceConstructor PublicUser (string userId, string Username, string password, string name, string email, Date registrationdate, set<string>roles) { This. UserId =userId; This. Username =username; This. Password =password; This. Name =name; This. email =email; This. registrationdate =registrationdate; This. roles =roles; } PublicUser () {}}
View Code
Query Interface Userrepository
Package Org.myshsky.mongodemo;import org.springframework.data.mongodb.repository.MongoRepository; Public Interface Userrepository extends mongorepository<user,string> { User findbyusername (String username);}
View Code
Configuration class
Package Org.myshsky.mongodemo;import Com.mongodb.mongo;import com.mongodb.mongoclient;import Com.mongodb.mongocredential;import Com.mongodb.serveraddress;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.annotation.configuration;import Org.springframework.context.annotation.propertysource;import Org.springframework.core.env.environment;import Org.springframework.data.mongodb.config.abstractmongoconfiguration;import Org.springframework.data.mongodb.repository.config.enablemongorepositories;import Java.util.ArrayList;import Java.util.List, @Configuration @enablemongorepositories@propertysource ("classpath:test.properties") Public classTestdatasourceconfig extends Abstractmongoconfiguration {@AutowiredPrivateEnvironment env; @OverrideprotectedString GetDatabaseName () {returnEnv.getrequiredproperty ("Mongo.name"); } @Override PublicMongo Mongo () throws Exception {serveraddress serveraddress=NewServerAddress (Env.getrequiredproperty ("Mongo.host")); List<MongoCredential> credentials=NewArraylist<>(); return Newmongoclient (serveraddress,credentials); }}
View Code
Configuration file Test.properties
#MongoDbmongo. Host=192.168. 31.146 : ${mongo.port}mongo.name=localmongo.port=32768
View Code
Unit Test
Package Org.myshsky.mongodemo;import org.junit.before;import org.junit.test;import org.junit.runner.RunWith; Import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.boot.test.context.springboottest;import Org.springframework.test.context.junit4.springrunner;import Org.springframework.util.assert;import Java.util.date;import java.util.hashset;import java.util.list;import java.util.Set; @RunWith (Springrunner.class) @SpringBootTest Public classmongodemoapplicationtests {Private StaticLogger logger= Loggerfactory.getlogger (mongodemoapplicationtests.class); @Autowired userrepository userrepository; @Before Public voidSetup () {Set<String> roles=NewHashset<>(); Roles.add ("Manage"); User User=NewUser ("1","User","12345678","name","[email protected]",NewDate (), roles); Userrepository.save (user); } @Test Public voidFindAll () {List<User> users=Userrepository.findall (); Assert.notnull (users); for(User user:users) {logger.info ("===user=== userid:{},username:{},pass:{},registrationdate:{}", User.getuserid (), User.getname (), User.getpassword (), user.getregistrationdate ()); } }}
View Code
Run Tests
Viewing through the client
Spring Boot + Docker +mongo