JPA全稱(chēng)Java Persistence API.JPA通過(guò)JDK 5.0注解或XML描述對(duì)象-關(guān)系表的映射關(guān)系,并將運(yùn)行期的實(shí)體對(duì)象持久化到數(shù)據(jù)庫(kù)中。
成都創(chuàng)新互聯(lián)公司主營(yíng)依蘭網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開(kāi)發(fā),依蘭h5成都微信小程序搭建,依蘭網(wǎng)站營(yíng)銷(xiāo)推廣歡迎依蘭等地區(qū)企業(yè)咨詢(xún)
JPA 的目標(biāo)之一是制定一個(gè)可以由很多供應(yīng)商實(shí)現(xiàn)的API,并且開(kāi)發(fā)人員可以編碼來(lái)實(shí)現(xiàn)該API,而不是使用私有供應(yīng)商特有的API。
JPA是需要Provider來(lái)實(shí)現(xiàn)其功能的,hibernate就是JPA Provider中很強(qiáng)的一個(gè),應(yīng)該說(shuō)無(wú)人能出其右。從功能上來(lái)說(shuō),JPA就是Hibernate功能的一個(gè)子集。
添加相關(guān)依賴(lài)
添加spring-boot-starter-jdbc依賴(lài):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa </artifactId> </dependency>
添加MySQL連接類(lèi)和連接池類(lèi):
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
配置數(shù)據(jù)源,在application.properties文件配置:
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8 username: root password: 123456 jpa: hibernate: ddl-auto: update # 第一次簡(jiǎn)表create 后面用update show-sql: true
注意,如果通過(guò)jpa在數(shù)據(jù)庫(kù)中建表,將jpa.hibernate,ddl-auto改為create,建完表之后,要改為update,要不然每次重啟工程會(huì)刪除表并新建。
創(chuàng)建實(shí)體類(lèi)
通過(guò)@Entity 表明是一個(gè)映射的實(shí)體類(lèi), @Id表明id, @GeneratedValue 字段自動(dòng)生成
@Entity public class Account { @Id @GeneratedValue private int id ; private String name ; private double money; ... 省略getter setter }
Dao層
數(shù)據(jù)訪問(wèn)層,通過(guò)編寫(xiě)一個(gè)繼承自 JpaRepository 的接口就能完成數(shù)據(jù)訪問(wèn),其中包含了幾本的單表查詢(xún)的方法,非常的方便。值得注意的是,這個(gè)Account 對(duì)象名,而不是具體的表名,另外Interger是主鍵的類(lèi)型,一般為Integer或者Long
public interface AccountDao extends JpaRepository<Account,Integer> { }
Web層
在這個(gè)栗子中我簡(jiǎn)略了service層的書(shū)寫(xiě),在實(shí)際開(kāi)發(fā)中,不可省略。新寫(xiě)一個(gè)controller,寫(xiě)幾個(gè)restful api來(lái)測(cè)試數(shù)據(jù)的訪問(wèn)。
@RestController @RequestMapping("/account") public class AccountController { @Autowired AccountDao accountDao; @RequestMapping(value = "/list", method = RequestMethod.GET) public List<Account> getAccounts() { return accountDao.findAll(); } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Account getAccountById(@PathVariable("id") int id) { return accountDao.findOne(id); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "money", required = true) double money) { Account account = new Account(); account.setMoney(money); account.setName(name); account.setId(id); Account account1 = accountDao.saveAndFlush(account); return account1.toString(); } @RequestMapping(value = "", method = RequestMethod.POST) public String postAccount(@RequestParam(value = "name") String name, @RequestParam(value = "money") double money) { Account account = new Account(); account.setMoney(money); account.setName(name); Account account1 = accountDao.save(account); return account1.toString(); } }
通過(guò)postman請(qǐng)求測(cè)試,代碼已經(jīng)全部通過(guò)測(cè)試。
源碼下載:https://github.com/forezp/SpringBootLearning
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前題目:SpringBoot整合JPA的實(shí)例代碼
標(biāo)題網(wǎng)址:http://jinyejixie.com/article48/jjeehp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、App開(kāi)發(fā)、網(wǎng)站排名、微信小程序、搜索引擎優(yōu)化、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
全網(wǎng)營(yíng)銷(xiāo)推廣知識(shí)