[Spring Data Repositories] learning notes -- define your own repository and springrepository

Source: Internet
Author: User

[Spring Data Repositories] learning notes -- define your own repository and springrepository

Sometimes, we need to use some of our own query methods, you can follow the steps below.

1. Define an interface containing this method

Interface UserRepositoryCustom {public void someCustomMethod(User user);}

2. Definition implementation

class UserRepositoryImpl implements UserRepositoryCustom {public void someCustomMethod(User user){//Your custom implementation}}

3. Define the repository you want to use

public interface UserRepository extends CrudRepository<User,Long>, UserRepositoryCustom {//Declare query methods here.}

4. There are two ways to configure repository

4.1 use a uniform suffix (the restriction is that your implementation must be configured with annotation and used only for autowired)

<Repositories base-package = "com. acme. repository "// The default suffix is Impl <repositories base-package =" com. acme. repository "repository-impl-postfix =" FooBar "/>

If no suffix is configured, The UserRepositoryImpl class is used by default.

4.2 define your own bean

<repositories base-package="com.acme.repository"/><beans:bean id="userRepositoryImpl" class="..."><!--further configuration--></beans:bean>

 

The above method can only be used for a single repository. If you want all repository to include this method, please refer to the next article.


I am using springmvc + spring data jpa; failed to convert the qgetResultList () TYPE OF THE EntityManager class.

Q. getResultList () is not necessarily a custom entity class. I remember a strong turn ..


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.