MongoDB 学习(三)MongoDB 和 Spring 整合(Mav…

2018-06-18 02:50:32来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

一、MongoDB 和 Spring 整合(Maven)

1、相关 jar 包准备

2、用 Maven 创建项目,pom.xml 文件

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.test.web</groupId>
  5     <artifactId>study-maven</artifactId>
  6     <packaging>war</packaging>
  7     <version>1.0-SNAPSHOT</version>
  8     <name>study-maven Maven Webapp</name>
  9     <url>http://maven.apache.org</url>
 10 
 11 
 12     <properties>
 13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 14         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 15         <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
 16 
 17         <!-- spring版本号 -->
 18         <spring.version>4.1.9.RELEASE</spring.version>
 19         <org.mybatis.version>3.4.1</org.mybatis.version>
 20         <org.mybatis.spring.version>1.3.0</org.mybatis.spring.version>
 21     </properties>
 22 
 23 
 24     <dependencies>
 25         <!--驱动包-->
 26         <dependency>
 27             <groupId>org.mongodb</groupId>
 28             <artifactId>mongo-java-driver</artifactId>
 29             <version>2.13.0-rc2</version>
 30         </dependency>
 31         <!--核心包-->
 32         <dependency>
 33             <groupId>org.springframework.data</groupId>
 34             <artifactId>spring-data-mongodb</artifactId>
 35             <version>1.8.2.RELEASE</version>
 36         </dependency>
 37         <!--依赖-->
 38         <dependency>
 39             <groupId>org.springframework.data</groupId>
 40             <artifactId>spring-data-commons</artifactId>
 41             <version>1.10.0.RELEASE</version>
 42         </dependency>
 43 
 44 
 45         <!-- 添加spring核心依赖 -->
 46         <dependency>
 47             <groupId>org.springframework</groupId>
 48             <artifactId>spring-core</artifactId>
 49             <version>${spring.version}</version>
 50         </dependency>
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-web</artifactId>
 54             <version>${spring.version}</version>
 55         </dependency>
 56         <dependency>
 57             <groupId>org.springframework</groupId>
 58             <artifactId>spring-oxm</artifactId>
 59             <version>${spring.version}</version>
 60         </dependency>
 61         <dependency>
 62             <groupId>org.springframework</groupId>
 63             <artifactId>spring-tx</artifactId>
 64             <version>${spring.version}</version>
 65         </dependency>
 66         <dependency>
 67             <groupId>org.springframework</groupId>
 68             <artifactId>spring-jdbc</artifactId>
 69             <version>${spring.version}</version>
 70         </dependency>
 71         <dependency>
 72             <groupId>org.springframework</groupId>
 73             <artifactId>spring-webmvc</artifactId>
 74             <version>${spring.version}</version>
 75         </dependency>
 76         <dependency>
 77             <groupId>org.springframework</groupId>
 78             <artifactId>spring-context</artifactId>
 79             <version>${spring.version}</version>
 80         </dependency>
 81         <dependency>
 82             <groupId>org.springframework</groupId>
 83             <artifactId>spring-context-support</artifactId>
 84             <version>${spring.version}</version>
 85         </dependency>
 86         <dependency>
 87             <groupId>org.springframework</groupId>
 88             <artifactId>spring-aop</artifactId>
 89             <version>${spring.version}</version>
 90         </dependency>
 91 
 92         <dependency>
 93             <groupId>org.springframework</groupId>
 94             <artifactId>spring-test</artifactId>
 95             <version>${spring.version}</version>
 96         </dependency>
 97         <dependency>
 98             <groupId>junit</groupId>
 99             <artifactId>junit</artifactId>
100             <version>4.12</version>
101         </dependency>
102 
103     </dependencies>
104     <build>
105         <finalName>study-maven</finalName>
106     </build>
107 </project>
View Code

3、spring-config.xml 配置文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mongo="http://www.springframework.org/schema/data/mongo"
 6 
 7        xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
 8     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 9     http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
10 
11 
12     <!--1.开启注解-->
13     <context:annotation-config/>
14 
15     <!--2.自动扫描-->
16     <context:component-scan base-package="com.mongodb"/>
17 
18     <!--3.服务器连接信息-->
19     <mongo:mongo host="localhost" port="27017"/>
20 
21     <!--4.创建mongoTemplate模板-->
22     <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
23         <constructor-arg ref="mongo"/>
24         <!-- 设置使用的数据库 名-->
25         <constructor-arg name="databaseName" value="station"/>
26     </bean>
27     
28 </beans>

4、pojo 类

Product 类

 1 package com.mongodb.pojo;
 2 
 3 /**
 4  * @author zt1994 2018/3/12 10:43
 5  */
 6 public class Product {
 7     private String id;
 8     private String productName;
 9     private Integer productNum;
10 
11     public String getId() {
12         return id;
13     }
14 
15     public void setId(String id) {
16         this.id = id;
17     }
18 
19     public String getProductName() {
20         return productName;
21     }
22 
23     public void setProductName(String productName) {
24         this.productName = productName;
25     }
26 
27     public Integer getProductNum() {
28         return productNum;
29     }
30 
31     public void setProductNum(Integer productNum) {
32         this.productNum = productNum;
33     }
34 
35 
36     @Override
37     public String toString() {
38         return "Product{" +
39                 "id='" + id + '\'' +
40                 ", productName='" + productName + '\'' +
41                 ", productNum=" + productNum +
42                 '}';
43     }
44 }

5、dao 层

IProductDao 接口:

 1 package com.mongodb.dao;
 2 
 3 import com.mongodb.pojo.Product;
 4 
 5 import java.util.List;
 6 
 7 /**
 8  * @author zt1994 2018/3/12 10:45
 9  */
10 public interface IProductDao {
11     /**
12      * 添加产品
13      * @param product
14      */
15     void addProduct(Product product);
16 
17 
18     /**
19      * 删除产品
20      * @param id
21      */
22     void removeProduct(String id);
23 
24 
25     /**
26      * 保存或修改产品
27      * @param product
28      */
29     void saveOrUpdateProduct(Product product);
30 
31 
32     /**
33      * 查询单个产品
34      * @param id
35      * @return
36      */
37     Product findById(String id);
38 
39 
40     /**
41      * 查询所有产品
42      * @return
43      */
44     List<Product> findAll();
45 }

ProductDaoImpl 实现类:

 1 package com.mongodb.dao.impl;
 2 
 3 import com.mongodb.dao.IProductDao;
 4 import com.mongodb.pojo.Product;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.data.mongodb.core.MongoTemplate;
 7 import org.springframework.stereotype.Repository;
 8 
 9 import java.util.List;
10 
11 /**
12  * @author zt1994 2018/3/12 10:46
13  */
14 @Repository
15 public class ProductDaoImpl implements IProductDao {
16 
17     @Autowired
18     private MongoTemplate mongoTemplate;
19 
20     public void addProduct(Product product) {
21         //1.如果没有指定集合,则默认添加到和对象名称相同的集合中,没有则创建一个
22         //2.也可以指定集合 mongoTemplate.save(product, "product_db");
23         mongoTemplate.save(product);
24     }
25 
26     public void removeProduct(String id) {
27         Product product = findById(id);
28         mongoTemplate.remove(product);
29     }
30 
31     public void saveOrUpdateProduct(Product product) {
32         mongoTemplate.save(product);
33     }
34 
35     public Product findById(String id) {
36         return mongoTemplate.findById(id, Product.class);
37     }
38 
39     public List<Product> findAll() {
40         return mongoTemplate.findAll(Product.class);
41     }
42 }

6、测试

 1 package com.mongodb.test;
 2 
 3 import com.mongodb.dao.IProductDao;
 4 import com.mongodb.pojo.Product;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.test.context.ContextConfiguration;
 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 
11 import java.util.List;
12 
13 /**
14  * @author zt1994 2018/3/12 10:52
15  */
16 @RunWith(SpringJUnit4ClassRunner.class)
17 @ContextConfiguration("classpath:spring-config.xml")
18 public class TestProductDao {
19 
20     @Autowired
21     private IProductDao productDao;
22 
23     /**
24      * 测试添加产品
25      */
26     @Test
27     public void testAddProduct(){
28         Product product = new Product();
29         product.setProductName("测试新增");
30         product.setProductNum(3);
31         productDao.addProduct(product);
32     }
33     
34     
35     /**
36      * 测试查找所有产品
37      */
38     @Test
39     public void testFindAll(){
40         List<Product> productList = productDao.findAll();
41         for (Product product : productList) {
42             System.out.println(product);
43         }
44     }
45 
46 
47     /**
48      * 测试通过id查找产品
49      */
50     @Test
51     public void testFindId(){
52         Product product = productDao.findById("5aa5f3d9b735172bc3a617c1");
53         System.out.println(product);
54     }
55 
56 
57     /**
58      * 测试更新产品
59      */
60     @Test
61     public void testUpdateProduct(){
62         Product product = productDao.findById("5aa5f3d9b735172bc3a617c1");
63         System.out.println(product);
64         product.setProductName("测试修改");
65         product.setProductNum(5);
66         this.productDao.saveOrUpdateProduct(product);
67         Product product2 = productDao.findById("5aa5f3d9b735172bc3a617c1");
68         System.out.println(product2);
69     }
70 }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:004数组

下一篇:java Reflection(反射)基础知识讲解