1-整体目录如下:
2-实体类:在实体类层配置表名
package com.example.test.entity;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
@Data
@Document(collection = "test") //表名
public class MongoTest implements Serializable {
private Integer id;
private Integer age;
private String name;
public MongoTest() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MongoTest(Integer id, Integer age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "MongoTest{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
3-配置controller层:
package com.example.test.controller;
import com.example.test.entity.MongoTest;
import com.example.test.service.MongoTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Validated
@RestController
@RequestMapping("Mongo")
public class MongoTestC {
@Autowired
private MongoTestService mongoTestService;
@GetMapping(value = "/test1")
public void saveTest(){
List<MongoTest> list = new ArrayList<MongoTest>();
MongoTest mgtest = new MongoTest();
mgtest.setId(1111);
mgtest.setAge(3223);
mgtest.setName("111测试");
list.add(mgtest);
System.out.println(mgtest);
mongoTestService.saveTest(mgtest);
}
}
4-配置service层和ServiceImpl层:
package com.example.test.service;
import com.example.test.entity.MongoTest;
import org.springframework.stereotype.Service;
@Service
public interface MongoTestService {
/**
* 创建对象
* @param mgtest
*/
public void saveTest(MongoTest mgtest);
}
package com.example.test.service.serviceImpl;
import com.example.test.entity.MongoTest;
import com.example.test.service.MongoTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service("MongoTestService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MongoTestServiceImpl implements MongoTestService {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 创建对象
* @param mgtest
*/
@Override
public void saveTest(MongoTest mgtest) {
mongoTemplate.save(mgtest);
}
}
5- 配置文件的配置:
配置方法1:
spring:
data:
mongodb:
uri: mongodb://localhost:27017/test
#本地数据库配置直接输入地址和端口号就行,不用输入密码,我才用的是这种方式
配置方法2:输入相关的地址和用户名和密码
#地址
spring.data.mongodb.host=127.0.0.1
#端口
spring.data.mongodb.port=27017
#认证的库
spring.data.mongodb.authentication-database=admin
#操作的库
spring.data.mongodb.database=test
#账号
spring.data.mongodb.username=root
#密码 此时 @符号不替换也可以
spring.data.mongodb.password=123456
6-数据库设置:使用navicat设置;
7-测试连接:成功调通