Springboot+spring-data系列之(一)mongodb整合示例
1、项目结构
使用ideaj快速生成springboot项目,结构如下
2、pom.xml依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.leao.springdata</groupId>
<artifactId>mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mongodb</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、application.xml
#通常是自己建立的database
spring.data.mongodb.database=springdata
spring.data.mongodb.host=192.168.127.128
spring.data.mongodb.password=admin
spring.data.mongodb.username=admin
spring.data.mongodb.port=27017
#认证的用户
spring.data.mongodb.authentication-database=admin
3、编写保存实体Order
@Data
@Document(collection = "order")
public class Order {
@Id
private String id;
private String orderId;
private String content;
private Date date;
}
4、两种不同形式的使用方式 OrderDao (一种用Repository,一种tempalate)
@Repository
public interface OrderDao extends PagingAndSortingRepository<Order, String> {
}
@Repository
public class OrderDao2 {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 创建对象
*/
public void saveorder(Order order) {
mongoTemplate.save(order);
}
/**
* 根据用户名查询对象
* @return
*/
public Order findTestByName(String name) {
Query query=new Query(Criteria.where("name").is(name));
Order order = mongoTemplate.findOne(query , Order.class);
return order;
}
/**
* 更新对象
*/
public void updateTest(Order order) {
Query query=new Query(Criteria.where("id").is(order.getId()));
Update update= new Update().set("content", order.getContent()).set("date", order.getDate());
//更新查询返回结果集的第一条
mongoTemplate.updateFirst(query,update,Order.class);
//更新查询返回结果集的所有
// mongoTemplate.updateMulti(query,update,TestEntity.class);
}
/**
* 删除对象
* @param id
*/
public void deleteTestById(Integer id) {
Query query=new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query,Order.class);
}
public void findAll() {
List<Order> all = mongoTemplate.findAll(Order.class,"order");
System.out.println(all);
}
5、service编写
@Service
public class OrderServiceImpl {
@Autowired
private OrderDao orderDao;
@Autowired
private OrderDao2 orderDao2;
/**
Repository方式
*/
public void save(Order order){
orderDao.save(order);
System.out.println("保存成功");
}
public void findAll(Pageable pageable){
Page<Order> orders = orderDao.findAll(pageable);
System.out.println(orders.getTotalElements());
Stream<Order> orderStream = orders.get();
orders.get().forEach(x-> System.out.println(x.getContent()));
}
/**
* template方式
*/
public void save2(Order order){
orderDao2.saveorder(order);
System.out.println("保存成功");
}
public void findAll2(){
orderDao2.findAll();
}
6、springboot测试类单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class MongodbApplicationTests {
@Test
public void contextLoads() {
test2();
test3();
}
@Autowired
private OrderServiceImpl orderService;
private void test1(){
orderService.findAll(PageRequest.of(0,10, Sort.by("id")));
}
private void test2() {
Order order = new Order();
order.setContent("springdata");order.setDate(new Date());
// order.setId("123456");
order.setOrderId(UUID.randomUUID().toString());
orderService.save(order);
}
private void test3(){
orderService.findAll2();
}
}
最后,使用Nosqlbooster查看结果
相关代码已经上传至:https://github.com/anshitou/springboot-springdata-compose/tree/master