Skip to content

Commit 87779b7

Browse files
committed
Adding examples realted to Spring Data Repository.
1 parent ed9eceb commit 87779b7

File tree

9 files changed

+64
-19
lines changed

9 files changed

+64
-19
lines changed

src/main/java/com/springdata/example/Main.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.springdata.example;
22

33
import java.util.Date;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
47
import org.springframework.context.ApplicationContext;
58
import org.springframework.context.support.ClassPathXmlApplicationContext;
69
import org.springframework.data.cassandra.core.CassandraOperations;
@@ -9,15 +12,30 @@
912
import com.datastax.driver.core.querybuilder.Insert;
1013
import com.datastax.driver.core.querybuilder.QueryBuilder;
1114
import com.springdata.example.model.Person;
15+
import com.springdata.example.repo.PersonRepository;
1216

1317
public class Main {
1418

1519
public static void main(String[] args) {
1620
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-context.xml");
17-
CassandraOperations cassandraOperations = context.getBean("cassandraTemplate", CassandraOperations.class);
21+
CassandraOperations cassandraOperations = context.getBean("cqlTemplate", CassandraOperations.class);
22+
23+
PersonRepository personRepo = context.getBean(PersonRepository.class);
24+
25+
Iterator<Person> personIterator = personRepo.findAll().iterator();
26+
27+
while(personIterator.hasNext()) {
28+
System.out.println(personIterator.next());
29+
}
30+
31+
List<Person> persons = personRepo.findByLastname("Ware");
32+
33+
for(Person person : persons) {
34+
System.out.println(person);
35+
}
1836
//insertPerson(cassandraOperations);
19-
Person person = selectPerson(cassandraOperations);
20-
insertLoginEventByQueryBuilder(cassandraOperations, person);
37+
//Person person = selectPerson(cassandraOperations);
38+
//insertLoginEventByQueryBuilder(cassandraOperations, person);
2139
}
2240

2341
private static void insertLoginEventByQueryBuilder(
@@ -35,14 +53,14 @@ private static void insertLoginEventByQueryBuilder(
3553
}
3654

3755
private static Person selectPerson(CassandraOperations cassandraOperations) {
38-
String cqlOne = "select * from person where id = '1234567890'";
56+
String cqlOne = "select * from person where id = '123123123'";
3957
Person person = cassandraOperations.selectOne(cqlOne, Person.class);
40-
System.out.println(String.format("Found People with Name [%s] for id [%s]", person.getName(), person.getId()));
58+
System.out.println(String.format("Found People with FirstName [%s] LastName [%s] for id [%s]", person.getFirstname(), person.getLastname(), person.getId()));
4159
return person;
4260
}
4361

4462
private static void insertPerson(CassandraOperations cassandraOperations) {
45-
cassandraOperations.update(new Person("123123123", "Alison", 35));
63+
cassandraOperations.update(new Person("123123123", "Nathan", "Ware", 35, "nathanware@gmail.com"));
4664
}
4765

4866
}

src/main/java/com/springdata/example/model/Person.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,43 @@ public class Person {
88

99
@PrimaryKey
1010
private String id;
11-
12-
private String name;
11+
private String firstname;
12+
private String lastname;
1313
private int age;
14+
private String emailAddress;
15+
1416

15-
public Person(String id, String name, int age) {
17+
public Person(String id, String firstname, String lastname, int age, String emailAddress) {
1618
this.id = id;
17-
this.name = name;
19+
this.firstname = firstname;
20+
this.lastname = lastname;
1821
this.age = age;
22+
this.emailAddress = emailAddress;
1923
}
2024

2125
public String getId() {
2226
return id;
2327
}
28+
29+
public String getFirstname() {
30+
return firstname;
31+
}
2432

25-
public String getName() {
26-
return name;
33+
public String getLastname() {
34+
return lastname;
2735
}
2836

2937
public int getAge() {
3038
return age;
3139
}
3240

41+
public String getEmailAddress() {
42+
return emailAddress;
43+
}
44+
3345
@Override
3446
public String toString() {
35-
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
47+
return "Person [id=" + id + ", name=" + firstname + " " + lastname + ", age=" + age + ", email=" + emailAddress + "]";
3648
}
3749

3850
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.springdata.example.repo;
2+
3+
import java.util.List;
4+
5+
import org.springframework.data.cassandra.repository.Query;
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
import com.springdata.example.model.Person;
9+
10+
public interface PersonRepository extends CrudRepository<Person, String> {
11+
12+
@Query("select * from Person where lastname=?0")
13+
List<Person> findByLastname(String lastname);
14+
15+
}

src/resources/application-context.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
<cassandra:session keyspace-name="${cassandra.keyspace}" />
2121

2222
<!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter -->
23-
<cassandra:mapping />
23+
<cassandra:mapping entity-base-packages="com.springdata.example.model" />
2424

2525
<!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate -->
2626
<cassandra:converter />
2727

2828
<!-- REQUIRED: The Cassandra Template is the building block of all Spring
2929
Data Cassandra -->
30-
<cassandra:template id="cassandraTemplate" />
30+
<cassandra:template id="cqlTemplate" />
3131

3232
<!-- OPTIONAL: If you are using Spring Data Cassandra Repositories, add
3333
your base packages to scan here -->
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Manifest-Version: 1.0
22
Built-By: nitin
3-
Build-Jdk: 1.8.0_05
3+
Build-Jdk: 1.8.0_66
44
Created-By: Maven Integration for Eclipse
55

target/classes/META-INF/maven/com.test.springdata.example/SpringDataCassandra/pom.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Generated by Maven Integration for Eclipse
2-
#Thu Aug 20 21:21:27 EDT 2015
2+
#Sat Jun 04 09:22:37 EDT 2016
33
version=0.0.1-SNAPSHOT
44
groupId=com.test.springdata.example
55
m2e.projectName=SpringDataCassandra

target/classes/application-context.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
<cassandra:session keyspace-name="${cassandra.keyspace}" />
2121

2222
<!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter -->
23-
<cassandra:mapping />
23+
<cassandra:mapping entity-base-packages="com.springdata.example.model" />
2424

2525
<!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate -->
2626
<cassandra:converter />
2727

2828
<!-- REQUIRED: The Cassandra Template is the building block of all Spring
2929
Data Cassandra -->
30-
<cassandra:template id="cassandraTemplate" />
30+
<cassandra:template id="cqlTemplate" />
3131

3232
<!-- OPTIONAL: If you are using Spring Data Cassandra Repositories, add
3333
your base packages to scan here -->
989 Bytes
Binary file not shown.
347 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)