MongoDB与SpringBoot整合(支持事务)

Posted vettel0329

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB与SpringBoot整合(支持事务)相关的知识,希望对你有一定的参考价值。

1.创建SpringBoot工程,选择 Web、MonogDB 依赖,pom如下:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    ......

    <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-web</artifactId>
        </dependency>

        ......

    </dependencies>

    ......

 

 

2.配置yml文件

# 服务端口号
server:
  port: 8081

spring:
  # 服务名称
  application:
    name: test-springboot-mongo
  #数据库
  data:
    mongodb:
      uri: mongodb://localhost:27017/testmongo

 

 

3.创建配置类,开启事务

@Configuration
public class TransactionConfig {

    @Bean
    MongoTransactionManager transactionManager(MongoDbFactory factory){
        return new MongoTransactionManager(factory);
    }

}

 

 

4.创建Entity实体类

public class BaseEntity {

    @Id
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
@Document
public class User extends BaseEntity {

    private String userName;
    private String passWord;
    @DBRef
    private UserInfo userInfo;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }
}
@Document
public class UserInfo extends BaseEntity {

    private int age;
    private String nickName;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}

 

 

5.创建service

@Service
public class UserService {

    @Resource
    MongoTemplate mongotemplate;

    public List<User> getAllUser(){
        return mongotemplate.findAll(User.class);
    }

    @Transactional
    public boolean addUser(User user, UserInfo userInfo){
        user.setUserInfo(userInfo);
        mongotemplate.save(userInfo);
//        int a = 1/0;    //事务测试代码
        mongotemplate.save(user);
        return true;
    }

}

 

 

6.创建controller

@RestController
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping(value = "/getAllUser")
    public List<User> getAllUser(){
        return userService.getAllUser();
    }

    @RequestMapping(value = "/addUser")
    public boolean addUser(User user, UserInfo userInfo){
        userService.addUser(user, userInfo);
        return true;
    }

}

 

 

7.运行 SpringBootApplication 启动服务,在浏览器中输入 " http://localhost:8081/addUser?userName=z3&passWord=123&age=12&nickName=zhangsan " 测试

@SpringBootApplication
public class TestspringmongoApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestspringmongoApplication.class, args);
    }

}

 

8.注:以上为MongoTemplate方式,MongoTemplate和MongoRepository方式都与Spring整合SpringDataMongoDB相同

 

以上是关于MongoDB与SpringBoot整合(支持事务)的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合RocketMQ

SpringBoot整合MongoDB实战

此客户端所连接的 MongoDB 集群不支持会话-springboot-mongodb-transactions

SpringBoot整合MongoDB教程

SpringBoot整合MongoDB教程

MongoDB简介与应用场景Docker安装Mongo整合SpringBoot实现CRUD