From dcb0ae46a715ad035d2d84739e114bb90cf236f7 Mon Sep 17 00:00:00 2001 From: javahongxi Date: Wed, 4 Dec 2019 01:42:46 +0800 Subject: [PATCH] spring-data-mongodb --- whatsmars-spring-data/pom.xml | 4 +++ .../whatsmars/spring/data/Application.java | 2 -- ...n.java => ElasticsearchConfiguration.java} | 2 +- .../spring/data/config/MogoConfiguration.java | 9 +++++++ .../whatsmars/spring/data/model/Customer.java | 27 +++++++++++++++++++ .../data/repository/CustomerRepository.java | 14 ++++++++++ .../spring/data/service/DataService.java | 6 +++++ .../resources/application-test.properties | 6 ++++- .../src/main/resources/spring-mongo.xml | 22 +++++++++++++++ 9 files changed, 88 insertions(+), 4 deletions(-) rename whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/{EsConfiguration.java => ElasticsearchConfiguration.java} (96%) create mode 100644 whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/MogoConfiguration.java create mode 100644 whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/model/Customer.java create mode 100644 whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/repository/CustomerRepository.java create mode 100644 whatsmars-spring-data/src/main/resources/spring-mongo.xml diff --git a/whatsmars-spring-data/pom.xml b/whatsmars-spring-data/pom.xml index 3dfa2e63..213ef7b6 100644 --- a/whatsmars-spring-data/pom.xml +++ b/whatsmars-spring-data/pom.xml @@ -20,6 +20,10 @@ org.springframework.data spring-data-elasticsearch + + org.springframework.data + spring-data-mongodb + org.hongxi diff --git a/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/Application.java b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/Application.java index b48aab16..07b58686 100644 --- a/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/Application.java +++ b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/Application.java @@ -1,14 +1,12 @@ package org.hongxi.whatsmars.spring.data; import org.hongxi.whatsmars.common.profile.ProfileUtils; -import org.hongxi.whatsmars.spring.data.config.PropertyConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Application { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - context.register(PropertyConfiguration.class); context.scan("org.hongxi.whatsmars.spring.data"); context.getEnvironment().setActiveProfiles(ProfileUtils.getProfile()); context.refresh(); diff --git a/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/EsConfiguration.java b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/ElasticsearchConfiguration.java similarity index 96% rename from whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/EsConfiguration.java rename to whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/ElasticsearchConfiguration.java index 6327336e..943d2f68 100644 --- a/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/EsConfiguration.java +++ b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/ElasticsearchConfiguration.java @@ -9,7 +9,7 @@ import org.springframework.data.elasticsearch.client.TransportClientFactoryBean; import java.util.Properties; @Configuration -public class EsConfiguration { +public class ElasticsearchConfiguration { @Value("${es.cluster.name}") private String clusterName; diff --git a/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/MogoConfiguration.java b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/MogoConfiguration.java new file mode 100644 index 00000000..fd4591ee --- /dev/null +++ b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/config/MogoConfiguration.java @@ -0,0 +1,9 @@ +package org.hongxi.whatsmars.spring.data.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@ImportResource("classpath:spring-mongo.xml") +public class MogoConfiguration { +} diff --git a/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/model/Customer.java b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/model/Customer.java new file mode 100644 index 00000000..a7d7abd7 --- /dev/null +++ b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/model/Customer.java @@ -0,0 +1,27 @@ +package org.hongxi.whatsmars.spring.data.model; + +import org.springframework.data.annotation.Id; + +public class Customer { + + @Id + private String id; + + private String firstName; + private String lastName; + + public Customer() { + } + + public Customer(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + @Override + public String toString() { + return String.format("Customer[id=%s, firstName='%s', lastName='%s']", id, + firstName, lastName); + } + +} \ No newline at end of file diff --git a/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/repository/CustomerRepository.java b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/repository/CustomerRepository.java new file mode 100644 index 00000000..4f8ff814 --- /dev/null +++ b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/repository/CustomerRepository.java @@ -0,0 +1,14 @@ +package org.hongxi.whatsmars.spring.data.repository; + +import org.hongxi.whatsmars.spring.data.model.Customer; +import org.springframework.data.mongodb.repository.MongoRepository; + +import java.util.List; + +public interface CustomerRepository extends MongoRepository { + + Customer findByFirstName(String firstName); + + List findByLastName(String lastName); + +} \ No newline at end of file diff --git a/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/service/DataService.java b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/service/DataService.java index abd68cac..4d118cc3 100644 --- a/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/service/DataService.java +++ b/whatsmars-spring-data/src/main/java/org/hongxi/whatsmars/spring/data/service/DataService.java @@ -1,6 +1,8 @@ package org.hongxi.whatsmars.spring.data.service; import org.elasticsearch.client.transport.TransportClient; +import org.hongxi.whatsmars.spring.data.repository.CustomerRepository; +import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @@ -13,4 +15,8 @@ public class DataService { private RedisTemplate redisTemplate; @Resource private TransportClient transportClient; + @Resource + private MongoTemplate mongoTemplate; + @Resource + private CustomerRepository customerRepository; } diff --git a/whatsmars-spring-data/src/main/resources/application-test.properties b/whatsmars-spring-data/src/main/resources/application-test.properties index 5c90a489..b8b51271 100644 --- a/whatsmars-spring-data/src/main/resources/application-test.properties +++ b/whatsmars-spring-data/src/main/resources/application-test.properties @@ -2,4 +2,8 @@ redis.host=127.0.0.1 redis.port=6379 es.cluster.name=elasticsearch -es.cluster.nodes=127.0.0.1:9300 \ No newline at end of file +es.cluster.nodes=127.0.0.1:9300 + +mongo.host=127.0.0.1 +mongo.port=27017 +mongo.db=test \ No newline at end of file diff --git a/whatsmars-spring-data/src/main/resources/spring-mongo.xml b/whatsmars-spring-data/src/main/resources/spring-mongo.xml new file mode 100644 index 00000000..17fc59ee --- /dev/null +++ b/whatsmars-spring-data/src/main/resources/spring-mongo.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + \ No newline at end of file -- GitLab