2019-04-22 | spring | UNLOCK

SpringBoot整合mongodb

1. 添加依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

2. 编写配置文件

1
2
3
4
5
6
spring:
data:
mongodb:
database: report_pools
host: 192.168.43.148
port: 27017

3. 创建实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Document("content")
public class Content {
@Id
private String id;
@Field("title")
private String title;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Content{" +
"id='" + id + '\'' +
", title='" + title +
'}';
}
}

4. 创建封装实体类(可扩展)

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.restfulapio.demo.entity;

public class ContentVO {
private Object data;

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}
}

5. 自定义接口ContentRepository

1
2
3
4
5
6
7
@Repository
public interface ContentRepository extends MongoRepository<Content, String> {
List<Content> findAll();
Page<Content> findAll(Pageable pageable);
Optional<Content> findById(String s);
void deleteById(String s);
}

6. 自定义Service接口及实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface ContentService {
List<Content> findAll();

List<Content> findAll(int page,int limit);

Content findById(String s);

void deleteById(String s);

void save(Content content);

long count();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

@Service
public class ContentServiceImpl implements ContentService{
@Autowired
private ContentRepository contentRepository;
@Override
public void save(Content content) {
contentRepository.save(content);
}
@Override
public long count() {
return contentRepository.count();
}
@Override
public List<Content> findAll() {
return contentRepository.findAll();
}
@Override
public List<Content> findAll(int page,int limit) {
Pageable pageable = new PageRequest(page - 1, limit);
Page<Content> contentpage = contentRepository.findAll(pageable);
return contentpage.getContent();
}

@Override
public Content findById(String s) {
return contentRepository.findById(s).get();
}

@Override
public void deleteById(String s) {
contentRepository.deleteById(s);
}

}

7. 创建控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

@RestController
public class ContentHandler {

@Autowired
private ContentService service;

private ContentVO contentvo = new ContentVO();
@RequestMapping(value = "/contents/{page}/{limit}")
public ResponseEntity<Object> findAll(@PathVariable("page") int page, @PathVariable("limit") int limit) {
contentvo.setData(service.findAll(page, limit));
return new ResponseEntity<>(contentvo, HttpStatus.OK);
}

@RequestMapping(value = "/contents")
public ResponseEntity<Object> findAll(){
return new ResponseEntity<>(service.findAll(),HttpStatus.OK);
}

@RequestMapping(value = "/content/{id}",method = RequestMethod.DELETE)
public ResponseEntity<Object> delete(@PathVariable("id") String id){
service.deleteById(id);
return new ResponseEntity<>("content be deleted successfully",HttpStatus.OK);
}

@RequestMapping(value = "/content/{id}",method = RequestMethod.POST)
public ResponseEntity<Object> save(@RequestBody Content content) {
service.save(content);
return new ResponseEntity<>("Post successfully",HttpStatus.OK);
}

@RequestMapping("/content/{id}")
public ResponseEntity<Object> find(@PathVariable("id") String id) {
return new ResponseEntity<>(service.findById(id),HttpStatus.OK);
}

@RequestMapping(value = "/content/{id}",method = RequestMethod.PUT)
public ResponseEntity<Object> update(@PathVariable("id") String id,@RequestBody Content content){
service.deleteById(id);
service.save(content);
return new ResponseEntity<>("Update successfully",HttpStatus.OK);
}
}

8. 项目结构

9. 测试

10. 地址

https://github.com/10ngui/SpringBootDemo

评论加载中