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相同

上一篇:使用Spring管理数据库事务


下一篇:spring整合mongodb