前言
Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化規(guī)范蛛蒙。它為 Java 開(kāi)發(fā)人員提供了一種對(duì)象/關(guān)聯(lián)映射工具來(lái)管理 Java 應(yīng)用中的關(guān)系數(shù)據(jù)箩兽。Spring Boot Jpa 是 Spring 基于 ORM 框架、Jpa 規(guī)范的基礎(chǔ)上封裝的一套 Jpa 應(yīng)用框架缓淹,可使開(kāi)發(fā)者用極簡(jiǎn)的代碼即可實(shí)現(xiàn)對(duì)數(shù)據(jù)的訪問(wèn)和操作每币。它提供了包括增刪改查等在內(nèi)的常用功能力麸,且易于擴(kuò)展拐辽!
1、pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.huzh</groupId>
<artifactId>springboot-jpa</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springboot-jpa</name>
<description>springboot-jpa</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2佛吓、配置jpa和數(shù)據(jù)庫(kù)
使用xxx.yml配置(使用xxx.properties也可以)宵晚。此處配置可能會(huì)導(dǎo)致數(shù)據(jù)庫(kù)事務(wù)無(wú)法使用,如需使用事務(wù)請(qǐng)參考SpringBoot 使用事務(wù)
spring:
#數(shù)據(jù)庫(kù)配置
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
##validate 加載hibernate時(shí)辈毯,驗(yàn)證創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu)
##create 每次加載hibernate坝疼,重新創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu),這就是導(dǎo)致數(shù)據(jù)庫(kù)表數(shù)據(jù)丟失的原因谆沃。
##create-drop 加載hibernate時(shí)創(chuàng)建钝凶,退出是刪除表結(jié)構(gòu)
##update 加載hibernate自動(dòng)更新數(shù)據(jù)庫(kù)結(jié)構(gòu)
##validate 啟動(dòng)時(shí)驗(yàn)證表的結(jié)構(gòu),不會(huì)創(chuàng)建表
##none 啟動(dòng)時(shí)不做任何操作
#jpa配置
jpa:
hibernate:
ddl-auto: update
show-sql: true
#端口號(hào)
server:
port: 8080
3唁影、創(chuàng)建實(shí)體類City
JPA可以自動(dòng)生成表耕陷。
- @Entity注解:
- @Entity必須與@Id注解結(jié)合使用,否則No identifier specified for entity:
- 其name 屬性:
(可選)實(shí)體名稱据沈。 缺省為實(shí)體類的非限定名稱哟沫。
該名稱用于引用查詢中的實(shí)體。
該名稱不能是Java持久性查詢語(yǔ)言中的保留字面值锌介。 - 不與@Table結(jié)合的話表名默認(rèn)為SnakeCaseStrategy(命名策略 )為表名
若使用name屬性且沒(méi)有與@Table結(jié)合嗜诀,則表名為 name值的SnakeCaseStrategy(命名策略 )
- 其中@Table常用的兩個(gè)屬性:
- name 用來(lái)命名 當(dāng)前實(shí)體類 對(duì)應(yīng)的數(shù)據(jù)庫(kù) 表的名字
- uniqueConstraints 用來(lái)批量命名唯一鍵
其作用等同于多個(gè):@Column(unique = true)
- 主鍵采用uuid生成策略。
@Entity
@Table(name = "city")
public class City {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String cityId;
private String cityName;
private String cityIntroduce;
public City(String 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 String getCityId() {
return cityId;
}
public void setCityId(String 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;
}
}
4孔祸、創(chuàng)建DAO
jpa提供基本查詢和復(fù)雜的查詢
基本查詢也分為兩種隆敢,一種是 Spring Data 默認(rèn)已經(jīng)實(shí)現(xiàn),一種是根據(jù)查詢的方法來(lái)自動(dòng)解析成 SQL崔慧。這里用到了自定義的簡(jiǎn)單查詢拂蝎。復(fù)雜查詢有時(shí)間在總結(jié)。
public interface CityRepository extends JpaRepository<City, String> {
void deleteByCityId(String id);
City findByCityId(String id);
}
5惶室、創(chuàng)建controller
- @Autowired自動(dòng)裝配温自,其作用是為了消除代碼Java代碼里面的getter/setter與bean屬性中的property』食可以用@Resource代替悼泌。
- @GetMapping是一個(gè)組合注解,是@RequestMapping(method = RequestMethod.GET)的縮寫(xiě)鹅士。該注解將HTTP Get 映射到 特定的處理方法上券躁。
- 刪除方法需加@Transactional,否則報(bào)錯(cuò)
@RestController
public class CityController {
@Autowired
private CityRepository cityRepository;
//http://localhost:8080/saveCity?cityName=北京&cityIntroduce=中國(guó)首都
@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")
@Transactional
public String deleteCity(String cityId) {
cityRepository.deleteByCityId(cityId);
return "success";
}
//http://localhost:8080/updateCity?cityId=3&cityName=鄭州&cityIntroduce=河南省省會(huì)
@GetMapping(value = "updateCity")
public String updateCity(String 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(String cityId) {
City city = cityRepository.findByCityId(cityId);
return city;
}
}