Skip to content

Commit d2d15f8

Browse files
author
Eugen
committed
Merge pull request eugenp#386 from KevinGilmore/master
Spring Data Couchbase tutorial
2 parents eba672e + 9ed06aa commit d2d15f8

19 files changed

Lines changed: 674 additions & 0 deletions

File tree

‎spring-data-couchbase-2/.classpath‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/main/resources"/>
4+
<classpathentry kind="src" path="src/main/java"/>
5+
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
6+
<classpathentry kind="src" path="src/test/resources"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
8+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
9+
<classpathentry kind="output" path="target/classes"/>
10+
</classpath>

‎spring-data-couchbase-2/.project‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring-data-couchbase-2</name>
4+
<comment>This project is a simple template for a jar utility using Spring.
5+
</comment>
6+
<projects>
7+
</projects>
8+
<buildSpec>
9+
<buildCommand>
10+
<name>org.eclipse.jdt.core.javabuilder</name>
11+
<arguments>
12+
</arguments>
13+
</buildCommand>
14+
<buildCommand>
15+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
16+
<arguments>
17+
</arguments>
18+
</buildCommand>
19+
<buildCommand>
20+
<name>org.eclipse.m2e.core.maven2Builder</name>
21+
<arguments>
22+
</arguments>
23+
</buildCommand>
24+
</buildSpec>
25+
<natures>
26+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
27+
<nature>org.eclipse.jdt.core.javanature</nature>
28+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
29+
</natures>
30+
</projectDescription>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beansProjectDescription>
3+
<version>1</version>
4+
<pluginVersion><![CDATA[2.2.7.200909012107-CI-R3397-B453]]></pluginVersion>
5+
<configSuffixes>
6+
<configSuffix><![CDATA[xml]]></configSuffix>
7+
</configSuffixes>
8+
<enableImports><![CDATA[true]]></enableImports>
9+
<configs>
10+
</configs>
11+
<configSets>
12+
<configSet>
13+
<name><![CDATA[test]]></name>
14+
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
15+
<incomplete>false</incomplete>
16+
<configs>
17+
</configs>
18+
</configSet>
19+
</configSets>
20+
</beansProjectDescription>

‎spring-data-couchbase-2/pom.xml‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.baeldung</groupId>
4+
<artifactId>spring-data-couchbase-2</artifactId>
5+
<version>0.1-SNAPSHOT</version>
6+
<name>spring-data-couchbase-2</name>
7+
<packaging>jar</packaging>
8+
9+
<dependencies>
10+
11+
<!-- Spring Context and Couchbase Peristence -->
12+
<dependency>
13+
<groupId>org.springframework</groupId>
14+
<artifactId>spring-context</artifactId>
15+
<version>${spring-framework.version}</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.springframework</groupId>
19+
<artifactId>spring-context-support</artifactId>
20+
<version>${spring-framework.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.data</groupId>
24+
<artifactId>spring-data-couchbase</artifactId>
25+
<version>${spring-data-couchbase.version}</version>
26+
</dependency>
27+
28+
<!-- Hibernate JSR-303 Bean Validation -->
29+
<dependency>
30+
<groupId>org.hibernate</groupId>
31+
<artifactId>hibernate-validator</artifactId>
32+
<version>${hibernate-validator.version}</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>joda-time</groupId>
37+
<artifactId>joda-time</artifactId>
38+
<version>${joda-time.version}</version>
39+
</dependency>
40+
41+
<!-- Logging with SLF4J & LogBack -->
42+
<dependency>
43+
<groupId>org.slf4j</groupId>
44+
<artifactId>slf4j-api</artifactId>
45+
<version>${org.slf4j.version}</version>
46+
<scope>compile</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>ch.qos.logback</groupId>
50+
<artifactId>logback-classic</artifactId>
51+
<version>${logback.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.slf4j</groupId>
55+
<artifactId>jcl-over-slf4j</artifactId>
56+
<version>${org.slf4j.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.slf4j</groupId>
60+
<artifactId>log4j-over-slf4j</artifactId>
61+
<version>${org.slf4j.version}</version>
62+
</dependency>
63+
64+
<!-- Test-Scoped Dependencies -->
65+
<dependency>
66+
<groupId>org.springframework</groupId>
67+
<artifactId>spring-test</artifactId>
68+
<version>${spring-framework.version}</version>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>junit</groupId>
73+
<artifactId>junit</artifactId>
74+
<version>${junit.version}</version>
75+
<scope>test</scope>
76+
</dependency>
77+
</dependencies>
78+
79+
<build>
80+
<plugins>
81+
<plugin>
82+
<artifactId>maven-compiler-plugin</artifactId>
83+
<version>2.3.2</version>
84+
<configuration>
85+
<source>1.7</source>
86+
<target>1.7</target>
87+
</configuration>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
92+
<properties>
93+
<java.version>1.7</java.version>
94+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
95+
<spring-framework.version>4.2.4.RELEASE</spring-framework.version>
96+
<spring-data-couchbase.version>2.0.0.RELEASE</spring-data-couchbase.version>
97+
<hibernate-validator.version>5.2.4.Final</hibernate-validator.version>
98+
<joda-time.version>2.9.2</joda-time.version>
99+
<logback.version>1.1.3</logback.version>
100+
<org.slf4j.version>1.7.12</org.slf4j.version>
101+
<junit.version>4.11</junit.version>
102+
103+
</properties>
104+
105+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.spring.data.couchbase;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
8+
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
9+
10+
@Configuration
11+
@EnableCouchbaseRepositories(basePackages={"org.baeldung.spring.data.couchbase"})
12+
public class MyCouchbaseConfig extends AbstractCouchbaseConfiguration {
13+
14+
public static final List<String> NODE_LIST = Arrays.asList("localhost");
15+
public static final String BUCKET_NAME = "baeldung";
16+
public static final String BUCKET_PASSWORD = "";
17+
18+
@Override
19+
protected List<String> getBootstrapHosts() {
20+
return NODE_LIST;
21+
}
22+
23+
@Override
24+
protected String getBucketName() {
25+
return BUCKET_NAME;
26+
}
27+
28+
@Override
29+
protected String getBucketPassword() {
30+
return BUCKET_PASSWORD;
31+
}
32+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.baeldung.spring.data.couchbase.model;
2+
3+
import javax.validation.constraints.NotNull;
4+
5+
import org.joda.time.DateTime;
6+
import org.springframework.data.annotation.Id;
7+
import org.springframework.data.couchbase.core.mapping.Document;
8+
9+
import com.couchbase.client.java.repository.annotation.Field;
10+
11+
@Document
12+
public class Person {
13+
14+
@Id
15+
private String id;
16+
@Field
17+
@NotNull
18+
private String firstName;
19+
@Field
20+
@NotNull
21+
private String lastName;
22+
@Field
23+
@NotNull
24+
private DateTime created;
25+
@Field
26+
private DateTime updated;
27+
28+
public Person(String id, String firstName, String lastName) {
29+
this.id = id;
30+
this.firstName = firstName;
31+
this.lastName = lastName;
32+
}
33+
34+
public String getId() {
35+
return id;
36+
}
37+
public void setId(String id) {
38+
this.id = id;
39+
}
40+
public String getFirstName() {
41+
return firstName;
42+
}
43+
public void setFirstName(String firstName) {
44+
this.firstName = firstName;
45+
}
46+
public String getLastName() {
47+
return lastName;
48+
}
49+
public void setLastName(String lastName) {
50+
this.lastName = lastName;
51+
}
52+
public DateTime getCreated() {
53+
return created;
54+
}
55+
public void setCreated(DateTime created) {
56+
this.created = created;
57+
}
58+
public DateTime getUpdated() {
59+
return updated;
60+
}
61+
public void setUpdated(DateTime updated) {
62+
this.updated = updated;
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
int hash = 1;
68+
if(id != null) {
69+
hash = hash * 31 + id.hashCode();
70+
}
71+
if(firstName != null) {
72+
hash = hash * 31 + firstName.hashCode();
73+
}
74+
if(lastName != null) {
75+
hash = hash * 31 + lastName.hashCode();
76+
}
77+
return hash;
78+
}
79+
80+
@Override
81+
public boolean equals(Object obj) {
82+
if((obj == null) || (obj.getClass() != this.getClass())) return false;
83+
if(obj == this) return true;
84+
Person other = (Person) obj;
85+
return this.hashCode() == other.hashCode();
86+
}
87+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung.spring.data.couchbase.repos;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.spring.data.couchbase.model.Person;
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
public interface PersonRepository extends CrudRepository<Person, String> {
9+
List<Person> findByFirstName(String firstName);
10+
List<Person> findByLastName(String lastName);
11+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.baeldung.spring.data.couchbase.service;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
import org.baeldung.spring.data.couchbase.model.Person;
8+
import org.baeldung.spring.data.couchbase.repos.PersonRepository;
9+
import org.joda.time.DateTime;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.beans.factory.annotation.Qualifier;
12+
import org.springframework.stereotype.Service;
13+
14+
@Service
15+
@Qualifier("PersonRepositoryService")
16+
public class PersonRepositoryService implements PersonService {
17+
18+
private PersonRepository repo;
19+
@Autowired
20+
public void setPersonRepository(PersonRepository repo) {
21+
this.repo = repo;
22+
}
23+
24+
public Person findOne(String id) {
25+
return repo.findOne(id);
26+
}
27+
28+
public List<Person> findAll() {
29+
List<Person> people = new ArrayList<Person>();
30+
Iterator<Person> it = repo.findAll().iterator();
31+
while(it.hasNext()) {
32+
people.add(it.next());
33+
}
34+
return people;
35+
}
36+
37+
public List<Person> findByFirstName(String firstName) {
38+
return repo.findByFirstName(firstName);
39+
}
40+
41+
public List<Person> findByLastName(String lastName) {
42+
return repo.findByLastName(lastName);
43+
}
44+
45+
public void create(Person person) {
46+
person.setCreated(DateTime.now());
47+
repo.save(person);
48+
}
49+
50+
public void update(Person person) {
51+
person.setUpdated(DateTime.now());
52+
repo.save(person);
53+
}
54+
55+
public void delete(Person person) {
56+
repo.delete(person);
57+
}
58+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.spring.data.couchbase.service;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.spring.data.couchbase.model.Person;
6+
7+
public interface PersonService {
8+
9+
Person findOne(String id);
10+
11+
List<Person> findAll();
12+
13+
List<Person> findByFirstName(String firstName);
14+
15+
List<Person> findByLastName(String lastName);
16+
17+
void create(Person person);
18+
19+
void update(Person person);
20+
21+
void delete(Person person);
22+
}

0 commit comments

Comments
 (0)