JPA(Java Persistence API)Java持久化API,是 Java 持久化的標準規範,Hibernate是持久化規範的技術實現,而Spring Data JPA是在 Hibernate 基礎上封裝的一款框架。html
<!--more-->java
添加Spring Data JPA 和 MySQL Connector,配置pom.xml文件,代碼以下:mysql
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.12</version> </dependency>
更多JPA版本:http://mvnrepository.com/arti... spring
更多Mysql版本:http://mvnrepository.com/arti...sql
## 數據源配置 spring.datasource.url=jdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql=true
hbm2ddl.auto有四個屬性:數據庫
@Entity public class User implements Serializable { @Id @GeneratedValue private Long id; @Column(name = "name", nullable = false) private String name; @Column(nullable = false) private int age; @Column(nullable = false) private String pwd; public User(){} public User(String name, int age, String pwd) { this.name = name; this.age = age; this.pwd = pwd; } //...忽略set、get方法 }
public interface UserRepository extends JpaRepository<User,Long> { public User findByName(String name); }
繼承JpaRepository以後就繼承了:服務器
這些方法,能夠不寫一行代碼就能夠實現對一個表的操做,固然你也能夠擴展一些本身的方法,只須要在UserRepository裏面添加方法便可。session
@Controller @RequestMapping("/") public class UserController { @Autowired private UserRepository userRepository; @RequestMapping("/") public ModelAndView index() { userRepository.save(new User("老王",18,"123456")); ModelAndView modelAndView = new ModelAndView("/index"); modelAndView.addObject("dataSize", userRepository.findAll().size()); return modelAndView; } }
到如今爲止,集成 Spring Data JPA 已經所有完成了,啓動調試,查看運行效果吧。app
本節高級使用將會涉及的知識點以下:框架
實現事務,只須要兩步便可:
步驟1、在application.properties配置數據庫引擎爲InnoDB:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
步驟2、在方法或類上標識事務@Transactional
示例代碼:
@Transactional public void saveGroup(){ userRepository.save(user); userRepository.save(user2); }
若是出現錯誤,就會進行事務回滾。
在application.properties配置數據庫引擎爲InnoDB:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
經過命令:
show table status from mytestdb;
修改表的引擎:
alter table table_name engine=innodb;
@Transactional註解來自org.springframework.transaction.annotation包,而不是javax.transaction.
JPA支持根據簡單的關鍵字自動生成Sql查詢的方法,好比根據name和age的組合查詢,代碼以下:
public User findByNameAndAge(String name,int age);
使用關鍵字「And」便可,或者查詢時間區間的:
public User findByStartDateBetween(Long startDate);
使用關鍵字「Between」便可。
更多內部支持的關鍵字,以下表:
Keyword | Sample | JPQL snippet |
---|---|---|
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstname,findByFirstnameIs | … where x.firstname = ?1 |
Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
LessThan | findByAgeLessThan | … where x.age < ?1 |
LessThanEqual | findByAgeLessThanEqual | … where x.age <= ?1 |
GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
After | findByStartDateAfter | … where x.startDate > ?1 |
Before | findByStartDateBefore | … where x.startDate < ?1 |
IsNull | findByAgeIsNull | … where x.age is null |
IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1(parameter bound with appended %) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1(parameter bound with prepended %) |
Containing | findByFirstnameContaining | … where x.firstname like ?1(parameter bound wrapped in %) |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection<Age> ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection<Age> ages) | … where x.age not in ?1 |
True | findByActiveTrue() | … where x.active = true |
False | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
官方文檔:https://docs.spring.io/spring...
對於用戶本身編寫sql,Spring Boot JPA也有很好的支持,只須要添加@Query(sql)便可。
示例代碼:
@Transactional @Modifying @Query("update User set name=?1 where id=?2") public int modifyName(String name,Long id);
注意:在執行修改和刪除的時候必須添加@Modifying註解,ORM才知道要執行寫操做,update/delete query 的時候,也必須須要加上@Transactional(事務)才能正常操做。
在 Spring Data JPA 的使用當中,可能會遇到以下的一些錯誤。
實體類Entity沒有空參數的默認構造函數,新增便可解決。
啓動項目報錯,用戶名和密碼配置的key有誤,MySQL8的用戶名和密碼配置和以前的不同,MySQL 8 正確的用戶名密碼配置以下:
spring.datasource.username=root spring.datasource.password=123456 # 如下爲配置老數據庫驅動配置 #spring.datasource.data-username=root #spring.datasource.data-password=123456
MySQL 8 的spring.datasource.driver-class-name配置須要改成「com.mysql.cj.jdbc.Driver」而不是「com.mysql.jdbc.Driver」,正確配置以下:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver