首先导入相关依赖:
org.mongodb
mongo-java-driver
org.mongodb.morphia
morphia
0.109
配置文件 application.properties 配置 连接参数:
spring.data.mongodb.host=xxx
spring.data.mongodb.port=xxx
spring.data.mongodb.database=xxx
package com.springboot.Test_myspringboot.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import org.hibernate.validator.constraints.NotEmpty;
import org.mongodb.morphia.annotations.Entity;
import org.springframework.data.mongodb.core.mapping.Document;
@Entity
@Document(collection = "ym_literature")
public class Literature extends MongoEntity implements Serializable {
private static final long serialVersionUID = -9139405423949058629L;
private String keywords;
// 文献目录
private ArrayList directories;
private String provider; // 来源
private String period; // 时期
private Date createDate;
private Date lastModifiedDate;
private String modifiedBy;
private int previewThreshold; //预览页数
private String title; //标题
@NotEmpty(message = "{liter.author.not-empty}")
private String author; //作者
private String press; //出版社
private String nIndexNo; //国图索引号
private LiteratureType literType; //文献类型
private LiteratureClassfication literClassfication; //文献分类
private String volumes; // 卷数
private String remark;
private String thumbnailId; //缩略图
private int totalPage; //总页数
private boolean updatePDF = true; //是否(重新)生成书籍
private String tips; //描述
private double originalPrice; //原价
private int contributionValue; //贡献值
private double promotionPrice; //促销价
private int favorites; //收藏数
private int downloads; //下载数
private int readers; //阅读数
private int previewers; //预览数
private int buyers; //购买人数
private double salesAmount; //销售总金额
public Literature() {
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public ArrayList getDirectories() {
return directories;
}
public void setDirectories(ArrayList directories) {
this.directories = directories;
}
public String getProvider() {
return provider;
}
public void setProvider(String provider) {
this.provider = provider;
}
public String getPeriod() {
return period;
}
public void setPeriod(String period) {
this.period = period;
}
public Date getLastModifiedDate() {
return lastModifiedDate;
}
public void setLastModifiedDate(Date lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
public String getModifiedBy() {
return modifiedBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
public int getPreviewThreshold() {
return previewThreshold;
}
public void setPreviewThreshold(int previewThreshold) {
this.previewThreshold = previewThreshold;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public String getnIndexNo() {
return nIndexNo;
}
public void setnIndexNo(String nIndexNo) {
this.nIndexNo = nIndexNo;
}
public LiteratureType getLiterType() {
return literType;
}
public void setLiterType(LiteratureType literType) {
this.literType = literType;
}
public LiteratureClassfication getLiterClassfication() {
return literClassfication;
}
public void setLiterClassfication(LiteratureClassfication literClassfication) {
this.literClassfication = literClassfication;
}
public String getVolumes() {
return volumes;
}
public void setVolumes(String volumes) {
this.volumes = volumes;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getThumbnailId() {
return thumbnailId;
}
public void setThumbnailId(String thumbnailId) {
this.thumbnailId = thumbnailId;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public boolean getUpdatePDF() {
return updatePDF;
}
public void setUpdatePDF(boolean updatePDF) {
this.updatePDF = updatePDF;
}
public String getTips() {
return tips;
}
public void setTips(String tips) {
this.tips = tips;
}
public double getOriginalPrice() {
return originalPrice;
}
public void setOriginalPrice(double originalPrice) {
this.originalPrice = originalPrice;
}
public int getContributionValue() {
return contributionValue;
}
public void setContributionValue(int contributionValue) {
this.contributionValue = contributionValue;
}
public double getPromotionPrice() {
return promotionPrice;
}
public void setPromotionPrice(double promotionPrice) {
this.promotionPrice = promotionPrice;
}
public int getFavorites() {
return favorites;
}
public void setFavorites(int favorites) {
this.favorites = favorites;
}
public int getDownloads() {
return downloads;
}
public void setDownloads(int downloads) {
this.downloads = downloads;
}
public int getReaders() {
return readers;
}
public void setReaders(int readers) {
this.readers = readers;
}
public int getPreviewers() {
return previewers;
}
public void setPreviewers(int previewers) {
this.previewers = previewers;
}
public int getBuyers() {
return buyers;
}
public void setBuyers(int buyers) {
this.buyers = buyers;
}
public double getSalesAmount() {
return salesAmount;
}
public void setSalesAmount(double salesAmount) {
this.salesAmount = salesAmount;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
package com.springboot.Test_myspringboot.service.impl;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Service;
import com.springboot.Test_myspringboot.entity.Literature;
import com.springboot.Test_myspringboot.service.IMongoService;
/**
* @author xuchuang
* @updateTime 2017年12月26日 下午3:18:56
*/
@Service("mongoService")
public class MongoService implements IMongoService{
@Autowired
private MongoTemplate mongoTemplate ;
@Override
public void MongoJdbc() {
String dbName = mongoTemplate.getDb().getName();
System.out.println("数据库名为:"+dbName);
Criteria c = new Criteria();
c.and("_id").is(new ObjectId("59f7e5a8f8fbac03249d977e"));
Literature lit= mongoTemplate.findOne(Query.query(c), Literature.class);
System.out.println("书籍标题为:"+lit.getTitle());
}
}
package com.springboot.Test_myspringboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.Test_myspringboot.service.IMongoService;
/**
* @author xuchuang
* @updateTime 2017年12月26日 下午3:16:38
*/
@RestController
public class MongoController_test {
@Autowired
private IMongoService mongoService;
@RequestMapping("/helloMongo")
public String home() {
mongoService.MongoJdbc();
return "helloMongo!";
}
}
localhost:6636/helloMongo
数据库名为:wymlib
书籍标题为:王阳明
ok 测试成功!