此篇文章讲述了如何使用springboot框架搭建后端并连接mongo数据库
文章目录
1. Mongo数据库
Mongo是一种key-value类型的数据库,nosql的特点弱化了数据之间的关系,易于拓展
mongodb版本为v4.2.3(具体下载方法这里暂不赘述)
在控制台打开mongod,命令行:sudo mongod
打开可视化工具mongoDB Compass方面导入和查看数据
导入数据,采用json格式或CSV导入,本文采用json格式导入,数据例如下
{"cid":2020001 , "name":"奶茶","describe":"可加珍珠和波波球","remark":"五分糖"}
{"cid":2020002 , "name":"奶盖","describe":"加满不胖","remark":null}
2. Springboot 框架搭建
2.1 pom.xml相关配置(maven配置)
<?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 https://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.3.2.RELEASE</version> -->
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- com.example.demo -->
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Demo</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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 application.yml配置
- mongo配置
- http端口配置
spring:
http:
encoding:
charset: UTF-8
enabled: true
force: true
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
application:
name: ${SPRING_APPLICATION_NAME:Demo}
data:
mongodb:
uri: mongodb://${MONGO_URI:localhost}:27017/database
banner:
charset: UTF-8
server:
servlet:
context-path: /
port: ${TOMCAT_PORT:8080}
tomcat:
uri-encoding: UTF-8
ssl:
enabled: false
2.3 Bean注入
因为在项目中导入了lombok包,可以省略不写get
和set
方法,使代码更加简洁,需要注意的是如果项目合作时,如果使用这个方法,要求所有人都有加载这个包才能使用。
注解@Document
中声明了连接数据库中collection的名称;@Id
强调自己定义的id,因为mongo导入数据会自生成一个id号,调用getId方法会产生混乱
import lombok.Data;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection="category")
@Data
public class Category {
@Id
private ObjectId id;
private String name;
private String describe;
private String remark;
}
2.4 Service层
service写了一个获取所有数据的方法,继承自定义接口方法
import com.example.demo.document.Category;
import com.example.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {
private final MongoTemplate mongoTemplate;
public MongoTemplate getMongoTemplate() {
if (this.mongoTemplate == null) {
System.out.println("----mongoTemplate注入失败----");
}
return mongoTemplate;
}
@Autowired
public CategoryServiceImpl(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
public CategoryServiceImpl(){
this.mongoTemplate=null;
}
@Override
public List<Category> getAllCategories(){
return getMongoTemplate().findAll(Category.class);
}
}
2.5 controller层
调用service层声明的方法,包装成数据包通过http传输
import com.example.demo.document.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import net.sf.json.JSONArray;
@RestController
@RequestMapping("/testDemo")
public class CategoryController {
private CategoryService categoryService;
@Autowired
public CategoryController(CategoryService categoryService) {
this.categoryService = categoryService;
}
@GetMapping("/getTestdata")
@ResponseBody
public JSONArray getAllCategories(){
JSONArray array = new JSONArray();
try{
List<Category> list =categoryController.getAllCategories();
if(list.isEmpty()){
String data="value is null";
array.add(data);
}
for(Category data : list){
array.add(data);
}
return array;
}
catch (Exception e){
array.add("error message : "+e.getMessage());
return array;
}
}
}
3. 测试数据
通过http请求获取存储在mongo中的数据
- 测试工具:Postman
本文章为作者原创,如有不足还请不吝赐教,如需转载请注明出处