SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA

轉(zhuǎn)載請標(biāo)明出處:
http://blog.csdn.net/forezp/article/details/70545038
本文出自方志朋的博客

JPA全稱Java Persistence API.JPA通過JDK 5.0注解或XML描述對象-關(guān)系表的映射關(guān)系,并將運(yùn)行期的實(shí)體對象持久化到數(shù)據(jù)庫中。

JPA 的目標(biāo)之一是制定一個可以由很多供應(yīng)商實(shí)現(xiàn)的API,并且開發(fā)人員可以編碼來實(shí)現(xiàn)該API,而不是使用私有供應(yīng)商特有的API。

JPA是需要Provider來實(shí)現(xiàn)其功能的,Hibernate就是JPA Provider中很強(qiáng)的一個,應(yīng)該說無人能出其右。從功能上來說,JPA就是Hibernate功能的一個子集。

添加相關(guān)依賴

添加spring-boot-starter-jdbc依賴:


<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa
            </artifactId>
        </dependency>

添加mysql連接類和連接池類:

    <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  # 第一次簡表create  后面用update
    show-sql: true

注意,如果通過jpa在數(shù)據(jù)庫中建表,將jpa.hibernate,ddl-auto改為create,建完表之后,要改為update,要不然每次重啟工程會刪除表并新建。

創(chuàng)建實(shí)體類

通過@Entity 表明是一個映射的實(shí)體類, @Id表明id, @GeneratedValue 字段自動生成

@Entity
public class Account {
    @Id
    @GeneratedValue
    private int id ;
    private String name ;
    private double money;

...  省略getter setter
}

Dao層

數(shù)據(jù)訪問層,通過編寫一個繼承自 JpaRepository 的接口就能完成數(shù)據(jù)訪問,其中包含了幾本的單表查詢的方法,非常的方便。值得注意的是,這個Account 對象名,而不是具體的表名,另外Interger是主鍵的類型,一般為Integer或者Long

public interface AccountDao  extends JpaRepository<Account,Integer> {
}


Web層

在這個栗子中我簡略了service層的書寫,在實(shí)際開發(fā)中,不可省略。新寫一個controller,寫幾個restful api來測試數(shù)據(jù)的訪問。

@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();

    }


}


通過postman請求測試,代碼已經(jīng)全部通過測試。

源碼下載:https://github.com/forezp/SpringBootLearning

參考資料

accessing-data-jpa

優(yōu)秀文章推薦:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文中我們介紹并比較兩種最流行的開源持久框架:iBATIS和Hibernate,我們還會討論到Java Persi...
    大同若魚閱讀 4,428評論 4 27
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,261評論 6 342
  • 本章是《 Spring Boot 快速入門 》系列教程的第三章,若要查看本系列的全部章節(jié),請點(diǎn)擊 這里 。 目錄...
    terran4j閱讀 5,964評論 4 10
  • 2017年8月21日 我原本只想簡單記錄一下springboot中應(yīng)用Jpa的簡單操作。不想由于hibernate...
    行者N閱讀 6,730評論 0 23
  • 好好活就是做有意義的事,做有意義的是就是好好活. 今天,我們就來說一下iOS多線程的問題,為什么要使用iOS多線程...
    神經(jīng)騷棟閱讀 325評論 0 3

友情鏈接更多精彩內(nèi)容