#149 - Polishing.

Renamed repository folder to repositories. Tweaked package name to reflect module name. Polished formatting in readme. Switched to Spring Boot version property for 1.7 RC1 upgrade.

Original pull request: #162.
This commit is contained in:
Oliver Gierke
2016-03-18 15:42:03 +01:00
parent 5a25e80bbf
commit 786ce2d39a
11 changed files with 73 additions and 104 deletions

View File

@@ -20,7 +20,7 @@
<module>cluster-sentinel</module> <module>cluster-sentinel</module>
<module>example</module> <module>example</module>
<module>cluster</module> <module>cluster</module>
<module>repository</module> <module>repositories</module>
</modules> </modules>
<dependencies> <dependencies>

View File

@@ -10,15 +10,15 @@ Redis Repository support allows to convert, store, retrieve and index entities w
@RedisHash("persons") @RedisHash("persons")
class Person { class Person {
@Id String id; @Id String id;
@Indexed String firstname;
@Indexed String lastname;
Gender gender; @Indexed String firstname;
Address address; @Indexed String lastname;
@Reference List<Person> children; Gender gender;
Address address;
@Reference List<Person> children;
} }
``` ```
@@ -81,36 +81,36 @@ The below configuration uses [Jedis](https://github.com/xetorthio/jedis) to conn
@EnableRedisRepositories @EnableRedisRepositories
class AppConfig { class AppConfig {
@Bean @Bean
RedisConnectionFactory connectionFactory() { RedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory(); return new JedisConnectionFactory();
} }
@Bean @Bean
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>(); RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
template.setConnectionFactory(connectionFactory); template.setConnectionFactory(connectionFactory);
return template; return template;
} }
} }
``` ```
Having the infrastructure in place you can go on declaring and using the `Repository` interface. Having the infrastructure in place you can go on declaring and using the `Repository` interface.
```java ```java
interface PersonRepository extends CrudRepository<Person, String> { interface PersonRepository extends CrudRepository<Person, String> {
List<Person> findByLastname(String lastname); List<Person> findByLastname(String lastname);
Page<Person> findByLastname(String lastname, Pageable page); Page<Person> findByLastname(String lastname, Pageable page);
List<Person> findByFirstnameAndLastname(String firstname, String lastname); List<Person> findByFirstnameAndLastname(String firstname, String lastname);
List<Person> findByFirstnameOrLastname(String firstname, String lastname); List<Person> findByFirstnameOrLastname(String firstname, String lastname);
List<Person> findByAddress_City(String city); List<Person> findByAddress_City(String city);
} }
``` ```

View File

@@ -2,7 +2,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 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> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-redis-repository-example</artifactId> <artifactId>spring-data-redis-repositories-example</artifactId>
<name>Spring Data Redis - Repository Support Example</name> <name>Spring Data Redis - Repository Support Example</name>
<parent> <parent>
@@ -12,39 +12,31 @@
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<properties>
<spring-data-releasetrain.version>Hopper-RC1</spring-data-releasetrain.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>spring-data-redis-example-utils</artifactId> <artifactId>spring-data-redis-example-utils</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.kstyrc</groupId> <groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId> <artifactId>embedded-redis</artifactId>
<version>0.6</version> <version>0.6</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-keyvalue</artifactId>
<version>1.1.0.RC1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.12.0.RC1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package example.springdata.redis.domain; package example.springdata.redis.repositories;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package example.springdata.redis; package example.springdata.redis.repositories;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -27,22 +27,13 @@ import org.springframework.data.redis.repository.configuration.EnableRedisReposi
*/ */
@Configuration @Configuration
@EnableRedisRepositories @EnableRedisRepositories
public class AppConfig { public class ApplicationConfiguration {
/**
* {@link RedisConnectionFactory} to be used when connecting to redis.
*
* @return
*/
@Bean @Bean
RedisConnectionFactory connectionFactory() { RedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory(); return new JedisConnectionFactory();
} }
/**
* @param connectionFactory
* @return
*/
@Bean @Bean
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
@@ -51,5 +42,4 @@ public class AppConfig {
return template; return template;
} }
} }

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package example.springdata.redis.domain; package example.springdata.redis.repositories;
/** /**
* @author Christoph Strobl * @author Christoph Strobl

View File

@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package example.springdata.redis.domain; package example.springdata.redis.repositories;
import java.lang.reflect.Field;
import java.util.List;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.lang.reflect.Field;
import java.util.List;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Reference; import org.springframework.data.annotation.Reference;
@@ -54,10 +55,12 @@ import org.springframework.data.redis.core.index.Indexed;
@Data @Data
@EqualsAndHashCode(exclude = { "children" }) @EqualsAndHashCode(exclude = { "children" })
@RedisHash("persons") @RedisHash("persons")
@NoArgsConstructor
class Person { class Person {
/** /**
* The {@literal id} and {@link RedisHash#toString()} build up the {@literal key} for the Redis {@literal HASH}. <br /> * The {@literal id} and {@link RedisHash#toString()} build up the {@literal key} for the Redis {@literal HASH}.
* <br />
* *
* <pre> * <pre>
* <code> * <code>
@@ -106,12 +109,10 @@ class Person {
*/ */
private @Reference List<Person> children; private @Reference List<Person> children;
public Person() {}
public Person(String firstname, String lastname, Gender gender) { public Person(String firstname, String lastname, Gender gender) {
this.firstname = firstname; this.firstname = firstname;
this.lastname = lastname; this.lastname = lastname;
this.gender = gender; this.gender = gender;
} }
} }

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package example.springdata.redis.domain; package example.springdata.redis.repositories;
import java.util.List; import java.util.List;
@@ -35,5 +35,4 @@ interface PersonRepository extends CrudRepository<Person, String> {
List<Person> findByFirstnameOrLastname(String firstname, String lastname); List<Person> findByFirstnameOrLastname(String firstname, String lastname);
List<Person> findByAddress_City(String city); List<Person> findByAddress_City(String city);
} }

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package example.springdata.redis.domain; package example.springdata.redis.repositories;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.*; import static org.hamcrest.collection.IsIterableContainingInAnyOrder.*;
import static org.hamcrest.core.Is.*; import static org.hamcrest.core.Is.*;
@@ -21,6 +21,9 @@ import static org.hamcrest.core.IsCollectionContaining.*;
import static org.hamcrest.core.IsNot.*; import static org.hamcrest.core.IsNot.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import example.springdata.redis.test.util.EmbeddedRedisServer;
import example.springdata.redis.test.util.RequiresRedisServer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@@ -41,15 +44,12 @@ import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.index.Indexed; import org.springframework.data.redis.core.index.Indexed;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import example.springdata.redis.AppConfig;
import example.springdata.redis.test.util.EmbeddedRedisServer;
import example.springdata.redis.test.util.RequiresRedisServer;
/** /**
* @author Christoph Strobl * @author Christoph Strobl
* @author Oliver Gierke
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AppConfig.class) @SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
public class PersonRepositoryTests<K, V> { public class PersonRepositoryTests<K, V> {
/** /**
@@ -57,14 +57,14 @@ public class PersonRepositoryTests<K, V> {
* 1) Start/Stop an embedded instance or reuse an already running local installation <br /> * 1) Start/Stop an embedded instance or reuse an already running local installation <br />
* 2) Ignore tests if startup failed and no server running locally. * 2) Ignore tests if startup failed and no server running locally.
*/ */
public static @ClassRule RuleChain rules = RuleChain.outerRule( public static @ClassRule RuleChain rules = RuleChain
EmbeddedRedisServer.runningAt(6379).suppressExceptions()).around(RequiresRedisServer.onLocalhost()); .outerRule(EmbeddedRedisServer.runningAt(6379).suppressExceptions()).around(RequiresRedisServer.onLocalhost());
/** {@link Charset} for String conversion **/ /** {@link Charset} for String conversion **/
private static final Charset CHARSET = Charset.forName("UTF-8"); private static final Charset CHARSET = Charset.forName("UTF-8");
@Autowired RedisOperations<K, V> ops; @Autowired RedisOperations<K, V> operations;
@Autowired PersonRepository repo; @Autowired PersonRepository repository;
/* /*
* Set of test users * Set of test users
@@ -81,7 +81,7 @@ public class PersonRepositoryTests<K, V> {
@After @After
public void setUp() { public void setUp() {
ops.execute((RedisConnection connection) -> { operations.execute((RedisConnection connection) -> {
connection.flushDb(); connection.flushDb();
return "OK"; return "OK";
}); });
@@ -94,10 +94,11 @@ public class PersonRepositoryTests<K, V> {
@Test @Test
public void saveSingleEntity() { public void saveSingleEntity() {
repo.save(eddard); repository.save(eddard);
assertThat(ops.execute((RedisConnection connection) -> connection.exists(new String("persons:" + eddard.getId()) assertThat(operations.execute(
.getBytes(CHARSET))), is(true)); (RedisConnection connection) -> connection.exists(new String("persons:" + eddard.getId()).getBytes(CHARSET))),
is(true));
} }
/** /**
@@ -108,7 +109,7 @@ public class PersonRepositoryTests<K, V> {
flushTestUsers(); flushTestUsers();
List<Person> starks = repo.findByLastname(eddard.getLastname()); List<Person> starks = repository.findByLastname(eddard.getLastname());
assertThat(starks, containsInAnyOrder(eddard, robb, sansa, arya, bran, rickon)); assertThat(starks, containsInAnyOrder(eddard, robb, sansa, arya, bran, rickon));
assertThat(starks, not(hasItem(jon))); assertThat(starks, not(hasItem(jon)));
@@ -122,7 +123,7 @@ public class PersonRepositoryTests<K, V> {
flushTestUsers(); flushTestUsers();
List<Person> aryaStark = repo.findByFirstnameAndLastname(arya.getFirstname(), arya.getLastname()); List<Person> aryaStark = repository.findByFirstnameAndLastname(arya.getFirstname(), arya.getLastname());
assertThat(aryaStark, hasItem(arya)); assertThat(aryaStark, hasItem(arya));
assertThat(aryaStark, not(hasItems(eddard, robb, sansa, bran, rickon, jon))); assertThat(aryaStark, not(hasItems(eddard, robb, sansa, bran, rickon, jon)));
@@ -136,7 +137,7 @@ public class PersonRepositoryTests<K, V> {
flushTestUsers(); flushTestUsers();
List<Person> aryaAndJon = repo.findByFirstnameOrLastname(arya.getFirstname(), jon.getLastname()); List<Person> aryaAndJon = repository.findByFirstnameOrLastname(arya.getFirstname(), jon.getLastname());
assertThat(aryaAndJon, containsInAnyOrder(arya, jon)); assertThat(aryaAndJon, containsInAnyOrder(arya, jon));
assertThat(aryaAndJon, not(hasItems(eddard, robb, sansa, bran, rickon))); assertThat(aryaAndJon, not(hasItems(eddard, robb, sansa, bran, rickon)));
@@ -150,12 +151,12 @@ public class PersonRepositoryTests<K, V> {
flushTestUsers(); flushTestUsers();
Page<Person> page1 = repo.findPersonByLastname(eddard.getLastname(), new PageRequest(0, 5)); Page<Person> page1 = repository.findPersonByLastname(eddard.getLastname(), new PageRequest(0, 5));
assertThat(page1.getNumberOfElements(), is(5)); assertThat(page1.getNumberOfElements(), is(5));
assertThat(page1.getTotalElements(), is(6L)); assertThat(page1.getTotalElements(), is(6L));
Page<Person> page2 = repo.findPersonByLastname(eddard.getLastname(), new PageRequest(1, 5)); Page<Person> page2 = repository.findPersonByLastname(eddard.getLastname(), new PageRequest(1, 5));
assertThat(page2.getNumberOfElements(), is(1)); assertThat(page2.getNumberOfElements(), is(1));
assertThat(page2.getTotalElements(), is(6L)); assertThat(page2.getTotalElements(), is(6L));
@@ -175,7 +176,7 @@ public class PersonRepositoryTests<K, V> {
flushTestUsers(); flushTestUsers();
List<Person> eddardStark = repo.findByAddress_City(winterfell.getCity()); List<Person> eddardStark = repository.findByAddress_City(winterfell.getCity());
assertThat(eddardStark, hasItem(eddard)); assertThat(eddardStark, hasItem(eddard));
assertThat(eddardStark, not(hasItems(robb, sansa, arya, bran, rickon, jon))); assertThat(eddardStark, not(hasItems(robb, sansa, arya, bran, rickon, jon)));
@@ -192,9 +193,9 @@ public class PersonRepositoryTests<K, V> {
eddard.setChildren(Arrays.asList(jon, robb, sansa, arya, bran, rickon)); eddard.setChildren(Arrays.asList(jon, robb, sansa, arya, bran, rickon));
repo.save(eddard); repository.save(eddard);
Person laoded = repo.findOne(eddard.getId()); Person laoded = repository.findOne(eddard.getId());
assertThat(laoded.getChildren(), hasItems(jon, robb, sansa, arya, bran, rickon)); assertThat(laoded.getChildren(), hasItems(jon, robb, sansa, arya, bran, rickon));
/* /*
@@ -203,14 +204,14 @@ public class PersonRepositoryTests<K, V> {
* - Robb was killed by Roose Bolton during the Red Wedding. * - Robb was killed by Roose Bolton during the Red Wedding.
* - Jon was stabbed by brothers or the Night's Watch. * - Jon was stabbed by brothers or the Night's Watch.
*/ */
repo.delete(Arrays.asList(robb, jon)); repository.delete(Arrays.asList(robb, jon));
laoded = repo.findOne(eddard.getId()); laoded = repository.findOne(eddard.getId());
assertThat(laoded.getChildren(), hasItems(sansa, arya, bran, rickon)); assertThat(laoded.getChildren(), hasItems(sansa, arya, bran, rickon));
assertThat(laoded.getChildren(), not(hasItems(robb, jon))); assertThat(laoded.getChildren(), not(hasItems(robb, jon)));
} }
private void flushTestUsers() { private void flushTestUsers() {
repo.save(Arrays.asList(eddard, robb, sansa, arya, bran, rickon, jon)); repository.save(Arrays.asList(eddard, robb, sansa, arya, bran, rickon, jon));
} }
} }

View File

@@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>
<root level="warn">
<appender-ref ref="console" />
</root>
</configuration>

View File

@@ -10,7 +10,7 @@
</parent> </parent>
<artifactId>spring-data-redis-example-utils</artifactId> <artifactId>spring-data-redis-example-utils</artifactId>
<name>Spring Data Redis - Example utilities</name> <name>Spring Data Redis - Example Utilities</name>
<dependencies> <dependencies>
<dependency> <dependency>