springboot 是我们java开发界的老牌开发框架,csdn上有一些基础的增删改查的教程详细但是不是很全,我自己再整合boot+mongodb的时候发现有些方法已经过时,或者是即将废弃的方法。 特总结如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
以上是我们boot+mongo需要引入的全部依赖。
package com.mongodbtest.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.bson.types.ObjectId;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Teacher {
private ObjectId id;
private String name;
}
以上部分是我的测试实体类,
```java
package com.mongodbtest.config;
import com.mongodbtest.entity.Teacher;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Component;
@Component
public interface TeacherRepository extends MongoRepository<Teacher, String> {
}
以上是我的mongodb操作类,
另外,
```java
```java
```java
package com.mongodbtest.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
@Configuration
public class Config {
@Bean
MongoTransactionManager transactionManager(MongoDatabaseFactory factory){
return new MongoTransactionManager(factory);
}
}
最主要的是自己实现mongodb事务的时候有很多网络上的config已经过时MongoFactory 你用这个去配置的时候会提示找不到type bean MongoFactory 这里是自己扒了源码看到的东西而且mongodb 官网也没有,这里是天坑(注意!!!!!!!!)
```java
package com.mongodbtest.entity;
import com.mongodbtest.config.TeacherRepository;
import com.mongodbtest.entity.Teacher;
import com.mongodbtest.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("teacher")
public class TeacherController {
@Autowired
MongoTemplate mongoTemplate;
@Autowired
TeacherService teacherService;
@PostMapping("teacher")
public Teacher saveTeacher(@RequestBody Teacher teacher ){
return teacherService.saveTeacherTemp(teacher);
}
@PostMapping("teacher001")
public Teacher saveTeacher001(@RequestBody Teacher teacher ){
return teacherService.saveTeacherTemp001(teacher);
}
}
以上是我的controller 这里没啥看的,会的都会。
@Transactional
public Teacher saveTeacherTemp001(Teacher teacher){
Teacher save = teacherRepository.save(teacher);
int io= 5/0;
System.out.println(io);
return save;
}
这里的@Transactional 加上去就可以同时要记得开启事务 否则你的事务还是不会生效(顺便吐槽一下mongodb api开发的是真的麻烦) 本来是学习quarkus的单文档事务的,没想到遇到山路十八弯,然后自己搭建一个mongdb 集群又整合了一下boot ,结论是boot 能很好的支持mongodb的事务,quarkus 就不是很好的支持! 希望大家的代码都没有bug;嘿嘿!
|