# MongoDao
是一个mongodb使用封装库
# Maven
```xml
<dependency>
<groupId>cn.finalteam</groupId>
<artifactId>mongodao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
```
# 配置
## 数据连接配置
```java
public static MongoConfig getMongoConfig(){
MongoConfig mongoConfig = new MongoConfig();
mongoConfig.setServerAddress(new ServerAddress("127.0.0.1", 27017));
try {
CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(new PropertiesConfiguration("mongo.cfg"));
String user = config.getString("user");
String passwd = config.getString("passwd");
String dbname = config.getString("dbname");
mongoConfig.setUsername(user);
mongoConfig.setPassword(passwd);
mongoConfig.setDatabaseName(dbname);
} catch (ConfigurationException e) {
e.printStackTrace();
}
MongoClientOptions mongoClientOptions = new MongoClientOptions.Builder()
.socketKeepAlive(true)
.connectTimeout(5000)
.socketTimeout(5000)
.readPreference(ReadPreference.primary())
.connectionsPerHost(5)
.maxWaitTime(5000)
.threadsAllowedToBlockForConnectionMultiplier(30)
.build();
mongoConfig.setMongoClientOptions(mongoClientOptions);
return mongoConfig;
}
```
## 数据操作DAO
```java
public class TeacherDao extends MongoDao<Teacher> {
public TeacherDao(MongoConfig mongoConfig){
super(mongoConfig);
}
//在这里可以扩展dao操作方法
}
```
# CRUD操作
## 插入
```java
Teacher teacher = new Teacher();
teacher.setName("马老师");
teacher.setAge(45);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
teacher.setBirthday(sdf.parse("1972-01-01").getTime());
Map<String, List<Student>> classes = new HashMap<String, List<Student>>();
List<Student> students = new ArrayList<Student>();
for(int i = 0; i < 10; i++){
Student student = new Student();
student.setName("彭学生" + i);
student.setAge(new Random().nextInt(10) + 10);
students.add(student);
}
classes.put("计高071", students);
teacher.setClasses(classes);
teacher.setMan(true);
List<String> courses = new ArrayList<String>();
courses.add("计算机应用");
courses.add("C语言程序设计");
teacher.setCourse(courses);
teacherDao.insertOne(teacher);
```
## 查询
```java
Bson filter = Filters.eq("name", "马老师");
List<Teacher> teacherList = teacherDao.find(filter);
System.out.println(JSON.toJSONString(teacherList));
```
## 修改
```java
Bson filter = Filters.eq("name", "马老师");
Document document = new Document();
document.put("isMan", false);
long l = teacherDao.updateOne(filter, document);
```
## 删除
```java
Bson filter = Filters.eq("name", "马老师");
long count = teacherDao.delete(filter);
```
没有合适的资源?快使用搜索试试~ 我知道了~
mongodb资源:MongoDB的基于DAO的基本操作
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共20个文件
java:17个
xml:1个
md:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 1 0 下载量 158 浏览量
2024-07-06
15:31:03
上传
评论
收藏 21KB ZIP 举报
温馨提示
mongodb资源:MongoDB的基于DAO的基本操作
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 20 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/436d7efc72e54325aab8a84a9c64992d_xiongyajun123.jpg!1)
天`南
- 粉丝: 1286
- 资源: 270
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)