Spring Boot series (eight) using the MongoDB database in spring boot
This article describes the use of the MongoDB database in spring boot, which requires a complete video tutorial of Springboot, click here.
MongoDB is an open source NoSQL document database that replaces traditional table-based relational data with a JSON-formatted schema. Spring Boot provides a lot of convenience for using MongoDB, including Spring-boot-starter-data-mongodb ' starter POM '.
Introducing the SPRING-BOOT-STARTER-DATA-MONGODB package, add the following in the Pom.xml configuration file (based on the Pom.xml file in the previous section, "Spring Boot Build Framework"):
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Inject an automatically configured Org.springframework.data.mongodb.MongoDbFactory to access the MONGO database. By default, the instance will attempt to connect to a MongoDB server using Url:mongodb://localhost/test.
Import Org.springframework.data.mongodb.MongoDbFactory;
Import Com.mongodb.DB;
@Component
public class Mybean {
Private final mongodbfactory MONGO;
@Autowired
Public Mybean (mongodbfactory MONGO) {
This.mongo = MONGO;
}
// ...
public void Example () {
DB db = Mongo.getdb ();
// ...
}
}
You can change the URL by setting Spring.data.mongodb.uri, or specify a host/port. Set the following properties in Application.properties:
Spring.data.mongodb.host=mongoserver
spring.data.mongodb.port=27017
Note: If Spring.data.mongodb.port is not specified, the default port of 27017 will be used. You can simply remove this line from the example above. If you do not use spring Data Mongo, you can inject Com.mongodb.Mongo beans instead of using mongodbfactory. If you want to fully control the creation of a MONGODB connection, you can also declare your own mongodbfactory or MONGO, @Beans.
Mongodbtemplate
Spring Data MONGO provides a mongotemplate class that is similar in design to the jdbctemplate of spring. Like JdbcTemplate, Spring boot automatically configures a bean so you can simply inject it:
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.data.mongodb.core.MongoTemplate;
Import org.springframework.stereotype.Component;
@Component
public class Mybean {
Private final mongotemplate mongotemplate;
@Autowired
Public Mybean (Mongotemplate mongotemplate) {
This.mongotemplate = mongotemplate;
}
// ...
}
Application Integration MongoDB Case
Add the following in the Application.properties configuration file:
Spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test
Multiple IP clusters are configured with the following:
Spring.data.mongodb.uri=mongodb://user:pass@ip1:port1,ip2:port2/database
Create the data entity class with the following code:
public class UserInfo implements Serializable {
Private Long ID;
Private String UserName;
Private String PassWord;
Getter, setter omitted
}
Create entity classes corresponding to the DAO layer, to implement additions and deletions to the operation, the specific code is as follows:
@Component
public class Userinfodaoimpl implements Userinfodao {//Userinfodao custom interface
@Autowired
Private Mongotemplate mongotemplate;
/**
* Create objects
* @param user
*/
@Override
public void Saveuser (UserInfo user) {
Mongotemplate.save (user);
}
/**
* Query objects based on user name
* @param userName
* @return
*/
@Override
Public UserInfo finduserbyusername (String userName) {
Query query=new query (Criteria.where ("UserName"). is (UserName));
UserInfo user = Mongotemplate.findone (query, Userinfo.class);
return user;
}
/**
* Update objects
* @param user
*/
@Override
public void UpdateUser (UserInfo user) {
Query query=new query (criteria.where ("id"). is (User.getid ()));
Update update= new Update (). Set ("UserName", User.getusername ()). Set ("PassWord", User.getpassword ());
The update query returns the first bar of the result set
Mongotemplate.updatefirst (Query,update,userinfo.class);
The update query returns all of the result sets
Mongotemplate.updatemulti (Query,update,userinfo.class);
}
/**
* Delete Objects
* @param ID
*/
@Override
public void Deleteuserbyid (Long id) {
Query query=new query (criteria.where ("id"). is (ID));
Mongotemplate.remove (Query,userinfo.class);
}
}
Simple test method, the code is as follows:
@RunWith (springrunner.class)
@SpringBootTest
public class Userdaotest {
@Autowired
private Userdao Userdao;
@Test
public void Testsaveuser () throws Exception {
user Info user=new UserInfo ();
user.setid (2);
user.setusername ("Passers-by");
user.setpassword ("123456");
userdao.saveuser (user);
&NBSP;}
@Test
public void Finduserbyusername () {
UserInfo user= Userdao . Finduserbyusername ("Passers-by");
SYSTEM.OUT.PRINTLN ("User is" +user);
&NBSP;}
@Test
public void UpdateUser () {
userinfo user=new Userin Fo ();
user.setid (3