接上一篇文章;
springboot连接mongo数据库有多种方式:例如
MongoTemplate 使用springboot的模板方法 mongodb-driver 使用数据库驱动操作数据库
- 使用MongoTemolate需要引入的依赖:
在pom文件中添加
代码语言:html复制 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
在数据库操作类中引入:
代码语言:java复制@Autowired
private MongoTemplate mongoTemplate;
即可;
- 使用mongodb-driver-sync
相对于mongoTemplate,使用数据库驱动连接数据库,可以进行很多底层操作,且目前其性能也不输mongoTemplate; 此次使用这个方法整合数据库;
- 在demo-common中的pom文件中引入依赖:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.6.0</version>
</dependency>
- 配置文件中配置数据库地址:
application.properties中配置
代码语言:c复制demo.mongodb.uri = mongodb://127.0.0.1:27017/demo
- 实例化连接客户端:
public class demoMongoClient implements InitializingBean {
private final String mongodbUriString;
private MongoClient mongoClient;
private MongoDatabase database;
public demoMongoClient(String mongodbUriString) {
this.mongodbUriString = mongodbUriString;
}
public MongoClient getMongoClient() {
return this.mongoClient;
}
public MongoDatabase getDatabase() {
return this.database;
}
@Override
public void afterPropertiesSet() {
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
CodecRegistries.fromProviders(PojoCodecProvider.builder().automatic(true).build()));
String dbName;
Builder builder;
if (StringUtils.hasLength(this.mongodbUriString)) {
ConnectionString connStr = new ConnectionString(this.mongodbUriString);
dbName = connStr.getDatabase();
this.mongodbUriString);
builder = MongoClientSettings.builder().codecRegistry(codecRegistry).applyConnectionString(connStr);
} else {
throw new IllegalArgumentException();
}
this.mongoClient = MongoClients.create(builder.build());
this.database = this.mongoClient.getDatabase(Objects.requireNonNull(dbName));
}
- 创建抽象类,抽取公共操作
public abstract class AbstractDAO<T> {
public T get(String id, List<String> fields) {
if (CollectionUtils.isEmpty(fields)) {
return this.collection.find(Filters.eq(id)).first();
} else {
return this.collection.find(Filters.eq(id)).projection(Projections.include(fields)).first();
}
}
public DeleteResult delete(String id) {
return this.collection.deleteOne(Filters.eq(id));
}
public void insert(T doc) {
this.collection.insertOne(doc);
}
public boolean exists(String id) {
return this.collection.countDocuments(Filters.eq(id)) > 0;
}
public T updateAndGet(Object id, Bson update, List<String> fields) {
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);
if (!CollectionUtils.isEmpty(fields)) {
options.projection(Projections.include(fields));
}
return this.collection.findOneAndUpdate(eq(id), update, options);
}
public UpdateResult update(Object id, @NonNull Bson update) {
return this.collection.updateOne(eq(id), update);
}
public List<T> find(Bson filter, List<String> fields) {
FindIterable<T> iterable = this.collection.find((filter == null ? new Document() : filter));
if (!CollectionUtils.isEmpty(fields)) {
iterable.projection(Projections.include(fields));
}
return iterable.into(new ArrayList<>());
}
}