[Open Source] Operate MongoDB Elegantly Using the MyBatisPlus Approach — Simple and Easy to Use

Open SourceProcess Management
2026-06-28 03:27 4 次浏览 • 0 次下载 0KB
[Open Source] Operate MongoDB Elegantly Using the MyBatisPlus Approach — Simple and Easy to Use

1. Project Overview

Use MyBatisPlus-style operations to work with MongoDB elegantly.

2. Open Source License

Licensed under Apache-2.0

3. Screenshots


4. Feature Overview

First, you need an entity class:

import com.anwen.mongo.sql.model.BaseModelID;

/**
 * @TableName: Specifies the corresponding table name. If not configured, the lowercase class name is used by default. Switch data sources through the dataSource property.
 **/
@TableName(value="Corresponding table name",dataSource="slaveName when configuring multiple data sources"/*If dataSource is not configured, the primary data source is used by default*/)
public class User extends BaseModelID {
    @TableField("user_name")//Identifies the corresponding field in the database
  private String username;
  private int status;
  private int age;
  private String gender;
  private Date joinDate;
  private int roleId;
  private String roleName;
}

Make your service interface extend IService.

import com.anwen.mongo.sql.IService;
import org.apache.catalina.User;
//If you are using it together with MyBatisPlus, you only need to inject IService.
public interface MongoServiceImpl extends IService<User> {
        
}

Then make your service implementation class extend ServiceImpl.

import com.anwen.mongo.config.MongoDBConnectProperty;
import com.anwen.mongo.sql.ServiceImpl;
import com.mongodb.MongoClient;
import org.apache.catalina.User;
//If you are using it together with MyBatisPlus, you only need to inject IService.
public class MongoServiceImpl extends ServiceImpl<User> implements MongoService {
    
}

Now you can operate MongoDB using exactly the same code style as MyBatisPlus:

import org.apache.catalina.User;

@RestController
@RequestMapping("/user")
public class UserController {

  @Autowired
  private MongoService mongoService;
  
  //If you are using it together with MyBatisPlus, you only need to inject IService.
//  private IService<User> service;  

  @GetMapping("/index")
  public void index() {
    List<User> userList = mongoService.lambdaQuery().eq(User::getName,"John").ne(User::getUsername,"admin").list();
    mongoService.save(new User());
    mongoService.updateById(new User());
    mongoService.removeById(1);
  }

}

That's it! You can now start and run the application directly. Isn't this exactly the same as using MyBatisPlus? You don't need to spend much time learning a new API.

5. Tech Stack

 Rapid Development

MyBatisPlus eliminates the need to write excessive SQL statements as you would with MyBatis, and Mongo-Plus works the same way!

 Easy Integration

Can be integrated with any Java Web framework, such as Spring Boot, Spring MVC, JFinal, and others.

For Spring Boot projects, simply add the dependency to complete the integration:

<dependency>
    <groupId>com.gitee.anwena</groupId>
    <artifactId>mongo-plus-boot-starter</artifactId>
    <version>1.0.2</version>
</dependency>

Configuration file settings:

mongo-plus:
  data:
    mongodb:
      host: 127.0.0.1
      port: 27017
      database: test

Multi-data-source configuration:

mongo-plus:
  data:
    mongodb:
      host: 127.0.0.1
      port: 27017
      database: test
      username: admin #Optional, omit if not available
      password: admin #Optional, omit if not available
      slaveDataSource[0]:
        slaveName: test1
        host: 127.0.0.1
        port: 27017
        database: database1
        username: admin #Optional, omit if not available
        password: admin #Optional, omit if not available
      slaveDataSource[1]:
        slaveName: test2
        host: 127.0.0.1
        port: 27017
        database: database2
        username: admin #Optional, omit if not available
        password: admin #Optional, omit if not available

 Changelog

v1.0 Basic Usage

#1
资源直链https://gitee.com/anwena/mongo-plus
去下载
星河码客

发现和分享优质开源项目,传承互联网精神,为开发者服务。

关于本站

联系我们

微信/广告合作:ITcloub (备注星河码客)

免责声明:本站资源版权归原作者所有,仅供学习与交流。如有侵权,请通过「关于我们 - 侵权投诉」联系删除。

Powered by Nuxt · Deployed on Cloudflare