mirror of
https://github.com/ityouknow/spring-boot-examples.git
synced 2026-09-03 07:27:38 +08:00
add Spring Boot 3.0 MongoDB axamples
This commit is contained in:
11
README.md
11
README.md
@@ -31,11 +31,18 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准,此开
|
||||
- [spring-boot-web](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-web):Spring Boot 3.0 web 示例
|
||||
- [spring-boot-redis](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-redis):Spring Boot 3.0 Redis 示例
|
||||
- [spring-boot-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-thymeleaf):Spring Boot 3.0 Thymeleaf 语法、布局使用示例
|
||||
- [spring-boot-jpa](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa):Spring Boot 3.0 Jpa 操作、多数据源使用示例
|
||||
- [spring-boot-mybatis](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis):Spring Boot 3.0 Mybatis 注解、xml 使用、多数据源使用示例
|
||||
- [spring-boot-jpa](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa):Spring Boot 3.0 Jpa 操作、增删、改查多数据源使用示例
|
||||
- [spring-boot-mybatis](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis):Spring Boot 3.0 Mybatis 注解、xml 使用、增删改查、多数据源使用示例
|
||||
- [spring-boot-rabbitmq](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-rabbitmq):Spring Boot 3.0 RabbitMQ 各种常见场景使用示例
|
||||
- [spring-boot-scheduler](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-scheduler):Spring Boot 3.0 定时任务 scheduler 使用示例
|
||||
- [spring-boot-mail](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mail):Spring Boot 3.0 邮件发送使用示例
|
||||
- [spring-boot-mongodb](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mongodb):Spring Boot 3.0 MongoDB 增删改查示例 多数据源使用案例
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
> 如果大家想了解关于 Spring Boot 的其它方面应用,也可以以[issues](https://github.com/ityouknow/spring-boot-examples/issues)的形式反馈给我,我后续来完善。
|
||||
|
||||
|
||||
58
spring-boot-mongodb/spring-boot-mongodb/pom.xml
Normal file
58
spring-boot-mongodb/spring-boot-mongodb/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.neo</groupId>
|
||||
<artifactId>spring-boot-mongodb</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-boot-mongodb</name>
|
||||
<description>Demo project for Spring Boot and mongodb</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
|
||||
<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-test</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.neo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MongoDBApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MongoDBApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.neo.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Created by summer on 2017/5/5.
|
||||
*/
|
||||
public class User implements Serializable {
|
||||
private static final long serialVersionUID = -3258839839160856613L;
|
||||
private Long id;
|
||||
private String userName;
|
||||
private String passWord;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserEntity{" +
|
||||
"id=" + id +
|
||||
", userName='" + userName + '\'' +
|
||||
", passWord='" + passWord + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.neo.repository;
|
||||
|
||||
import com.neo.model.User;
|
||||
|
||||
/**
|
||||
* Created by summer on 2017/5/5.
|
||||
*/
|
||||
public interface UserRepository {
|
||||
|
||||
public void saveUser(User user);
|
||||
|
||||
public User findUserByUserName(String userName);
|
||||
|
||||
public long updateUser(User user);
|
||||
|
||||
public void deleteUserById(Long id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.neo.repository.impl;
|
||||
|
||||
import com.mongodb.client.result.UpdateResult;
|
||||
import com.neo.repository.UserRepository;
|
||||
import com.neo.model.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Created by summer on 2017/5/5.
|
||||
*/
|
||||
@Component
|
||||
public class UserRepositoryImpl implements UserRepository {
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
/**
|
||||
* 创建对象
|
||||
* @param user
|
||||
*/
|
||||
@Override
|
||||
public void saveUser(User user) {
|
||||
mongoTemplate.save(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户名查询对象
|
||||
* @param userName
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public User findUserByUserName(String userName) {
|
||||
Query query=new Query(Criteria.where("userName").is(userName));
|
||||
User user = mongoTemplate.findOne(query , User.class);
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新对象
|
||||
* @param user
|
||||
*/
|
||||
@Override
|
||||
public long updateUser(User user) {
|
||||
Query query=new Query(Criteria.where("id").is(user.getId()));
|
||||
Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());
|
||||
//更新查询返回结果集的第一条
|
||||
UpdateResult result =mongoTemplate.updateFirst(query,update,User.class);
|
||||
//更新查询返回结果集的所有
|
||||
// mongoTemplate.updateMulti(query,update,UserEntity.class);
|
||||
if(result!=null)
|
||||
return result.getMatchedCount();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除对象
|
||||
* @param id
|
||||
*/
|
||||
@Override
|
||||
public void deleteUserById(Long id) {
|
||||
Query query=new Query(Criteria.where("id").is(id));
|
||||
mongoTemplate.remove(query,User.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
spring.application.name=spring-boot-mongodb
|
||||
|
||||
spring.data.mongodb.uri=mongodb://119.0.0.6:27017/test
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.neo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class MongoDBApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
System.out.println("hello world");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.neo.repository;
|
||||
|
||||
import com.neo.model.User;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* Created by summer on 2017/5/5.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class UserRepositoryTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userDao;
|
||||
|
||||
@Test
|
||||
public void testSaveUser() throws Exception {
|
||||
User user=new User();
|
||||
user.setId(2l);
|
||||
user.setUserName("小明");
|
||||
user.setPassWord("fffooo123");
|
||||
userDao.saveUser(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findUserByUserName(){
|
||||
User user= userDao.findUserByUserName("小明");
|
||||
System.out.println("user is "+user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUser(){
|
||||
User user=new User();
|
||||
user.setId(2l);
|
||||
user.setUserName("天空");
|
||||
user.setPassWord("fffxxxx");
|
||||
userDao.updateUser(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteUserById(){
|
||||
userDao.deleteUserById(1l);
|
||||
}
|
||||
|
||||
}
|
||||
58
spring-boot-mongodb/spring-boot-multi-mongodb/pom.xml
Normal file
58
spring-boot-mongodb/spring-boot-multi-mongodb/pom.xml
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.neo</groupId>
|
||||
<artifactId>spring-boot-multi-mongodb</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-boot-multi-mongodb</name>
|
||||
<description>Demo project for Spring Boot and multi mongodb</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
|
||||
<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-test</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.neo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MultiMongodbApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MultiMongodbApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.neo.config;
|
||||
|
||||
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
import com.neo.config.props.MultipleMongoProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.mongodb.MongoDatabaseFactory;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class MultipleMongoConfig {
|
||||
|
||||
@Autowired
|
||||
private MultipleMongoProperties mongoProperties;
|
||||
|
||||
@Primary
|
||||
@Bean(name = "primaryMongoTemplate")
|
||||
public MongoTemplate primaryMongoTemplate() throws Exception {
|
||||
return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Qualifier("secondaryMongoTemplate")
|
||||
public MongoTemplate secondaryMongoTemplate() throws Exception {
|
||||
return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public MongoDatabaseFactory primaryFactory(MongoProperties mongo) throws Exception {
|
||||
MongoClient client = MongoClients.create(mongo.getUri());
|
||||
return new SimpleMongoClientDatabaseFactory(client, mongoProperties.getPrimary().getDatabase());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoDatabaseFactory secondaryFactory(MongoProperties mongo) throws Exception {
|
||||
MongoClient client = MongoClients.create(mongo.getUri());
|
||||
return new SimpleMongoClientDatabaseFactory(client, mongoProperties.getSecondary().getDatabase());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.neo.config;
|
||||
|
||||
import com.neo.config.props.MultipleMongoProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(MultipleMongoProperties.class)
|
||||
@EnableMongoRepositories(basePackages = "com.neo.repository.primary",
|
||||
mongoTemplateRef = "primaryMongoTemplate")
|
||||
public class PrimaryMongoConfig {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.neo.config;
|
||||
|
||||
import com.neo.config.props.MultipleMongoProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(MultipleMongoProperties.class)
|
||||
@EnableMongoRepositories(basePackages = "com.neo.repository.secondary",
|
||||
mongoTemplateRef = "secondaryMongoTemplate")
|
||||
public class SecondaryMongoConfig {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.neo.config.props;
|
||||
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(prefix = "mongodb")
|
||||
public class MultipleMongoProperties {
|
||||
|
||||
private MongoProperties primary = new MongoProperties();
|
||||
private MongoProperties secondary = new MongoProperties();
|
||||
|
||||
public MongoProperties getPrimary() {
|
||||
return primary;
|
||||
}
|
||||
|
||||
public void setPrimary(MongoProperties primary) {
|
||||
this.primary = primary;
|
||||
}
|
||||
|
||||
public MongoProperties getSecondary() {
|
||||
return secondary;
|
||||
}
|
||||
|
||||
public void setSecondary(MongoProperties secondary) {
|
||||
this.secondary = secondary;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.neo.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
|
||||
public class User implements Serializable {
|
||||
private static final long serialVersionUID = -3258839839160856613L;
|
||||
private String id;
|
||||
private String userName;
|
||||
private String passWord;
|
||||
|
||||
public User(String userName, String passWord) {
|
||||
this.userName = userName;
|
||||
this.passWord = passWord;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id='" + id + '\'' +
|
||||
", userName='" + userName + '\'' +
|
||||
", passWord='" + passWord + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.neo.repository.primary;
|
||||
|
||||
import com.neo.model.User;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
/**
|
||||
* @author neo
|
||||
*/
|
||||
public interface PrimaryRepository extends MongoRepository<User, String> {
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.neo.repository.secondary;
|
||||
|
||||
import com.neo.model.User;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
/**
|
||||
* @author neo
|
||||
*/
|
||||
public interface SecondaryRepository extends MongoRepository<User, String> {
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
spring.application.name=spring-boot-multi-mongodb
|
||||
|
||||
mongodb.primary.uri=mongodb://119.0.0.6:27017
|
||||
mongodb.primary.database=primary
|
||||
mongodb.secondary.uri=mongodb://119.0.0.6:27017
|
||||
mongodb.secondary.database=secondary
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.neo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class MultiMongodbApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
System.out.println("hello world");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.neo.repository;
|
||||
|
||||
import com.neo.model.User;
|
||||
import com.neo.repository.primary.PrimaryRepository;
|
||||
import com.neo.repository.secondary.SecondaryRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class MuliDatabaseTest {
|
||||
|
||||
@Autowired
|
||||
private PrimaryRepository primaryRepository;
|
||||
|
||||
@Autowired
|
||||
private SecondaryRepository secondaryRepository;
|
||||
|
||||
@Test
|
||||
public void TestSave() {
|
||||
System.out.println("************************************************************");
|
||||
System.out.println("测试开始");
|
||||
System.out.println("************************************************************");
|
||||
this.primaryRepository.save(new User("小张", "123456"));
|
||||
this.secondaryRepository.save(new User("小王", "654321"));
|
||||
List<User> primaries = this.primaryRepository.findAll();
|
||||
for (User primary : primaries) {
|
||||
System.out.println(primary.toString());
|
||||
}
|
||||
List<User> secondaries = this.secondaryRepository.findAll();
|
||||
for (User secondary : secondaries) {
|
||||
System.out.println(secondary.toString());
|
||||
}
|
||||
System.out.println("************************************************************");
|
||||
System.out.println("测试完成");
|
||||
System.out.println("************************************************************");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user