Giriş
Şu satırı dahil ederiz.
- Couchbase için @EnableCouchbaseRepositories kullanılır.
Repository sınıfları klasik anlamda bean olmadıkları iiçin ayrıca taranmaları gerekir. @ComponentScan ile yüklenmezler
Belirtilen paketlerdeki sınıflar için kod üretir ve Spring'e tanıtır. Üretilen kod SimpleJpaRepository sınıfından kalıtır.
Açıklaması şöyle.
Örnek
Şöyle yaparız.
İki tane veri tabanı olsaydı application.properties için şöyle yaparız.
Şöyle yaparız.
Şöyle yaparız
Açıklaması şöyle.
Şu satırı dahil ederiz.
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;- Couchbase için @EnableCouchbaseRepositories kullanılır.
Repository sınıfları klasik anlamda bean olmadıkları iiçin ayrıca taranmaları gerekir. @ComponentScan ile yüklenmezler
Belirtilen paketlerdeki sınıflar için kod üretir ve Spring'e tanıtır. Üretilen kod SimpleJpaRepository sınıfından kalıtır.
Açıklaması şöyle.
In SpringData you have @EnableJpaRepositories + entire underlying scanning and repository bean generation mechanism and there is no need to mark your repository interfaces (or custom classes) with @Repository.Açıklaması şöyle.
In order to start leveraging the Spring Data programming model with JPA, a DAO interface needs to extend the JPA specific Repository interface – JpaRepository. This will enable Spring Data to find this interface and automatically create an implementation for it.Kodun şöyle olduğunu varsayalım. Bu durumda com.myproject.dao.jpa paketini belirtmemiz gerekir.
com.myproject
 - Application.java
com.myproject.configuration
 - ServiceConfiguration.java
 - OtherConfiguration.java
com.myproject.dao.jpa
 - .. your repositories..
com.myproject.domain.jpa
 - .. your @Entity classes...
basePackages Alanı
Örnek
Şöyle yaparız.Örnek
@SpringBootApplication(scanBasePackages = {"com.johndoe", "com.johndoe.repos"})
@EnableJpaRepositories("com.johndoe.repos")
public class MyApplication {
  public static void main(String[] args) {        
    SpringApplication.run(MyApplication.class, args);
  }
}Örnek
Şöyle yaparız.
@EnableJpaRepositories(basePackages="com.foo.bar",
 entityManagerFactoryRef ="entityManager", transactionManagerRef = "txManager")İki tane veri tabanı olsaydı application.properties için şöyle yaparız.
# MySQL DB - "foo"
spring.datasource.url=jdbc:mysql://XXX:3306/foo?currentSchema=public
spring.datasource.username=XXX
spring.datasource.password=XXX
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# MySQL DB - "bar"
bar.datasource.url=jdbc:mysql://YYYY:3306/bar?currentSchema=public
bar.datasource.username=YYYY
bar.datasource.password=YYYY
bar.datasource.driver-class-name=com.mysql.jdbc.Driver
# JPA
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager",
        basePackages = {"com.foobar.foo.repo"})
public class FooDbConfig {
  @Primary
  @Bean(name = "dataSource")
  @ConfigurationProperties(prefix = "spring.datasource")
  public DataSource dataSource() {
    return DataSourceBuilder.create().build();
  }
  @Primary
  @Bean(name = "entityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(
    EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
    return builder
      .dataSource(dataSource)
      .packages("com.foobar.foo.domain")
      .persistenceUnit("foo")
      .build();
  }
  @Primary
  @Bean(name = "transactionManager")
  public PlatformTransactionManager transactionManager(
    @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
  }
}@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "barEntityManagerFactory",
  transactionManagerRef = "barTransactionManager", basePackages = {"com.foobar.bar.repo"})
public class BarDbConfig {
  @Bean(name = "barDataSource")
  @ConfigurationProperties(prefix = "bar.datasource")
  public DataSource dataSource() {
    return DataSourceBuilder.create().build();
  }
  @Bean(name = "barEntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
  EntityManagerFactoryBuilder builder, @Qualifier("barDataSource") DataSource dataSource){
    return builder
      .dataSource(dataSource)
      .packages("com.foobar.bar.domain")
      .persistenceUnit("bar")
      .build();
  }
  @Bean(name = "barTransactionManager")
  public PlatformTransactionManager barTransactionManager(
    @Qualifier("barEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
    return new JpaTransactionManager(barEntityManagerFactory);
  }
}Şöyle yaparız.
@Configuration
@EnableTransactionManagement
@ComponentScan("com.myexample.demo")
@PropertySource(value="classpath:application.yml")
@EnableJpaRepositories(
            basePackages = "com.myexample.demo",
            entityManagerFactoryRef = "myEntityManager",
            transactionManagerRef = "myTransactionManager")
public class DatabaseExample {
  @Bean(name = "myEntityManager")
  @Primary
  public LocalContainerEntityManagerFactoryBean athenaEntityManager() {
    LocalContainerEntityManagerFactoryBean em =
      new LocalContainerEntityManagerFactoryBean();
    ...
    return em;
  }
  @Bean(name = "myTransactionManager")
  @Primary
  public PlatformTransactionManager athenaTransactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    ...
    return transactionManager;
  }
  ...
}Şöyle yaparız
@SpringBootApplication
@EnableJpaRepositories(excludeFilters = {
  @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
    DataRepository.class})
})repositoryBaseClass Alanı
Eğer repository sınıflarımı için farklı bir ata sınıf kullanmak istersek bu alanı kullanırız. Şöyle yaparız. Burada ata sınıf olarak "SpringData JPA MongoDB Expressions" kullanılıyor
@Configuration
@EnableJpaRepositories(repositoryBaseClass = ExpressionsRepositoryImpl.class)
class ApplicationConfiguration { … }repositoryFactoryBeanClass Alanı
Spring data uses JpaRepositoryFactory by default to find the right implementation for your repository. You can define a custom factory by the config @EnableJpaRepositories(repositoryFactoryBeanClass = CustomRepositoryFactoryBean.class)
 
Hiç yorum yok:
Yorum Gönder