本次使用Spring Data MongoDB连接MongoDB并不是一个整合进Spring的Web项目
就是一个连接和操作MongoDB的Demo
1.依赖
一共有两个依赖,一个是MongoDB Java Driver,一个是Spring Data MongoDB Core
注意MongoDB的驱动版本号最好和数据库版本相对应(我的数据库是3.6.4,我下载的驱动也是3.6.4)
仓库地址:MongoDB Java Driver;Spring Data MongoDB Core
2.连接数据库
我们这个demo由3部分组成:1.数据对象;2.数据连接类(工具类);3.数据库操作类(测试类)
2.1数据对象代码
package cn.mrxionge.mongospring;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
@Data
@AllArgsConstructor
public class VehicleVo {
@Id
private String vehicleId;
private String brand;
private String model;
private String level;
private Integer prices;
private String type;
private Integer horsepower;
}
注意:这里的@Id注解,这个注解的字段会变为数据库中的文档的唯一id。
2.2数据连接类(工具类)
package cn.mrxionge.mongospring;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import java.util.UUID;
public class MongoSpringUtil {
public static String getRandomId() {
return UUID.randomUUID().toString().replace("-", "").substring(0, 16);
}
public static MongoOperations getMongoOperations(String ip, String port, String name, String pwd, String db) {
MongoClientURI uri = new MongoClientURI("mongodb://" + name + ":" + pwd + "@" + ip + ":" + port + "/?authSource=admin&authMechanism=SCRAM-SHA-1");
return new MongoTemplate(new MongoClient(uri), db);
}
}
注意:这里的MongoClientURI,根据数据库配置的不同,URI的参数也有所不同
详情请参考:Connect to MongoDB
2.3数据库操作类(测试类)
import cn.mrxionge.mongospring.MongoSpringUtil;
import cn.mrxionge.mongospring.VehicleVo;
import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
class MongoSpringTest {
@Test
void mongoInsertOne() {
MongoOperations operations = MongoSpringUtil.getMongoOperations("127.0.0.1", "27017", "root", "123456", "work");
VehicleVo vehicle = new VehicleVo(MongoSpringUtil.getRandomId(), "Lexus", "LC 500h", "D", 1290000, "coupe", 359);
operations.insert(vehicle);
}
@Test
void mongoInsertMany() {
MongoOperations operations = MongoSpringUtil.getMongoOperations("127.0.0.1", "27017", "root", "123456", "work");
List<VehicleVo> vehicleVos = new ArrayList<>();
vehicleVos.add(new VehicleVo(MongoSpringUtil.getRandomId(), "Porsche", "Panamera Turbo", "D", 1950000, "sedan", 550));
vehicleVos.add(new VehicleVo(MongoSpringUtil.getRandomId(), "Mercedes-AMG", "C63 S", "C", 1060000, "sedan", 510));
vehicleVos.add(new VehicleVo(MongoSpringUtil.getRandomId(), "Audi Sport", "RS 6 Avant", "D", 1390000, "wagon", 560));
vehicleVos.add(new VehicleVo(MongoSpringUtil.getRandomId(), "BMW M-Power", "M5", "D", 1510000, "sedan", 600));
vehicleVos.add(new VehicleVo(MongoSpringUtil.getRandomId(), "Ferrari", "GTC4Lusso", "D", 3580000, "coupe", 609));
operations.insert(vehicleVos, VehicleVo.class);
}
@Test
void mongoFindOne() {
MongoOperations operations = MongoSpringUtil.getMongoOperations("127.0.0.1", "27017", "root", "123456", "work");
VehicleVo vehicleVo = operations.findOne(new Query(where("model").is("C63 S")), VehicleVo.class);
System.out.println(vehicleVo.toString());
}
@Test
void mongoFindList() {
MongoOperations operations = MongoSpringUtil.getMongoOperations("127.0.0.1", "27017", "root", "123456", "work");
List<VehicleVo> vehicleVos = operations.find(new Query(where("type").is("coupe")), VehicleVo.class);
vehicleVos.forEach(System.out::println);
}
@Test
void mongoUpdate() {
MongoOperations operations = MongoSpringUtil.getMongoOperations("127.0.0.1", "27017", "root", "123456", "work");
operations.updateMulti(new Query(where("type").is("supercar")), new Update().set("type", "coupe"), VehicleVo.class);
}
@Test
void mongoRemove() {
MongoOperations operations = MongoSpringUtil.getMongoOperations("127.0.0.1", "27017", "root", "123456", "work");
operations.remove(new Query(where("type").is("sedan")), VehicleVo.class);
}
@Test
void mongoDropCollection() {
MongoOperations operations = MongoSpringUtil.getMongoOperations("127.0.0.1", "27017", "root", "123456", "work");
operations.dropCollection(VehicleVo.class);
}
}
注意:代码导入里面有“import static”,这是静态导入,而且后面直接指向的是一个方法,这样我们就能直接使用where方法了。
到此,我们就可以使用JUnit来进行测试了。
我们不需要提前建立集合,当你执行insert的时候,如果没有指定集合名称的话,会自动创建一个和数据对象同名的集合。(当然我们在操作的时候,也可以使用字符串参数来指定集合,我更喜欢用对象名字自己去映射)
最后放一张MongoDB的测试数据截图