1. 什么是JPA ?
全称Java Persistence API ,通俗来讲就是一套API框架,通过操作相应API,完成实体对象持久化存储到数据库的操作。
2 JPA 能做什么?
ORM映射元数据:通过注解或xml 配置方式,建立对象和表关系。(JPA支持XML和注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中)
JPA 的API:一套执行CRUD 的操作API。封装了不同的方法进行操作。(用来操作实体对象,执行CRUD操作,框架在后台替我们完成所有的事情,开发者从繁琐的JDBC和SQL代码中解脱出来。)
查询语言:查询sql 时,通过对象进行查询,而非原生SQL.(通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。)
JPA 本身是一套规范,其他厂商基于JPA 规范做了相应实现,比如Hibernate, Spring Data JPA 等。类似于Log4J 规范,向Logback,Slf4j 等具体实现了日志操作。
3. 接口约定关键字
关键字 |
描述 |
方法命名 |
样例 |
And |
sql and 条件 |
findByUsernameAndPwd |
where username = ? and pwd = ? |
Or |
sql or 条件 |
findByUserNameOrEmail |
where username =? or email=? |
Is,Equals |
sql = 条件 |
findById,findByIdEquals |
where id = ? |
Between |
sql between 条件 |
findByIdBetween |
where id between ? and ? |
LessThan |
sql < 条件 |
findByIdLessThan |
where id < ? |
LessThanEquals |
sql <= 条件 |
findByIdLessThanEquals |
where id<=? |
GreaterThan |
sql > 条件 |
findByIdGreaterThan |
where id > ? |
GreaterThanEquals |
sql >= 条件 |
findByIdGreaterThanEquals |
where id >=? |
After |
sql > 条件 |
findByIdAfter |
where id >? |
Before |
sql < 条件 |
findByIdBefore |
where id < ? |
IsNull |
sql is null |
findByIdIsNull |
where id is null |
NotNull |
sql not null |
findByIdNotNull |
where id is not null |
Like |
sql like |
findByNameLike |
where name l |