JPA=Java Persistence API,Java持久層API。JDK5引入JPA ORM目的:簡化Java EE開發(fā),整合ORM技術(shù)。JPA定義了JPQL(Java Persistence Query Language)。
成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括洛寧網(wǎng)站建設(shè)、洛寧網(wǎng)站制作、洛寧網(wǎng)頁制作以及洛寧網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,洛寧網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到洛寧省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
Spring Data JPA默認(rèn)集成的JPA Provider是Hibernate。
a.在pom.xml中增加JPA依賴
????????
????????????
????????????
????????
?
b.然后增加MySQL依賴:
????????
????????????
????????????
????????
c.在Model類前增加標(biāo)注@Entity
@Entity
public?class?user {
d.在ID字段錢增加標(biāo)注@Id和?@GeneratedValue
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private?Integer?id;
e.擴(kuò)展數(shù)據(jù)倉庫接口:
public?interface?UserRepository?extends?CrudRepository
f.在控制類前面增加標(biāo)注
@Controller
@RequestMapping(path="/demo")
public?class?UserController {
g.在控制類倉庫屬性前面增加標(biāo)注
@Autowired
private?UserRepositoryrepository;
h.在控制類操作函數(shù)之中和前面增加標(biāo)注
@GetMapping(path ="/add")
public?@ResponseBody?String addNewUser(@RequestParam?Stringname,@RequestParam?Stringemail) {
useru??=new?user();
u.setName(name);
u.setEmail(email);
repository.save(u);
return?"Saved";
}
?
????@Query("select id,username,sex,address from #{#entityName} u where u.username=:name") ??
????List
?
i.在Application類前面增加標(biāo)注
@SpringBootApplication
public?class?Application {
public?static?void?main(String[]args) {
SpringApplication.run(Application.class,args);
}
}
J.在Application.properties中增加配置項(xiàng)
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
k.注解解說
@Entity說明此類是實(shí)體類,使用默認(rèn)ORM規(guī)則,即class名即數(shù)據(jù)庫表中表名,class字段名即表中的字段名。如果想改變這種默認(rèn)規(guī)則,就要使用@Table來改變class名與數(shù)據(jù)庫中表名的映射規(guī)則,@Column來改變class中字段名與db中表的字段名的映射規(guī)則。
@Query?這是JPA支持重量級(jí)查詢方式,有兩種方式:一種是JPQL的SQL語言方式,一種是原生SQL的語言。常用情景如下:
***like表達(dá)式:
@Query(value = "select id,username,sex,address fromUserModel?b where b.name like %:name%")
List
***原生SQL語言
@Query(value = "select * fromuserb where b.username=?1", nativeQuery = true)
List
***使用@Param注解注入?yún)?shù)
@Query(value = "select id,username,sex,address from UserModel b where b.username = :name AND b.address=:address?AND b.sex=:sex")
List
***SPEL表達(dá)式
@Query(value = "select * from #{#entityName} b where b.name=?1", nativeQuery = true)
List
l.JPA框架圖
m.JPQL語法
基本格式如下:
select 實(shí)體別名.屬性名,?實(shí)體別名.屬性名from實(shí)體名as實(shí)體別名where實(shí)體別名.實(shí)體屬性op比較值
表達(dá)式 | 查詢方法 | hql查詢語句 |
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstname,findByFirstnameIs,findByFirstnameEqual | … 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 ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection age) | … 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) |
?
SpringBoot都支持mybatis和Hibernate,實(shí)際上它們都屬于ORM框架,整體架構(gòu)也差不多,如下:
對(duì)比項(xiàng) | MyBatis | Hibernate |
JDBC | 支持 | 支持 |
JTA事務(wù) | 支持 | 支持 |
SessionFactoryBuilder | 支持 | 支持 |
SessionFactory | 支持 | 支持 |
Session | 支持 | 支持 |
開發(fā)難度 | 框架掌握簡單 | 框架掌握復(fù)雜 |
SQL語句 | 支持原生SQL,便于性能優(yōu)化 | HQL,一般不需要編寫原生SQL語句,性能較低 |
緩存 | 支持二級(jí)緩存,可能存在臟數(shù)據(jù) | 支持二級(jí)緩存 |
可移植性 | 較差 | 較好 |
自動(dòng)化 | 半自動(dòng) | 全自動(dòng) |
安全性 | 較差 | 較好 |
日志 | 較差 | 較好 |