Merge pull request #81 from Microsoft/update-samplecode-with-more-feature
Update samplecode with more feature covered.
This commit is contained in:
Коммит
d2e5eb3771
|
@ -0,0 +1,22 @@
|
|||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2018 Pan Li
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,72 @@
|
|||
<?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>
|
||||
|
||||
<artifactId>spring-data-documentdb-example</artifactId>
|
||||
<name>Spring Data DocumentDb - Example</name>
|
||||
|
||||
<parent>
|
||||
<groupId>pli.spring.data.documentdb.examples</groupId>
|
||||
<artifactId>pli-spring-data-documentdb-examples</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>spring-data-documentdb</artifactId>
|
||||
<version>2.0.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-rest</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-rest-webmvc</artifactId>
|
||||
<version>3.0.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<!-- Build an executable JAR -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>example.springdata.documentdb.Application</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright 2015-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.documentdb;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
private String postalCode;
|
||||
|
||||
private String street;
|
||||
|
||||
private String city;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s, %s, %s", this.street, this.city, this.postalCode);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright 2015-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.documentdb;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.Arrays;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
private static final String ID = "0123456789";
|
||||
private static final String EMAIL = "xxx-xx@xxx.com";
|
||||
private static final String NAME = "myName";
|
||||
private static final String POSTAL_CODE = "0123456789";
|
||||
private static final String STREET = "zixing road";
|
||||
private static final String CITY = "shanghai";
|
||||
private static final String ROLE_CREATOR = "creator";
|
||||
private static final String ROLE_CONTRIBUTOR = "contributor";
|
||||
private static final int COST_CREATOR = 234;
|
||||
private static final int COST_CONTRIBUTOR = 666;
|
||||
|
||||
private final Address address = new Address(POSTAL_CODE, STREET, CITY);
|
||||
private final Role creator = new Role(ROLE_CREATOR, COST_CREATOR);
|
||||
private final Role contributor = new Role(ROLE_CONTRIBUTOR, COST_CONTRIBUTOR);
|
||||
private final User user = new User(ID, EMAIL, NAME, address, Arrays.asList(creator, contributor));
|
||||
|
||||
@Autowired
|
||||
private UserRepository repository;
|
||||
|
||||
public static void main(String... args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void setup() {
|
||||
this.repository.save(user);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void cleanup() {
|
||||
this.repository.deleteAll();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.documentdb;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigurationProperties(prefix = "azure.documentdb")
|
||||
public class DocumentDbProperties {
|
||||
|
||||
private String uri;
|
||||
|
||||
private String key;
|
||||
|
||||
private String database;
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright 2015-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.documentdb;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Setter
|
||||
@Getter
|
||||
public class Role {
|
||||
|
||||
private String name;
|
||||
|
||||
int cost;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright 2015-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example.springdata.documentdb;
|
||||
|
||||
import com.microsoft.azure.documentdb.IndexingMode;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.Document;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.DocumentIndexingPolicy;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Document(collection = "mycollection")
|
||||
@DocumentIndexingPolicy(mode = IndexingMode.Lazy)
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String email;
|
||||
|
||||
private String name;
|
||||
|
||||
private Address address;
|
||||
|
||||
private List<Role> roleList;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s: %s %s %s", this.id, this.email, this.name, this.address.toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright 2015-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example.springdata.documentdb;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.DocumentDbRepository;
|
||||
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
@RepositoryRestResource(collectionResourceRel = "user", path = "user")
|
||||
public interface UserRepository extends DocumentDbRepository<User, String> {
|
||||
|
||||
List<User> findByName(String firstName);
|
||||
|
||||
List<User> findByEmailAndAddress(String email, Address address);
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.documentdb;
|
||||
|
||||
import com.microsoft.azure.documentdb.ConnectionPolicy;
|
||||
import com.microsoft.azure.documentdb.ConsistencyLevel;
|
||||
import com.microsoft.azure.documentdb.DocumentClient;
|
||||
import com.microsoft.azure.spring.data.documentdb.config.AbstractDocumentDbConfiguration;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.config.EnableDocumentDbRepositories;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableDocumentDbRepositories
|
||||
@EnableConfigurationProperties(DocumentDbProperties.class)
|
||||
@PropertySource("classpath:application.properties")
|
||||
public class UserRepositoryConfiguration extends AbstractDocumentDbConfiguration {
|
||||
|
||||
@Autowired
|
||||
private DocumentDbProperties properties;
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public DocumentClient documentClient() {
|
||||
return new DocumentClient(this.properties.getUri(), this.properties.getKey(),
|
||||
ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDatabase() {
|
||||
return this.properties.getDatabase();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
azure.documentdb.uri=your-documentDb-uri
|
||||
azure.documentdb.key=your-documentDb-key
|
||||
azure.documentdb.database=your-documentDb-dbName
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Copyright 2015-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.documentdb;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {UserRepositoryConfiguration.class})
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
private static final String ID = "0123456789";
|
||||
private static final String EMAIL = "xxx-xx@xxx.com";
|
||||
private static final String NAME = "myName";
|
||||
private static final String POSTAL_CODE = "0123456789";
|
||||
private static final String STREET = "zixing road";
|
||||
private static final String CITY = "shanghai";
|
||||
private static final String ROLE_CREATOR = "creator";
|
||||
private static final String ROLE_CONTRIBUTOR = "contributor";
|
||||
private static final int COST_CREATOR = 234;
|
||||
private static final int COST_CONTRIBUTOR = 666;
|
||||
|
||||
@Autowired
|
||||
private UserRepository repository;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.repository.deleteAll();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
this.repository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserRepository() {
|
||||
final Address address = new Address(POSTAL_CODE, STREET, CITY);
|
||||
final Role creator = new Role(ROLE_CREATOR, COST_CREATOR);
|
||||
final Role contributor = new Role(ROLE_CONTRIBUTOR, COST_CONTRIBUTOR);
|
||||
final User user = new User(ID, EMAIL, NAME, address, Arrays.asList(creator, contributor));
|
||||
|
||||
this.repository.save(user);
|
||||
|
||||
// Test for findById
|
||||
User result = this.repository.findById(ID).get();
|
||||
Assert.notNull(result, "should be exist in database");
|
||||
Assert.isTrue(result.getId().equals(ID), "should be the same id");
|
||||
|
||||
// Test for findByName
|
||||
List<User> resultList = this.repository.findByName(user.getName());
|
||||
Assert.isTrue(resultList.size() == 1, "should be only one user here");
|
||||
Assert.isTrue(resultList.get(0).getName().equals(user.getName()), "should be same Name");
|
||||
Assert.notNull(result.getRoleList(), "roleList should not be null");
|
||||
Assert.isTrue(result.getRoleList().size() == user.getRoleList().size(), "must be the same list size");
|
||||
|
||||
for (int i = 0; i < user.getRoleList().size(); i++) {
|
||||
final Role role = result.getRoleList().get(i);
|
||||
final Role roleReference = user.getRoleList().get(i);
|
||||
|
||||
Assert.isTrue(role.getName().equals(roleReference.getName()), "should be the same role name");
|
||||
Assert.isTrue(role.getCost() == roleReference.getCost(), "should be the same role cost");
|
||||
}
|
||||
|
||||
// Test for findByEmailAndAddress
|
||||
resultList = this.repository.findByEmailAndAddress(user.getEmail(), user.getAddress());
|
||||
Assert.isTrue(resultList.size() == 1, "should be only one user here");
|
||||
|
||||
result = resultList.get(0);
|
||||
Assert.isTrue(result.getEmail().equals(user.getEmail()), "should be same Email");
|
||||
Assert.isTrue(result.getAddress().getPostalCode().equals(user.getAddress().getPostalCode()),
|
||||
"should be same postalCode");
|
||||
Assert.isTrue(result.getAddress().getCity().equals(user.getAddress().getCity()), "should be same City");
|
||||
Assert.isTrue(result.getAddress().getStreet().equals(user.getAddress().getStreet()), "should be same street");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
azure.documentdb.uri=your-documentDb-uri
|
||||
azure.documentdb.key=your-documentDb-key
|
||||
azure.documentdb.database=your-documentDb-dbName
|
|
@ -1,133 +1,41 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>spring-data-azure-cosmodb-documentdb-sample</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<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>
|
||||
|
||||
<name>Spring Data Azure Cosmos DB DocumentDB Sample</name>
|
||||
<description>Sample project for Spring Data Azure Cosmos DB DocumentDB</description>
|
||||
<url>https://github.com/Microsoft/spring-data-documentdb</url>
|
||||
<groupId>pli.spring.data.documentdb.examples</groupId>
|
||||
<artifactId>pli-spring-data-documentdb-examples</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>MIT</name>
|
||||
<url>https://github.com/Microsoft/spring-data-documentdb/blob/master/LICENSE</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<id>yungez</id>
|
||||
<name>yunge zhu</name>
|
||||
<email>yungez@microsoft.com</email>
|
||||
</developer>
|
||||
</developers>
|
||||
<name>Spring Data DocumentDb - Examples</name>
|
||||
<description>Sample projects for Spring Data DocumentDb</description>
|
||||
|
||||
<scm>
|
||||
<connection>scm:git:git://github.com/Microsoft/spring-data-documentdb.git</connection>
|
||||
<developerConnection>scm:git:ssh://github.com:Microsoft/spring-data-documentdb.git
|
||||
</developerConnection>
|
||||
<url>https://github.com/Microsoft/spring-data-documentdb/tree/master</url>
|
||||
</scm>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring.data.documentdb.version>2.0.2</spring.data.documentdb.version>
|
||||
</properties>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.microsoft.azure</groupId>
|
||||
<artifactId>spring-data-documentdb</artifactId>
|
||||
<version>${spring.data.documentdb.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
<version>2.17</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>validate</id>
|
||||
<phase>validate</phase>
|
||||
<configuration>
|
||||
<configLocation>${project.basedir}/../config/checkstyle.xml</configLocation>
|
||||
<encoding>UTF-8</encoding>
|
||||
<consoleOutput>true</consoleOutput>
|
||||
<failsOnError>true</failsOnError>
|
||||
<failOnViolation>true</failOnViolation>
|
||||
<includeTestSourceDirectory>true</includeTestSourceDirectory>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<linkXRef>false</linkXRef>
|
||||
</configuration>
|
||||
<inherited>true</inherited>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>findbugs-maven-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<configuration>
|
||||
<effort>Max</effort>
|
||||
<threshold>Low</threshold>
|
||||
<xmlOutput>true</xmlOutput>
|
||||
<findbugsXmlOutputDirectory>${project.build.directory}/findbugs
|
||||
</findbugsXmlOutputDirectory>
|
||||
<excludeFilterFile>${project.basedir}/../config/findbugs-exclude.xml</excludeFilterFile>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.ant</groupId>
|
||||
<artifactId>ant</artifactId>
|
||||
<version>1.9.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<modules>
|
||||
<module>example</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
|
@ -1,37 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.sample;
|
||||
|
||||
import com.microsoft.azure.documentdb.ConnectionPolicy;
|
||||
import com.microsoft.azure.documentdb.ConsistencyLevel;
|
||||
import com.microsoft.azure.documentdb.DocumentClient;
|
||||
import com.microsoft.azure.spring.data.documentdb.config.AbstractDocumentDbConfiguration;
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.config.EnableDocumentDbRepositories;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableDocumentDbRepositories
|
||||
public class AppConfiguration extends AbstractDocumentDbConfiguration {
|
||||
|
||||
@Value("${azure.documentdb.uri}")
|
||||
private String uri;
|
||||
|
||||
@Value("${azure.documentdb.key}")
|
||||
private String key;
|
||||
|
||||
@Value("${azure.documentdb.database}")
|
||||
private String dbName;
|
||||
|
||||
public DocumentClient documentClient() {
|
||||
return new DocumentClient(uri, key, ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);
|
||||
}
|
||||
|
||||
public String getDatabase() {
|
||||
return dbName;
|
||||
}
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.sample;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleApplication implements CommandLineRunner {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SampleApplication.class);
|
||||
|
||||
@Autowired
|
||||
private UserRepository repository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SampleApplication.class, args);
|
||||
}
|
||||
|
||||
public void run(String... var1) {
|
||||
|
||||
final User testUser = new User("test@test.com", "testFirstName", "testLastName");
|
||||
|
||||
repository.deleteAll();
|
||||
repository.save(testUser);
|
||||
|
||||
final List<User> results = repository.findByEmailAddressAndLastName(testUser.getEmailAddress(),
|
||||
testUser.getLastName());
|
||||
Assert.isTrue(results.size() == 1, "Result size should be 1");
|
||||
|
||||
final User result1 = results.get(0);
|
||||
Assert.state(result1.getFirstName().equals(testUser.getFirstName()), "query result firstName doesn't match!");
|
||||
Assert.state(result1.getLastName().equals(testUser.getLastName()), "query result lastName doesn't match!");
|
||||
|
||||
LOGGER.info("findByEmailAddressAndLastName in User collection get result: {}", result1.toString());
|
||||
System.out.println("findByEmailAddressAndLastName in User collection get result:" + result1.toString());
|
||||
|
||||
final List<User> result2 = repository.findByFirstName(testUser.getFirstName());
|
||||
|
||||
Assert.state(result2.get(0).getFirstName().equals(testUser.getFirstName()),
|
||||
"query result firstName doesn't match!");
|
||||
Assert.state(result2.get(0).getLastName().equals(testUser.getLastName()),
|
||||
"query result lastName doesn't match!");
|
||||
|
||||
LOGGER.info("findByFirstName in User collection get result: {}", result2.get(0).toString());
|
||||
System.out.println("findByFirstName in User collection get result:" + result2.get(0).toString());
|
||||
|
||||
final List<User> result3 = repository.findByLastName(testUser.getLastName());
|
||||
|
||||
Assert.state(result3.get(0).getFirstName().equals(testUser.getFirstName()),
|
||||
"query result firstName doesn't match!");
|
||||
Assert.state(result3.get(0).getLastName().equals(testUser.getLastName()),
|
||||
"query result lastName doesn't match!");
|
||||
|
||||
LOGGER.info("findByFirstName in User collection get result: {}", result3.get(0).toString());
|
||||
System.out.println("findByFirstName in User collection get result:" + result3.get(0).toString());
|
||||
}
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.sample;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.Document;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.DocumentIndexingPolicy;
|
||||
import com.microsoft.azure.spring.data.documentdb.core.mapping.PartitionKey;
|
||||
import com.microsoft.azure.documentdb.IndexingMode;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
@Document(collection = "mycollection")
|
||||
@DocumentIndexingPolicy(mode = IndexingMode.Lazy, automatic = false, includePaths = {}, excludePaths = {})
|
||||
public class User {
|
||||
@Id
|
||||
private String emailAddress;
|
||||
private String firstName;
|
||||
@PartitionKey
|
||||
private String lastName;
|
||||
|
||||
@JsonCreator
|
||||
public User(@JsonProperty("emailAddress") String emailAddress, @JsonProperty("firstName") String firstName,
|
||||
@JsonProperty("lastName") String lastName) {
|
||||
this.emailAddress = emailAddress;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmailAddress() {
|
||||
return emailAddress;
|
||||
}
|
||||
|
||||
public void setEmailAddress(String emailAddress) {
|
||||
this.emailAddress = emailAddress;
|
||||
}
|
||||
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s: %s %s", emailAddress, firstName, lastName);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See LICENSE in the project root for
|
||||
* license information.
|
||||
*/
|
||||
|
||||
package com.microsoft.azure.sample;
|
||||
|
||||
import com.microsoft.azure.spring.data.documentdb.repository.DocumentDbRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends DocumentDbRepository<User, String> {
|
||||
List<User> findByFirstName(String firstName);
|
||||
|
||||
List<User> findByLastName(String lastName);
|
||||
|
||||
List<User> findByEmailAddressAndLastName(String emailAddress, String lastName);
|
||||
}
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
azure.documentdb.uri=put-your-documentdb-uri-here
|
||||
azure.documentdb.key=put-your-documentdb-key-here
|
||||
azure.documentdb.database=put-your-documentdb-databasename-here
|
Загрузка…
Ссылка в новой задаче