Spring Framework (24 Part Series)
1 Spring Configuration
2 External Properties in Spring
… 20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types
To use JPA with Spring we need to implement 4 steps:
- Define mapping metadata in entities
- Define an EntityManagerFactory bean
- Define Transaction Manager and DataSource beans
- Define Repository/DAO
With Spring Boot, it automatically creates a DataSource, an EntityManagerFactoryBean, and sets up a JpaTransactionManager. So, to use JPA with Spring Boot we just need to implement 2 steps:
Annotate domain classes
@Entity
@Table(name="T_ACCOUNT")
public class Account {
@Id
@Column(name="ID")
private Long entityId;
@Column(name="CREDIT_CARD")
private String creditCardNumber;
...
}
Enter fullscreen mode Exit fullscreen mode
Define repositories as interfaces
public interface AccountRepository extends Repository<Account,Long> {
public Account findByCreditCardNumber(String creditCardNumber);
}
Enter fullscreen mode Exit fullscreen mode
Spring Data will implement at run-time scans for interfaces extending Repository. Besides, it will implement CRUD methods (extending CrudRepository), paging and sorting (extending PagingAndSortingRepository), among other subinterfaces. See Repository documentation
Using CrudRepository and custom query (JPQL)
public interface AccountRepository extends CrudRepository<Account,Long> {
public Account findByCreditCardNumber(String creditCardNumber);
@Query("SELECT a FROM Account WHERE a.email NOT LIKE '%@%'")
public List<Account> findInvalidEmails();
}
Enter fullscreen mode Exit fullscreen mode
Using the repository
public class AccountServiceImpl implements AccountService {
private AccountRepository accountRepository;
public AccountServiceImpl(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
@Transactional
public Account getAccount(String creditCardNumber) {
return accountRepository.findByCreditCardNumber(creditCardNumber);
}
@Transactional
public void saveAccount(Account account) {
accountRepository.save(account);
}
}
Enter fullscreen mode Exit fullscreen mode
Spring Framework (24 Part Series)
1 Spring Configuration
2 External Properties in Spring
… 20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types
原文链接:JPA with Spring Boot
暂无评论内容