Quick Start
MongoPlus is introduced into your project as a third-party component.
We will demonstrate the powerful features of Mongo-Plus through a simple demo. Before that, we assume you have:
- A Java development environment and the corresponding IDE
- Familiarity with Spring Boot
- Familiarity with Maven
We have a User
collection with the following structure:
id | name | age | |
---|---|---|---|
1 | Jone | 18 | test1@163.com |
2 | Jack | 20 | test2@163.com |
3 | Tom | 28 | test3@163.com |
4 | Sandy | 21 | test4@163.com |
5 | Billie | 24 | test5@163.com |
Question
What do we need to do to implement CRUD for this table using Mongo-Plus from scratch?
Initialize Project
Create an empty Spring Boot project.
TIP
You can use Spring Initializer to quickly initialize a Spring Boot project.
Add Dependencies
Add the Spring Boot Starter parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6+ version</version>
<relativePath/>
</parent>
Add spring-boot-starter
, spring-boot-starter-test
, and mongo-plus-boot-starter
dependencies:
<dependencyManagement>
<dependencies>
<!-- mongoplus-bom -->
<dependency>
<groupId>com.mongoplus</groupId>
<artifactId>mongo-plus-bom</artifactId>
<version>2.1.9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mongoplus dependency -->
<dependency>
<groupId>com.mongoplus</groupId>
<artifactId>mongo-plus-boot-starter</artifactId>
</dependency>
</dependencies>
Configuration
Add MongoPlus configuration in application.yml
:
# DataSource Config
mongo-plus:
data:
mongodb:
host: 127.0.0.1 # ip
port: 27017 # port
database: test # database name
username: test # username, optional if not set (no need to encode special characters like @ or !)
password: test # password, optional if not set (no need to encode special characters like @ or !)
authenticationDatabase: admin # authentication database
connectTimeoutMS: 50000 # max time to wait to open a connection in milliseconds
For older Spring Boot versions like 2.1, 2.2, extra steps are required
POM file: Spring Boot controls the MongoDB driver version, so you need to exclude it and then re-import:
<dependency>
<groupId>com.mongoplus</groupId>
<artifactId>mongo-plus-boot-starter</artifactId>
<version>latest version</version>
<exclusions>
<exclusion>
<artifactId>bson</artifactId>
<groupId>org.mongodb</groupId>
</exclusion>
<exclusion>
<artifactId>mongodb-driver-core</artifactId>
<groupId>org.mongodb</groupId>
</exclusion>
<exclusion>
<artifactId>mongodb-driver-sync</artifactId>
<groupId>org.mongodb</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<artifactId>mongodb-driver-sync</artifactId>
<groupId>org.mongodb</groupId>
<version>5.1.2</version>
</dependency>
Main class: To adapt MongoTemplate, OverrideMongoConfiguration
is configured, but it's incompatible with older versions, so simply exclude it:
// Exclude OverrideMongoConfiguration
@SpringBootApplication(exclude = OverrideMongoConfiguration.class)
// If there are errors related to MongoPropertyConfiguration.class, exclude it as well
//@SpringBootApplication(exclude = {OverrideMongoConfiguration.class, MongoPropertyConfiguration.class})
public class MongoPlusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MongoPlusDemoApplication.class, args);
}
}
Code
Create entity class User.java
(using Lombok to simplify code)
TIP
This demonstrates using the entity class approach.
@Data
public class User {
@ID // Mark this field as MongoDB _id using ID annotation or extend BaseModelID
private String id;
private String name;
private Long age;
private String email;
}
Create UserService
interface and UserServiceImpl
class like MyBatis-Plus:
public interface UserService extends IService<User> {
}
public class UserServiceImpl extends ServiceImpl<User> implements UserService {
}
Usage
Add a test class to verify functionality:
@SpringBootTest
public class SampleTest {
@Autowired
private UserService userService;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userService.list();
userList.forEach(System.out::println);
}
}
Console output:
User(id=1, name=Jone, age=18, email=test1@mongoplus.com)
User(id=2, name=Jack, age=20, email=test2@mongoplus.com)
User(id=3, name=Tom, age=28, email=test3@mongoplus.com)
User(id=4, name=Sandy, age=21, email=test4@mongoplus.com)
User(id=5, name=Billie, age=24, email=test5@mongoplus.com)
TIP
For the full code example, see: Spring Boot Demo
Summary
With these simple steps, we have implemented CRUD functionality for the User table!
As you can see, integrating Mongo-Plus
is very simple — just include the starter dependency.
But Mongo-Plus is far more powerful than these features. To explore its full capabilities, continue reading!