Define Repository
Jimmer integrates spring data and provides a base repository interface for Java and Kotlin users respectively.
Java | org.babyfish.jimmer.spring.repository.JRepository<E, ID> |
Kotlin | org.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
- Java
- Kotlin
BookStoreRepository.javapackage com.example.repository;
import com.example.model.BookStore;
import org.babyfish.jimmer.spring.repository.JRepository;
public interface BookRepository extends JRepository<BookStore, Long> {}BookStoreRepository.ktpackage com.example.repository
import com.example.model.BookStore
import org.babyfish.jimmer.spring.repository.KRepository
interface BookRepository : KRepository<BookStore, Long> -
BookRepository
- Java
- Kotlin
BookRepository.javapackage com.example.repository;
import com.example.model.Book;
import org.babyfish.jimmer.spring.repository.JRepository;
public interface BookRepository extends JRepository<Book, Long> {}BookRepository.ktpackage com.example.repository
import com.example.model.Book
import org.babyfish.jimmer.spring.repository.KRepository
interface BookRepository : KRepository<Book, Long> -
AuthorRepository
- Java
- Kotlin
AuthorRepository.javapackage com.example.repository;
import com.example.model.Author;
import org.babyfish.jimmer.spring.repository.JRepository;
public interface AuthorRepository extends JRepository<Author, Long> {}AuthorRepository.ktpackage com.example.repository
import com.example.model.Author
import org.babyfish.jimmer.spring.repository.KRepository
interface AuthorRepository : KRepository<Author, Long> -
TreeNodeRepository
- Java
- Kotlin
TreeNodeRepository.javapackage com.example.repository;
import com.example.model.TreeNode;
import org.babyfish.jimmer.spring.repository.JRepository;
public interface TreeNodeRepository extends JRepository<TreeNode, Long> {}TreeNodeRepository.ktpackage com.example.repository
import com.example.model.TreeNode
import org.babyfish.jimmer.spring.repository.KRepository
interface TreeNodeRepository : KRepository<TreeNode, Long>
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.