对Mongo驱动程序实例和副本集使用基于Java的@configuration类或XML命名空间的Spring配置支持。
MongoTemplate帮助程序类,用于提高执行常见Mongo操作的效率。包括文档和POJO之间的集成对象映射。
将异常转换为Spring的可移植数据访问异常层次结构
与Spring转换服务集成的功能丰富的对象映射
基于注释的映射元数据,但可扩展以支持其他元数据格式
持久性和映射生命周期事件
使用MongoReader/MongoWriter抽象的低级映射
基于Java的查询、条件和更新dsl
存储库接口的自动实现,包括对自定义查询方法的支持。
支持类型安全查询的QueryDSL集成。地理空间一体化
映射减少集成
JMX管理和监控
对存储库的CDI支持
GridFS支持
package com.programb.pos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.programb.pos.dao.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "customer")
public class Customer {
@Id
private String id;
private String firstName;
private String lastName;
public Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
package com.programb.pos.dao.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.programb.pos.dao.entity.Customer;
import java.util.List;
public interface CustomerRepository extends MongoRepository<Customer, String> {
Customer findByFirstName(String firstName);
List<Customer> findByLastName(String lastName);
}
package com.programb.pos.service;
import org.springframework.stereotype.Service;
import com.programb.pos.dao.entity.Customer;
import com.programb.pos.dao.repository.CustomerRepository;
import javax.annotation.Resource;
import java.util.List;
@Service
public class CustomerService {
@Resource
private CustomerRepository repository;
public void deleteAll() {
repository.deleteAll();
}
public void save(Customer customer) {
repository.save(customer);
}
public Iterable<Customer> findAll() {
return repository.findAll();
}
public Customer findByFirstName(String firstName) {
return repository.findByFirstName(firstName);
}
public List<Customer> findByLastName(String lastName) {
return repository.findByLastName(lastName);
}
}
spring:
profiles:
active: dev
---
spring:
profiles: dev
data:
mongodb:
uri: mongodb://programb:0000@localhost:1007/pro
logging:
level:
ROOT: INFO
com:
xncoding: DEBUG
file: D:/programb/logs/app.log
<?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.programb</groupId>
<artifactId>springboot-mongodb</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-mongodb</name>
<description>MongoDB</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<!--<proc>none</proc>-->
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>