依赖pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
配置文件bootstrap.properties
pingruan.base.use-mongo=true
spring.data.mongodb.uri=mongodb://root:qwe123@192.168.164.100:27017
spring.data.mongodb.database=vander-service-demo
配置源码
/**
* Mongodb配置
*
* @author: vander
* @create: 2018/12/05 13:49
*/
@Configuration
@ConditionalOnProperty(value="pingruan.base.use-mongo",havingValue="true")
public class MongoConfig {
@Value("${spring.data.mongodb.uri}")
private String uri;
@Value("${spring.data.mongodb.databa se}")
private String database;
@Bean
@Primary
public MongoMappingContext mongoMappingContext() {
MongoMappingContext mappingContext = new MongoMappingContext();
return mappingContext;
}
@Bean
@Primary
public MappingMongoConverter mappingMongoConverter() {
DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(this.dbFactory());
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, this.mongoMappingContext());
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
return converter;
}
@Bean
@Primary
public MongoDbFactory dbFactory() {
return new SimpleMongoDbFactory(new MongoClient(new MongoClientURI(uri)),database);
}
@Bean
@Primary
public MongoTemplate mongoTemplate() {
return new MongoTemplate(dbFactory(), this.mappingMongoConverter());
}
}
/**
* mongodb操作
*
* @author: vander
* @create: 2018/12/03 14:39
*/
public class MongoDao<T> {
@Autowired(required = false)
MongoTemplate mongoTemplate;
private void checkObj(MongoTemplate mongoTemplate) {
if (null == mongoTemplate){
throw new BException("mongo init error ...", 500);
}
}
/**
* 条件查询
*
* @param query 条件
* @param entityClass 对象
* @return
*/
public List<T> find(Query query, Class<T> entityClass) {
checkObj(mongoTemplate);
return mongoTemplate.find(query, entityClass);
}
/**
* 查询记录数
*
* @param query 条件
* @param entityClass 对象
* @return
*/
public long count(Query query, Class<T> entityClass) {
checkObj(mongoTemplate);
return mongoTemplate.count(query, entityClass);
}
/**
* 查询所有
* @param entityClass 对象
* @return
*/
public List<T> findAll(Class<T> entityClass){
checkObj(mongoTemplate);
return mongoTemplate.findAll(entityClass);
}
/**
* 保存对象
* @param entityClass
*/
public void save(Object entityClass){
checkObj(mongoTemplate);
mongoTemplate.save(entityClass);
}
/**
* 根据id获取数据
*
* @param id
* @param entityClass
* @return
*/
public T findById(Object id,Class<T> entityClass){
checkObj(mongoTemplate);
return mongoTemplate.findById(id,entityClass);
}
/**
* 更新数据
* @param query
* @param update
* @param entityClass
* @return
*/
public long update(Query query, Update update, Class<?> entityClass){
checkObj(mongoTemplate);
UpdateResult updateResult = mongoTemplate.updateMulti(query, update, entityClass);
return updateResult.getModifiedCount();
}
/**
*
* 删除数据
* @param query
* @param entityClass
* @return
*/
public long delete(Query query, Class<?> entityClass){
checkObj(mongoTemplate);
DeleteResult deleteResult = mongoTemplate.remove(query, entityClass);
return deleteResult.getDeletedCount();
}
}
/**
* cms页面列表
*
* @author vander
* @date 2018年11月15日
*/
@Data
@ToString
@Document(collection = "cms_page")
public class CmsPage {
/**
* 页面名称、别名、访问地址、类型(静态/动态)、页面模版、状态
*/
//站点ID
@NotBlank(message = "站点ID不能为空")
private String siteId;
//页面ID
@Id
private String pageId;
//页面名称
@NotBlank(message = "页面名称不能为空")
private String pageName;
//别名
@NotBlank(message = "别名不能为空")
private String pageAliase;
//访问地址
@NotBlank(message = "访问地址不能为空")
private String pageWebPath;
//参数
private String pageParameter;
//物理路径
@NotBlank(message = "物理路径不能为空")
private String pagePhysicalPath;
//类型(静态/动态)
@NotBlank(message = "类型不能为空")
private String pageType;
//页面模版
private String pageTemplate;
//页面静态化内容
private String pageHtml;
//状态
private String pageStatus;
//创建时间
@NotNull(message = "创建时间不能为空")
private Date pageCreateTime;
//模版id@NotBlank(message = "页面模版不能为空")
private String templateId;
//参数列表
private List<CmsPageParam> pageParams;
//模版文件Id
// private String templateFileId;
//静态文件Id
private String htmlFileId;
//数据Url
private String dataUrl;
}