Spring Boot,记录一下库JPA,一上手的时候,看着spring的配置真也是头疼,各种数据库….之前我只了解Mysql或Oracle,结果你来这么多,不知道想干什么!
JPA是一个ORM的映射框架,目的就是为了提高开发效率和提升性能体验
添加JPA库包
1 | <dependency> |
在application.yml
中可以添加执行sql语句显示
1 | spring: |
添加JpaRepository
关联数据库应用
添加数据库对象
1 |
|
关联Reposity
1 |
|
映射api接口
1 |
|
添加完成后,就可以一键使用Spring Boot JPA
套餐了,套餐丰富到你已经不用再写一条SQL语句了,如果实在要写,可以参考我上面写的findTestList
示例(爬坑前方法!)
套餐示例,可以直接看JpaRepository
,PagingAndSortingRepository
和CrudRepository
。
JpaRepository
extends PagingAndSortingRepository
which in turn extends CrudRepository
.
Their main functions are:
CrudRepository
mainly provides CRUD functions.PagingAndSortingRepository
provides methods to do pagination and sorting records.JpaRepository
provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.
Because of the inheritance mentioned above, JpaRepository
will have all the functions of CrudRepository
and PagingAndSortingRepository
. So if you don’t need the repository to have the functions provided by JpaRepository
and PagingAndSortingRepository
, use CrudRepository
.
总结就是JpaRepository
功能强大,你只需要它,你还可以试试RxJava2CrudRepository
等异步Repository
支持关键字模糊匹配
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,findByFirstnameEquals | … 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, Null | findByAge(Is)Null | … 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 |
… where x.age in ?1 |
NotIn | findByAgeNotIn(Collection |
… 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) |
例如:
1 |
|
其中user
就是UserModel
中的成员变量
错误org.springframework.data.mapping.PropertyReferenceException: No property id found for type [entity]
这里因为基类Model中没有id
成员,不只是标记@Id
,而是必须为id
,如
1 |
|
Navicat修改表结构
- 保存当前SQL数据:转存SQL文件->选择“结构和数据”
- 修改表结构:右键->设计表,再修改所需要的表
- 删除数据库,再重新导航SQL运行文件
参考
https://stackoverflow.com/questions/14014086/what-is-difference-between-crudrepository-and-jparepository-interfaces-in-spring
https://docs.spring.io/spring-data/jpa/docs/2.2.1.RELEASE/reference/html/#reference
https://spring.io/projects/spring-data-jpa#overview