Last active
December 18, 2015 07:39
-
-
Save daschl/5748526 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MAin Code | |
public static void main(final String[] args) throws Exception { | |
// Load Configuration | |
ApplicationContext context = | |
new AnnotationConfigApplicationContext(ApplicationConfig.class); | |
// Load Repository | |
BlogPostRepository repo = context.getBean("blogPostService", BlogPostService.class).repository; | |
// Store 2 Blog Posts | |
BlogPost post1 = new BlogPost("My Title", "Long Content", "Michael", true); | |
BlogPost post2 = new BlogPost("Another Title", "Small Content", "John", false); | |
repo.save(post1); | |
repo.save(post2); | |
// List all Blog Posts | |
Iterable<BlogPost> allPosts = repo.findAll(); | |
for (BlogPost post : allPosts) { | |
System.out.println(post); | |
} | |
// Shutdown | |
context.getBean("couchbaseClient", CouchbaseClient.class).shutdown(); | |
} | |
// Repository | |
package template; | |
import org.springframework.data.couchbase.repository.CouchbaseRepository; | |
public interface BlogPostRepository extends CouchbaseRepository<BlogPost, String> { | |
} | |
// Service | |
public class BlogPostService { | |
@Autowired | |
public BlogPostRepository repository; | |
} | |
// Config | |
@Configuration | |
@EnableCouchbaseRepositories("template") | |
public class ApplicationConfig extends AbstractCouchbaseConfiguration { | |
@Bean | |
public CouchbaseClient couchbaseClient() throws Exception { | |
return new CouchbaseClient( | |
Arrays.asList(new URI("http://localhost:8091/pools")), | |
"default", | |
"" | |
); | |
} | |
@Bean | |
public BlogPostService blogPostService() { | |
return new BlogPostService(); | |
} | |
} | |
// Entity | |
package template; | |
import org.springframework.data.annotation.Id; | |
import org.springframework.data.couchbase.core.mapping.Field; | |
public class BlogPost { | |
@Id | |
private String id; | |
private String title; | |
private String content; | |
private String author; | |
private String type; | |
@Field("pub") | |
private boolean published; | |
public BlogPost(String title, String content, String author, boolean published) { | |
id = "post:" + title.toLowerCase().replace(" ", "-"); | |
this.title = title; | |
this.content = content; | |
this.author = author; | |
this.published = published; | |
type = "blogpost"; | |
} | |
public String getId() { | |
return id; | |
} | |
public String getType() { | |
return type; | |
} | |
public String getContent() { | |
return content; | |
} | |
public void setContent(String content) { | |
this.content = content; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getAuthor() { | |
return author; | |
} | |
public void setAuthor(String author) { | |
this.author = author; | |
} | |
public boolean isPublished() { | |
return published; | |
} | |
public void setPublished(boolean published) { | |
this.published = published; | |
} | |
@Override | |
public String toString() { | |
return "BlogPost{" + | |
"id='" + id + '\'' + | |
", title='" + title + '\'' + | |
", content='" + content + '\'' + | |
", author='" + author + '\'' + | |
", published=" + published + | |
", type=" + type + | |
'}'; | |
} | |
} | |
// View for findAll(): | |
function (doc, meta) { | |
if(doc.type == "blogpost") { | |
emit(null, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment