Skip to main content

Define Repository

Jimmer integrates spring data and provides a base repository interface for Java and Kotlin users respectively.

Javaorg.babyfish.jimmer.spring.repository.JRepository<E, ID>
Kotlinorg.babyfish.jimmer.spring.repository.KRepository<E, ID>

This interface has two generic parameters

  • E: Entity type
  • ID: Entity ID type

By inheriting this interface, developers can quickly implement various Repository

  • BookStoreRepository

    BookStoreRepository.java
    package com.example.repository;

    import com.example.model.BookStore;

    import org.babyfish.jimmer.spring.repository.JRepository;

    public interface BookRepository extends JRepository<BookStore, Long> {}
  • BookRepository

    BookRepository.java
    package com.example.repository;

    import com.example.model.Book;

    import org.babyfish.jimmer.spring.repository.JRepository;

    public interface BookRepository extends JRepository<Book, Long> {}
  • AuthorRepository

    AuthorRepository.java
    package com.example.repository;

    import com.example.model.Author;

    import org.babyfish.jimmer.spring.repository.JRepository;

    public interface AuthorRepository extends JRepository<Author, Long> {}
  • TreeNodeRepository

    TreeNodeRepository.java
    package com.example.repository;

    import com.example.model.TreeNode;

    import org.babyfish.jimmer.spring.repository.JRepository;

    public interface TreeNodeRepository extends JRepository<TreeNode, Long> {}
note

The rest is the same as spring-data. Just define the interface and Jimmer will automatically implement these interfaces and register them in Spring.

However, there is one caveat:

  • By default, the package of the custom Repository must be the same as the package of the SpringBoot main class or its subpackage.

  • Otherwise, you need to use @org.babyfish.jimmer.spring.repository.EnableJimmerRepositories to annotate the SpringBoot main class or other Spring configuration class to explicitly specify the package where the custom Repository interface is located.