标题:
java-spring与mongodb的整合方式一 自动注入xml(2)
[打印本页]
作者:
look_w
时间:
2019-4-19 19:29
标题:
java-spring与mongodb的整合方式一 自动注入xml(2)
接口:GenericReposity
package com.mongo.reposity.Impl;
import java.util.List;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import com.mongo.reposity.GenericReposity;
import com.mongodb.Mongo;
import com.mongodb.WriteResult;
public class GenericReposityImpl<T> extends MongoTemplate implements GenericReposity<T> {
public GenericReposityImpl(Mongo mongo, String databaseName) {
super(mongo, databaseName);
}
public List<T> getAllObjects(Class<T> clazz) {
return findAll(clazz);
}
public void saveObject(Object obj) {
insert(obj);
}
public void saveObjects(List<T> objects) {
for (T t : objects) {
insert(t);
}
}
public T getObject(Class<T> clazz, String id) {
return findOne(new Query(Criteria.where("id").is(id)), clazz);
}
public WriteResult updateObject(Class<T> clazz, String id, String name) {
return updateFirst(new Query(Criteria.where("id").is(id)),
Update.update("name", name), clazz);
}
public void deleteObject(Class<T> clazz, String id) {
remove(new Query(Criteria.where("id").is(id)), clazz);
}
public void createDbCollection(Class<T> clazz) {
if (collectionExists(clazz)) {
createCollection(clazz);
}
}
public void dropDbCollection(Class<T> clazz) {
if (collectionExists(clazz)) {
dropCollection(clazz);
}
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0