#19 - Add example for Spring Data Cassandra.

Shows java-config as well as Repository configuration.
This commit is contained in:
Thomas Darimont
2014-10-07 20:28:41 +02:00
parent eeb0485cf9
commit 4841bbdc11
9 changed files with 289 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2014 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.cassandra.simple;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cassandra.config.java.AbstractCqlTemplateConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import com.datastax.driver.core.Session;
/**
* @author Oliver Gierke
* @author Thomas Darimont
*/
@Configuration
@EnableAutoConfiguration
class SimpleConfiguration {
@Configuration
@EnableCassandraRepositories
static class CassandraConfig extends AbstractCqlTemplateConfiguration {
@Override
public String getKeyspaceName() {
return "example";
}
@Bean
public CassandraTemplate cassandraTemplate(Session session) {
return new CassandraTemplate(session);
}
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2013-2014 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.cassandra.simple;
import org.springframework.data.repository.CrudRepository;
/**
* Simple repository interface for {@link User} instances. The interface is used to declare so called query methods,
* methods to retrieve single entities or collections of them.
*
* @author Thomas Darimont
*/
public interface SimpleUserRepository extends CrudRepository<User, Long> {}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2013-2014 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.cassandra.simple;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;
/**
* Sample user class.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@Data
@NoArgsConstructor
@Table(value = "users")
public class User {
@PrimaryKey("user_id") private Long id;
@Column("uname") private String username;
@Column("fname") private String firstname;
@Column("lname") private String lastname;
public User(Long id) {
this.setId(id);
}
}

View File

@@ -0,0 +1,5 @@
/**
* Package showing a simple repository interface to use basic query method execution functionality.
*/
package example.springdata.cassandra.simple;

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2013-2014 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.cassandra.simple;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
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;
/**
* Integration test showing the basic usage of {@link SimpleUserRepository}.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SimpleConfiguration.class)
public class SimpleUserRepositoryTests {
@Autowired SimpleUserRepository repository;
User user;
@Before
public void setUp() {
user = new User();
user.setId(42L);
user.setUsername("foobar");
user.setFirstname("firstname");
user.setLastname("lastname");
}
@Test
public void findSavedUserById() {
user = repository.save(user);
assertThat(repository.findOne(user.getId()), is(user));
}
}