/java/

Spring Data - JPA

2013-02-28 00:27:07

pom.xml


<dependency>
<groupid>org.springframework.data</groupid>
<artifactid>spring-data-jpa</artifactid>
<version>1.3.0.RELEASE</version>
</dependency>

PostRepository.java

public interface PostRepository extends JpaRepository

<post, long="">{

    public List<post> findByStatus(@Param("status") boolean status);
    public List<post> findByType(@Param("type") int type);
    public List<post> findByStatusAndType(@Param("status") boolean status, @Param("type") int type);

    public List<post> nameLike(@Param("name") String name);
    public List<post> descriptionLike(@Param("description") String description);
}

PostRController.java

@Controller
@RequestMapping(value = "/post/r")
public class PostRController {

    @Autowired
    private PostRepository postRepository;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
        postRepository.findByStatusAndType(true,1);
        postRepository.findByType(1);
        postRepository.findByStatus(true);
        postRepository.nameLike("%ipsum%");
        postRepository.descriptionLike("%ipsum%");
        postRepository.findOne(id);
        postRepository.save(post);
        postRepository.delete(id);
    }
}