MongoDB学习(三)MongoDBJava入门

database

1、搭建测试环境

  • 步骤一:创建 maven 项目

  • 父项目的pom文件

    <?xml version="1.0" encoding="UTF-8"?>

    <project xmlns="http://maven.apache.org/POM/4.0.0"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tqylxuecheng</groupId>

    <artifactId>xc_parent</artifactId>

    <packaging>pom</packaging>

    <version>1.0-SNAPSHOT</version>

    <modules>

    <module>xc_test_parent</module>

    </modules>

    <!-- 1 确定spring boot的版本-->

    <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>2.1.4.RELEASE</version>

    </parent>

    <!--2 确定版本-->

    <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <java.version>1.8</java.version>

    <spring-cloud-release.version>Greenwich.RELEASE</spring-cloud-release.version>

    <mybatis.starter.version>1.3.2</mybatis.starter.version>

    <mapper.starter.version>2.0.3</mapper.starter.version>

    <pageHelper.starter.version>1.2.3</pageHelper.starter.version>

    <druid.starter.version>1.1.9</druid.starter.version>

    <mysql-connector-java.version>5.1.32</mysql-connector-java.version>

    <lombok.version>1.16.20</lombok.version>

    <commons-io.version>2.6</commons-io.version>

    <org.apache.commons.io.version>1.3.2</org.apache.commons.io.version>

    <commons-fileupload.version>1.3.3</commons-fileupload.version>

    <commons-codec.version>1.10</commons-codec.version>

    <commons-lang3.version>3.6</commons-lang3.version>

    <okhttp.version>3.9.1</okhttp.version>

    <feign-okhttp.version>8.18.0</feign-okhttp.version>

    <springfox-swagger.version>2.7.0</springfox-swagger.version>

    <fastjson.version>1.2.9</fastjson.version>

    <fastdfs-client-java.version>1.27.0.0</fastdfs-client-java.version>

    <guava.version>24.0-jre</guava.version>

    </properties>

    <!-- 3 锁定sprig cloud版本-->

    <dependencyManagement>

    <dependencies>

    <!--sprig cloud版本-->

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-dependencies</artifactId>

    <version>${spring-cloud-release.version}</version>

    <type>pom</type>

    <scope>import</scope>

    </dependency>

    <!-- mybatis启动器 -->

    <dependency>

    <groupId>org.mybatis.spring.boot</groupId>

    <artifactId>mybatis-spring-boot-starter</artifactId>

    <version>${mybatis.starter.version}</version>

    </dependency>

    <!-- 通用Mapper启动器 -->

    <dependency>

    <groupId>tk.mybatis</groupId>

    <artifactId>mapper-spring-boot-starter</artifactId>

    <version>${mapper.starter.version}</version>

    </dependency>

    <dependency>

    <groupId>com.github.pagehelper</groupId>

    <artifactId>pagehelper-spring-boot-starter</artifactId>

    <version>${pageHelper.starter.version}</version>

    </dependency>

    <!-- druid启动器 -->

    <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>druid-spring-boot-starter</artifactId>

    <version>${druid.starter.version}</version>

    </dependency>

    <dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <version>${mysql-connector-java.version}</version>

    </dependency>

    <dependency>

    <groupId>com.squareup.okhttp3</groupId>

    <artifactId>okhttp</artifactId>

    <version>${okhttp.version}</version>

    </dependency>

    <dependency>

    <groupId>com.netflix.feign</groupId>

    <artifactId>feign-okhttp</artifactId>

    <version>${feign-okhttp.version}</version>

    </dependency>

    <dependency>

    <groupId>commons-io</groupId>

    <artifactId>commons-io</artifactId>

    <version>${commons-io.version}</version>

    </dependency>

    <dependency>

    <groupId>org.apache.commons</groupId>

    <artifactId>commons-io</artifactId>

    <version>${org.apache.commons.io.version}</version>

    </dependency>

    <dependency>

    <groupId>commons-fileupload</groupId>

    <artifactId>commons-fileupload</artifactId>

    <version>${commons-fileupload.version}</version>

    </dependency>

    <dependency>

    <groupId>commons-codec</groupId>

    <artifactId>commons-codec</artifactId>

    <version>${commons-codec.version}</version>

    </dependency>

    <dependency>

    <groupId>org.apache.commons</groupId>

    <artifactId>commons-lang3</artifactId>

    <version>${commons-lang3.version}</version>

    </dependency>

    <!--swagger2-->

    <dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-swagger2</artifactId>

    <version>${springfox-swagger.version}</version>

    </dependency>

    <dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-swagger-ui</artifactId>

    <version>${springfox-swagger.version}</version>

    </dependency>

    <!--fastdfs-->

    <dependency>

    <groupId>net.oschina.zcx7878</groupId>

    <artifactId>fastdfs-client-java</artifactId>

    <version>${fastdfs-client-java.version}</version>

    </dependency>

    <dependency>

    <groupId>com.google.guava</groupId>

    <artifactId>guava</artifactId>

    <version>${guava.version}</version>

    </dependency>

    </dependencies>

    </dependencyManagement>

    <!-- 4 确定spring cloud私有仓库-->

    <repositories>

    <repository>

    <id>spring-milestones</id>

    <name>Spring Milestones</name>

    <url>https://repo.spring.io/milestone</url>

    <snapshots>

    <enabled>false</enabled>

    </snapshots>

    </repository>

    </repositories>

    </project>

  • 步骤三:子模块测试

  • 结构目录

  • test_mongo 的 pom文件添加依赖

     <dependencies>

    <!--测试-->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-test</artifactId>

    </dependency>

    <!--mongodb-->

    <dependency>

    <groupId>org.mongodb</groupId>

    <artifactId>mongo-java-driver</artifactId>

    <version>3.4.3</version>

    </dependency>

    </dependencies>

2、基本操作

  • 创建测试类 Test01

2.1、获得连接

  • 方式1:连接本地数据库

     @Test

    public void testConnection() {

    //获得本地连接

    MongoClient mongoClient = new MongoClient("localhost", 27017);

    System.out.println(mongoClient);

    }

  • 方式2:采用连接字符串

    @Test

    public void testConnection2() {

    //获得连接

    //拼凑连接字符串

    MongoClientURI connectionString = new MongoClientURI("mongodb://root:1234@localhost:27017");

    //获得连接

    MongoClient mongoClient = new MongoClient(connectionString);

    System.out.println(mongoClient);

    }

2.2、查询第一个

    @Test

public void testFindOne(){

//查询一个

//1 获得连接

MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");

MongoClient mongoClient = new MongoClient(mongoClientURI);

// 2 获得数据库

MongoDatabase database = mongoClient.getDatabase("demo");

//3 获得集合

MongoCollection<Document> studentCollection = database.getCollection("student");

//4 查询操作

Document document = studentCollection.find().first();

//5 将文档转换json,并输出

System.out.println(document.toJson());

}

2.3、创建集合

    @Test

public void testCreateColl(){

//1 获得连接

MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");

MongoClient mongoClient = new MongoClient(mongoClientURI);

//2 获得数据库

MongoDatabase database = mongoClient.getDatabase("demo");

//3 创建集合

database.createCollection("teacher");

}

2.4、插入一个文档

    @Test

public void testInsertDocument(){

//1 获得连接

MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");

MongoClient mongoClient = new MongoClient( mongoClientURI );

//2 获得数据库

MongoDatabase database = mongoClient.getDatabase("demo");

//3 获得集合

MongoCollection<Document> collection = database.getCollection("teacher");

//4 准备文档

Document document = new Document();

document.append("username","jack");

document.append("password","1234");

document.append("age", 18 );

//5 录入文档

collection.insertOne(document);

}

2.5、批量插入文档

    @Test

public void testInsertManyDocument(){

//批量插入

//1 获得连接

MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");

MongoClient mongoClient = new MongoClient(mongoClientURI);

//2 获得数据库

MongoDatabase database = mongoClient.getDatabase("demo");

//3 获得集合

MongoCollection<Document> collection = database.getCollection("teacher");

//4 准备一组数据

Document doc = new Document();

doc.append("username","rose");

doc.append("password","1234");

doc.append("age", 21 );

Document doc2 = new Document();

doc2.append("username","tom");

doc2.append("password","666");

doc2.append("age", 25 );

List<Document> list = new ArrayList<>();

list.add( doc );

list.add( doc2 );

//5 批量插入

collection.insertMany( list );

}

2.6、查询所有

 @Test

public void testFindAll(){

//1 获得连接

MongoClientURI mongoClientURI = new MongoClientURI("mongodb://root:1234@localhost:27017");

MongoClient mongoClient = new MongoClient(mongoClientURI);

//2 获得数据库

MongoDatabase database = mongoClient.getDatabase("demo");

//3 获得集合

MongoCollection<Document> collection = database.getCollection("teacher");

//4 查询所有

FindIterable<Document> findIterable = collection.find();

//5 处理数据(遍历迭代器)

MongoCursor<Document> it = findIterable.iterator();

while( it.hasNext() ){

Document document = it.next();

String username = document.get("username", String.class);

String password = document.get("password", String.class);

Integer age = document.get("age", Integer.class);

System.out.println(username + "_" + password + "_" + age);

}

2.7、更新文档

@Test

public void testUpdate() {

// 采用连接字符串

MongoClientURI connectionString = new MongoClientURI("mongodb://root:1234@localhost:27017");

MongoClient mongoClient = new MongoClient(connectionString);

// 连接数据库

MongoDatabase database = mongoClient.getDatabase("demo");

// 获得集合

MongoCollection<Document> collection = database.getCollection("teacher");

// 更新

collection.updateOne(Filters.eq("age",20), new Document("$set", new Document("name","YY老师")));

}

2.8、删除文档

@Test

public void testDelete() {

// 采用连接字符串

MongoClientURI connectionString = new MongoClientURI("mongodb://root:1234@localhost:27017");

MongoClient mongoClient = new MongoClient(connectionString);

// 连接数据库

MongoDatabase database = mongoClient.getDatabase("demo");

// 获得集合

MongoCollection<Document> collection = database.getCollection("teacher");

// 删除

collection.deleteOne(Filters.eq("age",20));

}

以上是 MongoDB学习(三)MongoDBJava入门 的全部内容, 来源链接: utcz.com/z/534155.html

回到顶部