SpringBoot整合JPA Spring Data JPA數據持久化

關於JPA和Spring Data JPA詳解:Spring Data JPA數據持久化html

一、引入依賴

  <!-- mysql驅動 -->
  <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
  </dependency>

  <!-- springdata jpa依賴 -->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>

二、配置application.properties

##端口號
server.port=8080


##數據庫配置
##數據庫地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##數據庫用戶名
spring.datasource.username=root
##數據庫密碼
spring.datasource.password=root
##數據庫驅動
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


##validate  加載hibernate時,驗證建立數據庫表結構
##create   每次加載hibernate,從新建立數據庫表結構,這就是致使數據庫表數據丟失的緣由。
##create-drop        加載hibernate時建立,退出是刪除表結構
##update                 加載hibernate自動更新數據庫結構
##validate 啓動時驗證表的結構,不會建立表
##none  啓動時不作任何操做
spring.jpa.hibernate.ddl-auto=create

##控制檯打印sql
spring.jpa.show-sql=true

三、實體類

  其中@Table中的name對應數據庫中表的名稱java

package com.echola.entity;

import javax.persistence.*;

@Entity  //標明這是一個實體類
@Table(name="city") //Table配置的屬性name指出它銀蛇數據庫的表,這樣實體就映射到對應的表上
public class City {

    @Id  //標註那個屬性爲表單的主鍵
    @GeneratedValue(strategy=GenerationType.AUTO) //配置採用何種策略生成主鍵
    private int cityId;
    private String cityName;
    private String cityIntroduce;

    public City(int cityId, String cityName, String cityIntroduce) {
        this.cityId = cityId;
        this.cityName = cityName;
        this.cityIntroduce = cityIntroduce;
    }

    public City(String cityName, String cityIntroduce) {
        this.cityName = cityName;
        this.cityIntroduce = cityIntroduce;
    }

    public City() {
    }

    public int getCityId() {
        return cityId;
    }

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCityIntroduce() {
        return cityIntroduce;
    }

    public void setCityIntroduce(String cityIntroduce) {
        this.cityIntroduce = cityIntroduce;
    }
}

四、持久層

  CityRepository,繼承了JpaRepository,本文只是簡單介紹了jpa的簡單功能,因此JpaRepository中內置的方法已經足夠使用。mysql

package com.echola.repository;

import com.dalaoyang.entity.City;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CityRepository extends JpaRepository<City,Integer> {

}

五、控制層

package com.echola.controller;

import com.dalaoyang.entity.City;
import com.dalaoyang.repository.CityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CityController {


    @Autowired
    private CityRepository cityRepository;


    //http://localhost:8080/saveCity?cityName=北京&cityIntroduce=中國首都
    @GetMapping(value = "saveCity")
    public String saveCity(String cityName,String cityIntroduce){
        City city = new City(cityName,cityIntroduce);
        cityRepository.save(city);
        return "success";
    }

    //http://localhost:8080/deleteCity?cityId=2
    @GetMapping(value = "deleteCity")
    public String deleteCity(int cityId){
        cityRepository.delete(cityId);
        return "success";
    }

    //http://localhost:8080/updateCity?cityId=3&cityName=瀋陽&cityIntroduce=遼寧省省會
    @GetMapping(value = "updateCity")
    public String updateCity(int cityId,String cityName,String cityIntroduce){
        City city = new City(cityId,cityName,cityIntroduce);
        cityRepository.save(city);
        return "success";
    }

    //http://localhost:8080/getCityById?cityId=3
    @GetMapping(value = "getCityById")
    public City getCityById(int cityId){
        City city = cityRepository.findOne(cityId);
        return city;
    }
}
相關文章
相關標籤/搜索