这是一篇关于springboot项目中使用mongodb。
环境:
jdk 1.8
springboot 1.5.6.RELEASE
maven 3.5
1. mongodb在springboot中的配置
springboot集成这个三方插件就是简单,只需要引入依赖,在properties或者yml中
添加相应的参数配置就好了。
(1) 引入依赖
以maven为例:
其中mongodb两个依赖包:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId><version>1.5.6.RELEASE</version></dependency><dependency><groupId>org.mongodb</groupId><artifactId>mongodb-driver</artifactId><version>3.4.2</version></dependency>
(2)properties或者yml配置(我这次没用到这个配置,是在工具类代码中直接写的)
以yml为例:
spring:data:mongodb:host: 127.0.0.1port: 27017username: musicpassword: musicdatabase: music
这里几个参数就是:
IP地址,端口号,用户名,密码,数据库。
2. mongodb的使用
(1)创建想存到mongo中的实体映射类,继承Document类
我这里是想存一些歌曲。
MusicInfo.java:
package com.test.test.test.bean;import org.bson.BsonDocument;
import org.bson.BsonDocumentWrapper;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.types.ObjectId;import java.io.Serializable;/*** Created by */
public class MusicInfo extends Document implements Serializable {private Integer returnId;private Integer id;private String mid;private String desc;private String name;private String singerName;private String fileHash;private String hqHash;private String albumId;private String type; //平台标识private String keyword; //查询关键字/*** 无参构造=*/public MusicInfo() {super();}/*** 全参构造,添加到Map中*/public MusicInfo(Integer returnId, Long id, String mid, String desc, String name, String singerName, String fileHash, String hqHash, String albumId, String type) {super();this.append("returnId", returnId).append("id", id).append("mid", mid).append("desc", desc).append("name", name).append("singerName", singerName).append("fileHash", fileHash).append("albumId", albumId).append("type", type).append("keyword",keyword);}public MusicInfo(String keyword) {super();this.append("keyword", keyword);}/*** 该方法用于collection的update*/public void setUpdate(MusicInfo musicInfo) {this.append("$set", musicInfo);}/*** mongo 中的_id** @return*/public ObjectId get_Id() {return this.getObjectId("_id");}public void set_Id(ObjectId id) {this.append("_id", id);}public Integer getReturnId() {//return returnId;return this.getInteger("returnId");}public void setReturnId(Integer returnId) {//this.returnId = returnId;this.append("returnId", returnId);}public Integer getId() {Integer integer = this.getInteger("id");return integer;//return id;}public void setId(Integer id) {//this.id = id;this.append("id", id);}public String getMid() {return this.getString("mid");//return mid;}public void setMid(String mid) {//this.mid = mid;this.append("mid", mid);}public String getDesc() {return this.getString("desc");// return desc;}public void setDesc(String desc) {//this.desc = desc;this.append("desc", desc);}public String getName() {return this.getString("name");// return name;}public void setName(String name) {this.append("name", name);// this.name = name;}public String getSingerName() {return this.getString("singerName");// return singerName;}public void setSingerName(String singerName) {this.append("singerName", singerName);//this.singerName = singerName;}public String getFileHash() {return this.getString("fileHash");//return fileHash;}public void setFileHash(String fileHash) {this.append("fileHash", fileHash);// this.fileHash = fileHash;}public String getHqHash() {return this.getString("hqHash");//return hqHash;}public void setHqHash(String hqHash) {this.append("hqHash", hqHash);// this.hqHash = hqHash;}public String getAlbumId() {return this.getString("albumId");//return albumId;}public void setAlbumId(String albumId) {this.append("albumId", albumId);// this.albumId = albumId;}public String getType() {return this.getString("type");//return albumId;}public void setType(String type) {this.append("type", type);// this.albumId = albumId;}public String getKeyword() {return this.getString("keyword");//return albumId;}public void setKeyword(String keyword) {this.append("keyword", keyword);// this.albumId = albumId;}public <TDocument> BsonDocument toBsonDocument(Class<TDocument> documentClass, CodecRegistry codecRegistry) {// TODO Auto-generated method stubreturn new BsonDocumentWrapper<MusicInfo>(this, codecRegistry.get(MusicInfo.class));}@Overridepublic String toString() {return "MusicInfo[" +"_id='" + this.get_Id() + '\'' +"albumId='" + this.getAlbumId() + '\'' +", returnId=" + this.getReturnId() +", id=" + this.getId() +", mid='" + this.getMid() + '\'' +", desc='" + this.getDesc() + '\'' +", name='" + this.getName() + '\'' +", singerName='" + this.getSingerName() + '\'' +", fileHash='" + this.getFileHash() + '\'' +", hqHash='" + this.getHqHash() + '\'' +", type='" + this.getType() + '\'' +']';}
}
(2) 创建歌曲对应操作mongodb的注册类
实现 CollectibleCodec接口
MusicInfoCodec.java:
package com.test.test.test.bean;import org.bson.*;
import org.bson.assertions.Assertions;
import org.bson.codecs.*;
import org.bson.codecs.configuration.CodecRegistry;import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;import static java.util.Arrays.asList;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;/*** Created by .***/
public class MusicInfoCodec implements CollectibleCodec<MusicInfo> {private static final String ID_FIELD_NAME = "_id";private static final CodecRegistry DEFAULT_REGISTRY = fromProviders(asList(new ValueCodecProvider(),new BsonValueCodecProvider(),new DocumentCodecProvider()));private static final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap();private final CodecRegistry registry;private final BsonTypeClassMap bsonTypeClassMap;private final IdGenerator idGenerator;private final Transformer valueTransformer;public MusicInfoCodec() {this(DEFAULT_REGISTRY, DEFAULT_BSON_TYPE_CLASS_MAP);}public MusicInfoCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap) {this(registry, bsonTypeClassMap, null);}public MusicInfoCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap, final Transformer valueTransformer) {this.registry = Assertions.notNull("registry", registry);this.bsonTypeClassMap = Assertions.notNull("bsonTypeClassMap", bsonTypeClassMap);this.idGenerator = Assertions.notNull("idGenerator", new ObjectIdGenerator());this.valueTransformer = valueTransformer != null ? valueTransformer : new Transformer() {@Overridepublic Object transform(final Object value) {return value;}};}@Overridepublic boolean documentHasId(final MusicInfo document) {return document.containsKey(ID_FIELD_NAME);}@Overridepublic BsonValue getDocumentId(final MusicInfo document) {if (!documentHasId(document)) {throw new IllegalStateException("The document does not contain an _id");}Object id = document.get(ID_FIELD_NAME);if (id instanceof BsonValue) {return (BsonValue) id;}BsonDocument idHoldingDocument = new BsonDocument();BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);writer.writeStartDocument();writer.writeName(ID_FIELD_NAME);writeValue(writer, EncoderContext.builder().build(), id);writer.writeEndDocument();return idHoldingDocument.get(ID_FIELD_NAME);}@Overridepublic MusicInfo generateIdIfAbsentFromDocument(final MusicInfo document) {if (!documentHasId(document)) {document.put(ID_FIELD_NAME, idGenerator.generate());}return document;}@Overridepublic void encode(final BsonWriter writer, final MusicInfo document, final EncoderContext encoderContext) {writeMap(writer, document, encoderContext);}@Overridepublic MusicInfo decode(final BsonReader reader, final DecoderContext decoderContext) {MusicInfo document = new MusicInfo();reader.readStartDocument();while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {String fieldName = reader.readName();document.put(fieldName, readValue(reader, decoderContext));}reader.readEndDocument();return document;}@Overridepublic Class<MusicInfo> getEncoderClass() {return MusicInfo.class;}private void beforeFields(final BsonWriter bsonWriter, final EncoderContext encoderContext, final Map<String, Object> document) {if (encoderContext.isEncodingCollectibleDocument() && document.containsKey(ID_FIELD_NAME)) {bsonWriter.writeName(ID_FIELD_NAME);writeValue(bsonWriter, encoderContext, document.get(ID_FIELD_NAME));}}private boolean skipField(final EncoderContext encoderContext, final String key) {return encoderContext.isEncodingCollectibleDocument() && key.equals(ID_FIELD_NAME);}@SuppressWarnings({"unchecked", "rawtypes"})private void writeValue(final BsonWriter writer, final EncoderContext encoderContext, final Object value) {if (value == null) {writer.writeNull();} else if (Iterable.class.isAssignableFrom(value.getClass())) {writeIterable(writer, (Iterable<Object>) value, encoderContext.getChildContext());} else if (Map.class.isAssignableFrom(value.getClass())) {writeMap(writer, (Map<String, Object>) value, encoderContext.getChildContext());} else {Codec codec = registry.get(value.getClass());encoderContext.encodeWithChildContext(codec, writer, value);}}private void writeMap(final BsonWriter writer, final Map<String, Object> map, final EncoderContext encoderContext) {writer.writeStartDocument();beforeFields(writer, encoderContext, map);for (final Map.Entry<String, Object> entry : map.entrySet()) {if (skipField(encoderContext, entry.getKey())) {continue;}writer.writeName(entry.getKey());writeValue(writer, encoderContext, entry.getValue());}writer.writeEndDocument();}private void writeIterable(final BsonWriter writer, final Iterable<Object> list, final EncoderContext encoderContext) {writer.writeStartArray();for (final Object value : list) {writeValue(writer, encoderContext, value);}writer.writeEndArray();}private Object readValue(final BsonReader reader, final DecoderContext decoderContext) {BsonType bsonType = reader.getCurrentBsonType();if (bsonType == BsonType.NULL) {reader.readNull();return null;} else if (bsonType == BsonType.ARRAY) {return readList(reader, decoderContext);} else if (bsonType == BsonType.BINARY) {byte bsonSubType = reader.peekBinarySubType();if (bsonSubType == BsonBinarySubType.UUID_STANDARD.getValue() || bsonSubType == BsonBinarySubType.UUID_LEGACY.getValue()) {return registry.get(UUID.class).decode(reader, decoderContext);}}return valueTransformer.transform(registry.get(bsonTypeClassMap.get(bsonType)).decode(reader, decoderContext));}private List<Object> readList(final BsonReader reader, final DecoderContext decoderContext) {reader.readStartArray();List<Object> list = new ArrayList<Object>();while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {list.add(readValue(reader, decoderContext));}reader.readEndArray();return list;}}
(3) MongoService:
(用于增删改查,这里只用到了查询和插入方法)
/*** Created by */
@Service
public class MongoService {/*** 数据插入MongoDB(初版)* @param Codec 用户将bson转化为Object的类* @param musicInfos 需要存储的实体集合* @param dbName 选择的db*///insertMongo(KeyEnum.TYPE_XIAMI.getKey(),keyword,resultContent);public void insertMongo(CollectibleCodec Codec,List<MusicInfo> musicInfos,String dbName){String colName = "songlist";MongoUtil instance = MongoUtil.instance;CodecRegistry registry = CodecRegistries.fromCodecs(Codec);MongoDatabase database = MongoUtil.instance.getDB(dbName);if(instance.getCollection(dbName, colName) ==null ){instance.createCollection(dbName, colName);}MongoCollection<MusicInfo> music = database.withCodecRegistry(registry).getCollection(colName, MusicInfo.class);musicInfos.forEach(item->{MusicInfo first = music.find(item, MusicInfo.class).first();if(null != first){return;}music.insertOne(item);});}public MongoService() {}/**** @param Codec 用户将bson转化为Object的类* @param musicInfo 封装查询条件的实体* @param dbName 选择的db* @return List<MusicInfo>*/public List<MusicInfo> getSongFromMongo(CollectibleCodec Codec,MusicInfo musicInfo,String dbName){String colName = "songlist";MongoUtil instance = MongoUtil.instance;CodecRegistry registry = CodecRegistries.fromCodecs(Codec);MongoDatabase database = MongoUtil.instance.getDB(dbName);if(instance.getCollection(dbName, colName) ==null ){instance.createCollection(dbName, colName);}MongoCollection<MusicInfo> music = database.withCodecRegistry(registry).getCollection(colName, MusicInfo.class);ArrayList<MusicInfo> musicInfos = new ArrayList<>();Block<MusicInfo> block = new Block<MusicInfo>() {public void apply(MusicInfo t) {musicInfos.add(t);}};music.find(musicInfo,MusicInfo.class).forEach(block);return musicInfos;}public static void main(String[] args) {//查询
// List<MusicInfo> musicInfoMongos = new MongoService().getSongFromMongo(new MusicInfoCodec(), new MusicInfo("小幸运"), "music";
// for (int i = 0; i < musicInfoMongos.size(); i++) {
// MusicInfo musicInfo = musicInfoMongos.get(i);
// System.out.println("mongo__"+musicInfo.getName());
// }}
}
(4) MongoUtil
用于mongoClient 客户端的连接
package com.test.test.test.utils;import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientOptions.Builder;
import com.mongodb.WriteConcern;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Projections.*;
import static com.mongodb.client.model.Sorts.ascending;
/*** Created by .*/
public enum MongoUtil {/*** 定义一个枚举的元素,它代表此类的一个实例*/instance;private static MongoClient mongoClient;static {System.out.println("===============MongoDBUtil初始化========================");String ip = "127.0.0.1";int port =27017;instance.mongoClient = new MongoClient(ip, port);// 大部分用户使用mongodb都在安全内网下,但如果将mongodb设为安全验证模式,就需要在客户端提供用户名和密码:// boolean auth = db.authenticate(myUserName, myPassword);Builder options = new MongoClientOptions.Builder();options.cursorFinalizerEnabled(true);// options.autoConnectRetry(true);// 自动重连true// options.maxAutoConnectRetryTime(10); // the maximum auto connect retry timeoptions.connectionsPerHost(300);// 连接池设置为300个连接,默认为100options.connectTimeout(30000);// 连接超时,推荐>3000毫秒options.maxWaitTime(5000); //options.socketTimeout(0);// 套接字超时时间,0无限制options.threadsAllowedToBlockForConnectionMultiplier(5000);// 线程队列数,如果连接线程排满了队列就会抛出“Out of semaphores to get db”错误。options.writeConcern(WriteConcern.SAFE);//options.build();}// ------------------------------------共用方法---------------------------------------------------/*** 获取DB实例 - 指定DB** @param dbName* @return*/public MongoDatabase getDB(String dbName) {if (dbName != null && !"".equals(dbName)) {MongoDatabase database = mongoClient.getDatabase(dbName);return database;}return null;}//mongoDatabase.createCollection("test");public void createCollection(String dbName, String collName){try{mongoClient.getDatabase(dbName).createCollection(collName);}catch(Exception e){System.err.println(e.getClass().getName() + ": " + e.getMessage() );}}/*** 获取collection对象 - 指定Collection** @param collName* @return*/public MongoCollection<Document> getCollection(String dbName, String collName) {if (null == collName || "".equals(collName)) {return null;}if (null == dbName || "".equals(dbName)) {return null;}MongoCollection<Document> collection = mongoClient.getDatabase(dbName).getCollection(collName);return collection;}/*** 查询DB下的所有表名*/public List<String> getAllCollections(String dbName) {MongoIterable<String> colls = getDB(dbName).listCollectionNames();List<String> _list = new ArrayList<String>();for (String s : colls) {_list.add(s);}return _list;}/*** 获取所有数据库名称列表** @return*/public MongoIterable<String> getAllDBNames() {MongoIterable<String> s = mongoClient.listDatabaseNames();return s;}/*** 删除一个数据库*/public void dropDB(String dbName) {getDB(dbName).drop();}/*** 查找对象 - 根据主键_id** @param collection* @param id* @return*/public Document findById(MongoCollection<Document> collection, String id) {Document myDoc = collection.find(Filters.eq("_id", id)).first();return myDoc;}/** 统计数 */public int getCount(MongoCollection<Document> coll) {int count = (int) coll.count();return count;}/** 条件查询 */public MongoCursor<Document> find(MongoCollection<Document> coll, Bson filter) {return coll.find(filter).iterator();}/** 分页查询 */public MongoCursor<Document> findByPage(MongoCollection<Document> coll, Bson filter, int pageNo, int pageSize) {Bson orderBy = new BasicDBObject("_id", 1);return coll.find(filter).sort(orderBy).skip((pageNo - 1) * pageSize).limit(pageSize).iterator();}/*** 通过ID删除** @param coll* @param id* @return*/public int deleteById(MongoCollection<Document> coll, String id) {int count = 0;ObjectId _id = null;try {_id = new ObjectId(id);} catch (Exception e) {return 0;}Bson filter = Filters.eq("_id", _id);DeleteResult deleteResult = coll.deleteOne(filter);count = (int) deleteResult.getDeletedCount();return count;}/*** FIXME** @param coll* @param id* @param newdoc* @return*/public Document updateById(MongoCollection<Document> coll, String id, Document newdoc) {ObjectId _idobj = null;try {_idobj = new ObjectId(id);} catch (Exception e) {return null;}Bson filter = Filters.eq("_id", _idobj);// coll.replaceOne(filter, newdoc); // 完全替代coll.updateOne(filter, new Document("$set", newdoc));return newdoc;}public void dropCollection(String dbName, String collName) {getDB(dbName).getCollection(collName).drop();}/*** 关闭Mongodb*/public void close() {if (mongoClient != null) {mongoClient.close();mongoClient = null;}}/*** 测试入口** @param args* @throws CloneNotSupportedException*/public static void main(String[] args) {String dbName = "music";String collName = "songlist";MongoCollection<Document> coll = MongoUtil.instance.getCollection(dbName, collName);ListIndexesIterable<Document> list = coll.listIndexes();//查询所有索引for (Document document : list) {System.out.println("---"+document.toJson());}}}
(5) MongoService的使用:
@Autowired
private MongoService mongoService;public void insertMongo(){List<MusicInfo> musicInfos = new ArrayList<>();mongoService.insertMongo(new MusicInfoCodec(),monicInfos, "music");
}
上一张成功的图:
最后安利一个mongodb的PC图形化客户端:
Robo 3T
Robo 3T官网