Last active
August 29, 2015 14:18
-
-
Save DeepSky8/07c230588aca9bdee082 to your computer and use it in GitHub Desktop.
Trying to search a database through Hibernate
This file contains hidden or 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
| package com.kyrlach.issuetracker.issue; | |
| import javax.persistence.Entity; | |
| import javax.persistence.GeneratedValue; | |
| import javax.persistence.GenerationType; | |
| import javax.persistence.Id; | |
| @Entity | |
| public class Issue { | |
| @Id | |
| @GeneratedValue(strategy=GenerationType.AUTO) | |
| private long id; | |
| private String title; | |
| private String description; | |
| private String category; | |
| private Integer difficulty; | |
| public Issue() {} | |
| public Issue(String title, String description, String category, Integer difficulty) { | |
| this.title = title; | |
| this.description = description; | |
| this.category= category; | |
| this.difficulty = difficulty; | |
| } | |
| public long getId() { | |
| return id; | |
| } | |
| public void setId(long id) { | |
| this.id = id; | |
| } | |
| public String getTitle() { | |
| return title; | |
| } | |
| public void setTitle(String title) { | |
| this.title = title; | |
| } | |
| public String getDescription() { | |
| return description; | |
| } | |
| public void setDescription(String description) { | |
| this.description = description; | |
| } | |
| public String getCategory() { | |
| return category; | |
| } | |
| public Number getDifficulty() { | |
| return difficulty; | |
| } | |
| public void setCategory(String category) { | |
| this.category = category; | |
| } | |
| public void setDifficulty(Integer difficulty) { | |
| this.difficulty = difficulty; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Issue("+id+","+title+","+description+","+category+","+difficulty+")"; | |
| } | |
| } |
This file contains hidden or 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
| package com.kyrlach.issuetracker.issue; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.stereotype.Controller; | |
| import org.springframework.ui.Model; | |
| import org.springframework.web.bind.annotation.ModelAttribute; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RequestMethod; | |
| @Controller | |
| @RequestMapping("/issues") | |
| public class IssueController { | |
| @Autowired | |
| private IssueRepository issueRepository; | |
| @RequestMapping(method = RequestMethod.GET) | |
| public String issues(Model model) { | |
| model.addAttribute("issues", issueRepository.findAll()); | |
| return "issueList"; | |
| } | |
| @RequestMapping(value = "/new", method = RequestMethod.GET) | |
| public String newIssue(Model model) { | |
| model.addAttribute("issueForm", new IssueForm()); | |
| return "newIssue"; | |
| } | |
| @RequestMapping(value = "/new", method = RequestMethod.POST) | |
| public String saveIssue(@ModelAttribute IssueForm issueForm, Model model) { | |
| Issue newIssue = new Issue(issueForm.getTitle(), issueForm.getDescription(), issueForm.getCategory(), issueForm.getDifficulty()); | |
| issueRepository.save(newIssue); | |
| return "redirect:/issues"; | |
| } | |
| @RequestMapping(value = "/search", method = RequestMethod.GET) | |
| public String newSearch(Model model) { | |
| return "issueSearch"; | |
| } | |
| @RequestMapping(value = "/search", method = RequestMethod.POST) | |
| public String searchIssues() | |
| { | |
| } | |
| } |
This file contains hidden or 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
| package com.kyrlach.issuetracker.issue; | |
| import java.util.List; | |
| import org.springframework.data.repository.CrudRepository; | |
| public interface IssueRepository extends CrudRepository<Issue, Long> { | |
| List<Issue> findByTitle(String title); | |
| List<Issue> findByDescription(String description); | |
| List<Issue> findByCategory(String category); | |
| List<Issue> findByDifficulty(Integer difficulty); | |
| } |
This file contains hidden or 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
| <!DOCTYPE HTML> | |
| <html xmlns:th="http://www.thymeleaf.org"> | |
| <head> | |
| <title>Issue Tracker -- Search</title> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
| <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css" /> | |
| </head> | |
| <form action="" method="POST"> | |
| <body> | |
| <a href="issues/new">Search Issues</a> | |
| <table class="pure-table pure-table-striped"> | |
| <thead> | |
| <tr> | |
| <th>Title</th> | |
| <th>Name</th> | |
| <th>Category</th> | |
| <th>Difficulty</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <tr> | |
| <label for="findByTitle">Title</label> | |
| <textarea th:field="*{findByTitle}">Search Title</textarea> | |
| <label for="findByDescription">Description</label> | |
| <textarea th:field="*{findByDescription}">Search Description</textarea> | |
| <label for="findByCategory">Category</label> | |
| <textarea th:field="*{findByCategory}">Search Category</textarea> | |
| <label for="findByDifficulty">Difficulty</label> | |
| <textarea th:field="*{findByDifficulty}">Search Difficulty</textarea> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <button type="submit" class="pure-button pure-button-primary">Search</button> | |
| </body> | |
| </form> | |
| </html> |
This file contains hidden or 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
| package com.kyrlach.issuetracker.issue; | |
| public class IssueSearchForm { | |
| private String title; | |
| private String description; | |
| private String category; | |
| private Integer difficulty; | |
| public IssueSearchForm () { | |
| this.title = null; | |
| this.description = null; | |
| this.category = null; | |
| this.difficulty = null; | |
| } | |
| public String getTitle() { | |
| return title; | |
| } | |
| public void setTitle(String title) { | |
| this.title = title; | |
| } | |
| public String getDescription() { | |
| return description; | |
| } | |
| public void setDescription(String description) { | |
| this.description = description; | |
| } | |
| public String getCategory() { | |
| return category; | |
| } | |
| public void setCategory(String category) { | |
| this.category = category; | |
| } | |
| public Integer getDifficulty() { | |
| return difficulty; | |
| } | |
| public void setDifficulty(Integer difficulty) { | |
| this.difficulty = difficulty; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment