如何用spring集成mongodb实现文件上传
答案:2 悬赏:30 手机版
解决时间 2021-01-25 00:23
- 提问者网友:刺鸟
- 2021-01-24 14:37
如何用spring集成mongodb实现文件上传
最佳答案
- 五星知识达人网友:持酒劝斜阳
- 2021-01-24 16:08
首先要把必要的MongoDB需要的jar加进项目中
定义mongoDB的bean
[html] view plain copy
自定义实现mongodb增删改实体类
[html] view plain copy
定义mongoClient基础类
[java] view plain copy
public class MongoDB {
private MongoClient mongoClient;
private String dbName;
public DB getDB() {
return mongoClient.getDB(dbName);
}
public MongoClient getMongoClient() {
return mongoClient;
}
public void setMongoClient(MongoClient mongoClient) {
this.mongoClient = mongoClient;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
}
定义mongodb操作Dao类
[java] view plain copy
public T save(T bean) {
String beanJson = JsonUtil.getJSONString(bean);
DBCollection collection = mongoDB.getDB().getCollection(clazz.getSimpleName());
collection.save((DBObject)JSON.parse(beanJson));
return bean;
}
public void remove(String id) {
DBCollection collection = mongoDB.getDB().getCollection(clazz.getSimpleName());
BasicDBObject doc = new BasicDBObject();
doc.put("_id", id);
collection.remove(doc);
}
public void update(BasicDBObject query, BasicDBObject newDoc) {
DBCollection collection = mongoDB.getDB().getCollection(clazz.getSimpleName());
collection.update(query, newDoc);
}
定义保存文件类
[java] view plain copy
public void save(InputStream in, String id, String fileName, String contentType, String downloadName) {
GridFS fs = new GridFS(mongoDB.getDB(), this.getClass().getSimpleName());
GridFSInputFile fsFile = fs.createFile(in);
fsFile.setId(id);
fsFile.setFilename(fileName);
fsFile.setContentType(contentType);
fsFile.put("downloadName", downloadName);
fsFile.save();
}
public void remove(String id) {
GridFS fs = new GridFS(mongoDB.getDB(), this.getClass().getSimpleName());
BasicDBObject query = new BasicDBObject("_id", id);
fs.remove(query);
}
public void batchRemove(String... ids) {
GridFS fs = new GridFS(mongoDB.getDB(), this.getClass().getSimpleName());
for(String id : ids){
BasicDBObject query = new BasicDBObject("_id", id);
fs.remove(query);
}
}
定义mongoDB的bean
[html] view plain copy
自定义实现mongodb增删改实体类
[html] view plain copy
定义mongoClient基础类
[java] view plain copy
public class MongoDB {
private MongoClient mongoClient;
private String dbName;
public DB getDB() {
return mongoClient.getDB(dbName);
}
public MongoClient getMongoClient() {
return mongoClient;
}
public void setMongoClient(MongoClient mongoClient) {
this.mongoClient = mongoClient;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
}
定义mongodb操作Dao类
[java] view plain copy
public T save(T bean) {
String beanJson = JsonUtil.getJSONString(bean);
DBCollection collection = mongoDB.getDB().getCollection(clazz.getSimpleName());
collection.save((DBObject)JSON.parse(beanJson));
return bean;
}
public void remove(String id) {
DBCollection collection = mongoDB.getDB().getCollection(clazz.getSimpleName());
BasicDBObject doc = new BasicDBObject();
doc.put("_id", id);
collection.remove(doc);
}
public void update(BasicDBObject query, BasicDBObject newDoc) {
DBCollection collection = mongoDB.getDB().getCollection(clazz.getSimpleName());
collection.update(query, newDoc);
}
定义保存文件类
[java] view plain copy
public void save(InputStream in, String id, String fileName, String contentType, String downloadName) {
GridFS fs = new GridFS(mongoDB.getDB(), this.getClass().getSimpleName());
GridFSInputFile fsFile = fs.createFile(in);
fsFile.setId(id);
fsFile.setFilename(fileName);
fsFile.setContentType(contentType);
fsFile.put("downloadName", downloadName);
fsFile.save();
}
public void remove(String id) {
GridFS fs = new GridFS(mongoDB.getDB(), this.getClass().getSimpleName());
BasicDBObject query = new BasicDBObject("_id", id);
fs.remove(query);
}
public void batchRemove(String... ids) {
GridFS fs = new GridFS(mongoDB.getDB(), this.getClass().getSimpleName());
for(String id : ids){
BasicDBObject query = new BasicDBObject("_id", id);
fs.remove(query);
}
}
全部回答
- 1楼网友:深街酒徒
- 2021-01-24 17:47
mongodb struts spring集成使用方法:
1、 定义mongo对象,对应的是mongodb官方jar包中的mongo,replica-set设置集群副本的ip地址和端口
2、定义mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建
3、 mongodb的主要操作对象,所有对mongodb的增删改查的操作都是通过它完成
4、 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯