No Entity Class Scenario
No Entity Class Scenario
Considering cases where the structure is too complex and there is no entity class, what should we do? Use:
MongoPlusMapMapperBaseMapper
- Operations and service inheritance are exactly the same
- Simply inject
MongoPlusMapMapperBaseMapper
to use - Returns a Map structure
Here we have a User
collection with the following structure:
id | name | age | |
---|---|---|---|
1 | Jone | 18 | test1@mongoplus.com |
2 | Jack | 20 | test2@mongoplus.com |
3 | Tom | 28 | test3@mongoplus.com |
4 | Sandy | 21 | test4@mongoplus.com |
5 | Billie | 24 | test5@mongoplus.com |
Question
If we use MongoPlusOperate to implement CRUD for this collection, what should we do?
Initialize Project
Create an empty Spring Boot project.
TIP
You can use Spring Initializer to quickly initialize a Spring Boot project.
Add Dependencies
Introduce the Spring Boot Starter parent project:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6+ version</version>
<relativePath/>
</parent>
Add the dependencies spring-boot-starter
, spring-boot-starter-test
, mongo-plus-boot-starter
:
<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>
<dependency>
<groupId>com.gitee.anwena</groupId>
<artifactId>mongo-plus-boot-starter</artifactId>
<version>latest version</version>
</dependency>
</dependencies>
Configuration
Add MongoPlus related configuration in the application.yml
file:
# DataSource Config
mongo-plus:
data:
mongodb:
host: 127.0.0.1 # IP
port: 27017 # Port
database: test # Database name
username: test # Username, optional
password: test # Password, optional
authenticationDatabase: admin # Authentication database
connectTimeoutMS: 50000 # Maximum time (ms) to wait for a connection before timeout
Coding
Add a test class for functionality testing:
@SpringBootTest
public class SampleTest {
@Autowired
//private MongoPlusMapMapper mongoPlusMapMapper;
private BaseMapper baseMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
// List<Map<String,Object>> userList = mongoPlusMapMapper.list("user");
List<Map<String, Object>> userList = baseMapper.list("asd", new TypeReference<Map<String, Object>>() {});
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 complete code example, please visit: Spring Boot Example
Summary
With just a few simple steps, we have implemented CRUD functionality for the User collection!
From the steps above, we can see that integrating Mongo-Plus
is very simple, requiring only the introduction of the starter project.
But the power of Mongo-Plus goes far beyond this. Want to learn more about the powerful features of Mongo-Plus? Keep reading!