配备如高:
application.yml文件形式如高
spring:
data:
mongodb:
host: 19两.168.9.二34 #指定MongoDB管事所在
port: 两7017 #指定端心,默许便为二7017
database: blog #指定运用的数据库(召集)
authentication-database: admin # 登录认证的逻辑库名
username: admin #用户名
password: 1两3456 #暗码
main:
banner-mode: off
server:
port: 8080
大众类如高
IBasicDao类:
package com.zingrow.blog.co妹妹on;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;
public interface IBasicDao<T> {
/**
* 盘问数据
*
* @param query
* @return
*/
List<T> find(Query query);
/**
* 分页查问
*
* @param query
* @param start
* @param size
* @return
*/
List<T> findList(Query query, int start, int size);
/**
* 按照ID盘问
*
* @param id
* @return
*/
T findOne(String id);
/**
* 拔出一条数据
*
* @param entity
*/
void insert(T entity);
/**
* 更新数据
*
* @param query
* @param update
* @return
*/
UpdateResult update(Query query, Update update);
/**
* 依照真体类增除了数据
*
* @param entity
* @return
*/
DeleteResult delete(T entity);
/**
* 按照query增除了数据
*
* @param query
* @return
*/
DeleteResult remove(Query query);
long count();
long count(Query query);
void save(T entity);
List<T> findAll();
List<T> findByKeyAndValue(String key, String Value);
List<T> findByKeyAndValue(String key, String Value, int start, int limit);
void upsert(Query query, Update update);
MongoTemplate getMongoTemplate();
}
其完成类BasicDao形式如高
package com.zingrow.blog.co妹妹on;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
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 java.lang.reflect.ParameterizedType;
import java.util.List;
public class BasicDao<T> implements IBasicDao<T> {
private Class<T> clazz;
public BasicDao() {
ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
clazz = (Class<T>) type.getActualTypeArguments()[0];
}
@Autowired
private MongoTemplate mongoTemplate;
@Override
public List<T> find(Query query) {
return mongoTemplate.find(query, clazz);
}
@Override
public List<T> findList(Query query, int start, int size) {
query.with(Sort.by(new Sort.Order(Sort.Direction.ASC, "_id")))
.skip(start)
.limit(size);
return mongoTemplate.find(query, clazz);
}
@Override
public T findOne(String id) {
Query query = new Query();
query.addCriteria(new Criteria("_id").is(id));
return mongoTemplate.findOne(query, clazz);
}
@Override
public void insert(T entity) {
mongoTemplate.insert(entity);
}
@Override
public UpdateResult update(Query query, Update update) {
return mongoTemplate.updateMulti(query, update, clazz);
}
@Override
public DeleteResult delete(T entity) {
return mongoTemplate.remove(entity);
}
@Override
public DeleteResult remove(Query query) {
return mongoTemplate.remove(query, clazz);
}
public DeleteResult remove(String id) {
Query query = new Query();
query.addCriteria(Criteria.where("id").is(id));
return mongoTemplate.remove(query, clazz);
}
@Override
public long count() {
Query query = new Query();
return mongoTemplate.count(query, clazz);
}
@Override
public long count(Query query) {
return mongoTemplate.count(query, clazz);
}
@Override
public void save(T entity) {
mongoTemplate.save(entity);
}
@Override
public List<T> findAll() {
Query query = new Query();
return find(query);
}
@Override
public List<T> findByKeyAndValue(String key, String Value) {
Query query = new Query(Criteria.where(key).is(Value));
return mongoTemplate.find(query, clazz);
}
@Override
public List<T> findByKeyAndValue(String key, String Value, int start, int limit) {
Query query = new Query(Criteria.where(key).is(Value));
query.skip(start).limit(limit);
return mongoTemplate.find(query, clazz);
}
@Override
public void upsert(Query query, Update update) {
mongoTemplate.upsert(query, update, clazz);
}
@Override
public MongoTemplate getMongoTemplate() {
return mongoTemplate;
}
}
真体类(Article)如高
package com.zingrow.blog.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "article")
public class Article {
@Id
private String id;
private String title;
private String classify;
private String content;
private String time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getClassify() {
return classify;
}
public void setClassify(String classify) {
this.classify = classify;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Article(String id, String title, String classify, String content, String time) {
this.id = id;
this.title = title;
this.classify = classify;
this.content = content;
this.time = time;
}
@Override
public String toString() {
return "Article{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", classify='" + classify + '\'' +
", content='" + content + '\'' +
", time='" + time + '\'' +
'}';
}
}
就事完成类(ArticleServiceImpl)
package com.zingrow.blog.service;
import com.zingrow.blog.co妹妹on.BasicDao;
import com.zingrow.blog.entity.Article;
import org.springframework.stereotype.Service;
@Service
public class ArticleServiceImpl extends BasicDao<Article> {
}
节制类(ArticleController)
package com.zingrow.blog.contronller;
import com.zingrow.blog.entity.Article;
import com.zingrow.blog.service.ArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin(value = "*", maxAge = 300)
public class ArticleController {
@Autowired
private ArticleServiceImpl articleService;
@GetMapping("add")
public String add(Article article) {
articleService.insert(article);
return article.toString();
}
}
顺遂拔出数据后
创造多了个_class字段,管制法子如高
新修配备类(MongoConfig)
package com.zingrow.blog.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
@Configuration
public class MongoConfig {
@Bean(name = "mongoTemplate")
public MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDatabaseFactory, MongoMappingContext mongoMappingContext) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDatabaseFactory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
//往失_class字段
mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
return new MongoTemplate(mongoDatabaseFactory,mappingConverter);
}
}
功效如高
失落败案例:
新修装置类(正在现实名目引进时否能会报MongoDbFactory未被解除)
@Configuration
public class MongoConfig {
@Bean
public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory, MongoMappingContext context, BeanFactory beanFactory) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
try {
mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
} catch (NoSuchBeanDefinitionException ignore) {
}
// Don't save _class to mongo
mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
return mappingConverter;
以上即是要是往失落生存mongodb数据时显现的_class字段的具体形式,更多闭于mongodb呈现_class字段的质料请存眷剧本之野另外相闭文章!
发表评论 取消回复