diff --git a/README.md b/README.md index 3b1d5fa..3749ea5 100644 --- a/README.md +++ b/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)的形式反馈给我,我后续来完善。 diff --git a/spring-boot-mongodb/spring-boot-mongodb/pom.xml b/spring-boot-mongodb/spring-boot-mongodb/pom.xml new file mode 100644 index 0000000..ed9d7c5 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-mongodb/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.neo + spring-boot-mongodb + 1.0.0 + jar + + spring-boot-mongodb + Demo project for Spring Boot and mongodb + + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + + + UTF-8 + 17 + + + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + org.springframework.boot + spring-boot-starter-test + + + org.junit.vintage + junit-vintage-engine + test + + + org.hamcrest + hamcrest-core + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/MongoDBApplication.java b/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/MongoDBApplication.java new file mode 100644 index 0000000..b8c63d5 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/MongoDBApplication.java @@ -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); + } +} diff --git a/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/model/User.java b/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/model/User.java new file mode 100644 index 0000000..c15340a --- /dev/null +++ b/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/model/User.java @@ -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 + '\'' + + '}'; + } +} diff --git a/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/repository/UserRepository.java b/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/repository/UserRepository.java new file mode 100644 index 0000000..e1a3d41 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/repository/UserRepository.java @@ -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); + +} diff --git a/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/repository/impl/UserRepositoryImpl.java b/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/repository/impl/UserRepositoryImpl.java new file mode 100644 index 0000000..8c96f5a --- /dev/null +++ b/spring-boot-mongodb/spring-boot-mongodb/src/main/java/com/neo/repository/impl/UserRepositoryImpl.java @@ -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); + } +} diff --git a/spring-boot-mongodb/spring-boot-mongodb/src/main/resources/application.properties b/spring-boot-mongodb/spring-boot-mongodb/src/main/resources/application.properties new file mode 100644 index 0000000..d2c1017 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-mongodb/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.application.name=spring-boot-mongodb + +spring.data.mongodb.uri=mongodb://119.0.0.6:27017/test + + diff --git a/spring-boot-mongodb/spring-boot-mongodb/src/test/java/com/neo/MongoDBApplicationTests.java b/spring-boot-mongodb/spring-boot-mongodb/src/test/java/com/neo/MongoDBApplicationTests.java new file mode 100644 index 0000000..cd30276 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-mongodb/src/test/java/com/neo/MongoDBApplicationTests.java @@ -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"); + } + +} diff --git a/spring-boot-mongodb/spring-boot-mongodb/src/test/java/com/neo/repository/UserRepositoryTest.java b/spring-boot-mongodb/spring-boot-mongodb/src/test/java/com/neo/repository/UserRepositoryTest.java new file mode 100644 index 0000000..cb6372f --- /dev/null +++ b/spring-boot-mongodb/spring-boot-mongodb/src/test/java/com/neo/repository/UserRepositoryTest.java @@ -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); + } + +} diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/pom.xml b/spring-boot-mongodb/spring-boot-multi-mongodb/pom.xml new file mode 100644 index 0000000..2c9b73c --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.neo + spring-boot-multi-mongodb + 1.0.0 + jar + + spring-boot-multi-mongodb + Demo project for Spring Boot and multi mongodb + + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + + + UTF-8 + 17 + + + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + org.springframework.boot + spring-boot-starter-test + + + org.junit.vintage + junit-vintage-engine + test + + + org.hamcrest + hamcrest-core + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/MultiMongodbApplication.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/MultiMongodbApplication.java new file mode 100644 index 0000000..e33c97d --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/MultiMongodbApplication.java @@ -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); + } +} diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/MultipleMongoConfig.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/MultipleMongoConfig.java new file mode 100644 index 0000000..d047711 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/MultipleMongoConfig.java @@ -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()); + } +} \ No newline at end of file diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/PrimaryMongoConfig.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/PrimaryMongoConfig.java new file mode 100644 index 0000000..076bc33 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/PrimaryMongoConfig.java @@ -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 { +} diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/SecondaryMongoConfig.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/SecondaryMongoConfig.java new file mode 100644 index 0000000..f07149b --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/SecondaryMongoConfig.java @@ -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 { + +} diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/props/MultipleMongoProperties.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/props/MultipleMongoProperties.java new file mode 100644 index 0000000..5bf4ff6 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/config/props/MultipleMongoProperties.java @@ -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; + } +} diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/model/User.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/model/User.java new file mode 100644 index 0000000..b5cd0a8 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/model/User.java @@ -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 + '\'' + + '}'; + } +} diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/repository/primary/PrimaryRepository.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/repository/primary/PrimaryRepository.java new file mode 100644 index 0000000..50877eb --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/repository/primary/PrimaryRepository.java @@ -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 { +} diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/repository/secondary/SecondaryRepository.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/repository/secondary/SecondaryRepository.java new file mode 100644 index 0000000..11df915 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/java/com/neo/repository/secondary/SecondaryRepository.java @@ -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 { +} diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/resources/application.properties b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/resources/application.properties new file mode 100644 index 0000000..5884b49 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/main/resources/application.properties @@ -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 + + diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/test/java/com/neo/MultiMongodbApplicationTests.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/test/java/com/neo/MultiMongodbApplicationTests.java new file mode 100644 index 0000000..8f59e58 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/test/java/com/neo/MultiMongodbApplicationTests.java @@ -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"); + } + +} diff --git a/spring-boot-mongodb/spring-boot-multi-mongodb/src/test/java/com/neo/repository/MuliDatabaseTest.java b/spring-boot-mongodb/spring-boot-multi-mongodb/src/test/java/com/neo/repository/MuliDatabaseTest.java new file mode 100644 index 0000000..42bf1b4 --- /dev/null +++ b/spring-boot-mongodb/spring-boot-multi-mongodb/src/test/java/com/neo/repository/MuliDatabaseTest.java @@ -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 primaries = this.primaryRepository.findAll(); + for (User primary : primaries) { + System.out.println(primary.toString()); + } + List secondaries = this.secondaryRepository.findAll(); + for (User secondary : secondaries) { + System.out.println(secondary.toString()); + } + System.out.println("************************************************************"); + System.out.println("测试完成"); + System.out.println("************************************************************"); + } + +}