MongoDB 学习(三)MongoDB 和 Spring 整合(Maven)

一、MongoDB 和 Spring 整合(Maven)

1、相关 jar 包准备

MongoDB 学习(三)MongoDB 和 Spring 整合(Maven)

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

 <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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.web</groupId>
<artifactId>study-maven</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>study-maven Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding> <!-- spring版本号 -->
<spring.version>4.1.9.RELEASE</spring.version>
<org.mybatis.version>3.4.1</org.mybatis.version>
<org.mybatis.spring.version>1.3.0</org.mybatis.spring.version>
</properties> <dependencies>
<!--驱动包-->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.13.0-rc2</version>
</dependency>
<!--核心包-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.8.2.RELEASE</version>
</dependency>
<!--依赖-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.10.0.RELEASE</version>
</dependency> <!-- 添加spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> </dependencies>
<build>
<finalName>study-maven</finalName>
</build>
</project>

3、spring-config.xml 配置文件

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

4、pojo 类

Product 类

 package com.mongodb.pojo;

 /**
* @author zt1994 2018/3/12 10:43
*/
public class Product {
private String id;
private String productName;
private Integer productNum; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getProductName() {
return productName;
} public void setProductName(String productName) {
this.productName = productName;
} public Integer getProductNum() {
return productNum;
} public void setProductNum(Integer productNum) {
this.productNum = productNum;
} @Override
public String toString() {
return "Product{" +
"id='" + id + '\'' +
", productName='" + productName + '\'' +
", productNum=" + productNum +
'}';
}
}

5、dao 层

IProductDao 接口:

 package com.mongodb.dao;

 import com.mongodb.pojo.Product;

 import java.util.List;

 /**
* @author zt1994 2018/3/12 10:45
*/
public interface IProductDao {
/**
* 添加产品
* @param product
*/
void addProduct(Product product); /**
* 删除产品
* @param id
*/
void removeProduct(String id); /**
* 保存或修改产品
* @param product
*/
void saveOrUpdateProduct(Product product); /**
* 查询单个产品
* @param id
* @return
*/
Product findById(String id); /**
* 查询所有产品
* @return
*/
List<Product> findAll();
}

ProductDaoImpl 实现类:

 package com.mongodb.dao.impl;

 import com.mongodb.dao.IProductDao;
import com.mongodb.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository; import java.util.List; /**
* @author zt1994 2018/3/12 10:46
*/
@Repository
public class ProductDaoImpl implements IProductDao { @Autowired
private MongoTemplate mongoTemplate; public void addProduct(Product product) {
//1.如果没有指定集合,则默认添加到和对象名称相同的集合中,没有则创建一个
//2.也可以指定集合 mongoTemplate.save(product, "product_db");
mongoTemplate.save(product);
} public void removeProduct(String id) {
Product product = findById(id);
mongoTemplate.remove(product);
} public void saveOrUpdateProduct(Product product) {
mongoTemplate.save(product);
} public Product findById(String id) {
return mongoTemplate.findById(id, Product.class);
} public List<Product> findAll() {
return mongoTemplate.findAll(Product.class);
}
}

6、测试

 package com.mongodb.test;

 import com.mongodb.dao.IProductDao;
import com.mongodb.pojo.Product;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; /**
* @author zt1994 2018/3/12 10:52
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class TestProductDao { @Autowired
private IProductDao productDao; /**
* 测试添加产品
*/
@Test
public void testAddProduct(){
Product product = new Product();
product.setProductName("测试新增");
product.setProductNum(3);
productDao.addProduct(product);
} /**
* 测试查找所有产品
*/
@Test
public void testFindAll(){
List<Product> productList = productDao.findAll();
for (Product product : productList) {
System.out.println(product);
}
} /**
* 测试通过id查找产品
*/
@Test
public void testFindId(){
Product product = productDao.findById("5aa5f3d9b735172bc3a617c1");
System.out.println(product);
} /**
* 测试更新产品
*/
@Test
public void testUpdateProduct(){
Product product = productDao.findById("5aa5f3d9b735172bc3a617c1");
System.out.println(product);
product.setProductName("测试修改");
product.setProductNum(5);
this.productDao.saveOrUpdateProduct(product);
Product product2 = productDao.findById("5aa5f3d9b735172bc3a617c1");
System.out.println(product2);
}
}
上一篇:mybatis学习笔记(六) -- maven+spring+mybatis从零开始搭建整合详细过程(下)


下一篇:【XSY1162】鬼计之夜 最短路