SpringBoot使用JPA
一.工程目錄如下
二.在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>
<groupId>com.xiao</groupId>
<artifactId>SpringBoot_03</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web元件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JPA元件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Mysql驅動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
三.在application.yml檔案中配置連線資料庫資訊及JPA的基本配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL5Dialect
database: mysql
generate-ddl: true
四.書寫實體類(注意:註解的包不要引錯了)
package com.xiao.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* @Author 笑笑
* @Date 14:34 2018/05/26
*/
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int uid;
private String username;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
五.書寫啟動類
package com.xiao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author 笑笑
* @Date 14:43 2018/05/26
*/
@SpringBootApplication
public class ApplicationEntry {
public static void main(String[] args){
SpringApplication.run(ApplicationEntry.class,args);
}
}
六.啟動測試
出現以上資訊,或者對應的資料庫中建立了實體類所對應的表,表示可以初步使用JPA運算元據庫了!
相關文章
- SpringBoot 中 JPA 的使用Spring Boot
- springboot(五):spring data jpa的使用Spring Boot
- springboot之jpa支援Spring Boot
- Spring Data JPA系列2:SpringBoot整合JPA詳細教程,快速在專案中熟練使用JPASpring Boot
- ElasticSearch與SpringBoot的整合與JPA方法的使用ElasticsearchSpring Boot
- SpringBoot整合系列-整合JPASpring Boot
- Springboot與springdata JPASpring Boot
- SpringBoot中JPA的學習Spring Boot
- SpringBoot整合Spring Data JPASpring Boot
- 如何在SpringBoot中使用Hibernate/JPA的@NaturalId?Spring Boot
- SpringBoot資料訪問(二) SpringBoot整合JPASpring Boot
- 【SpringBoot Demo】MySQL + JPA + Hibernate + Springboot + Maven DemoSpring BootMySqlMaven
- SpringBoot JPA 表關聯查詢Spring Boot
- SpringBoot Jpa多條件查詢Spring Boot
- 使用SpringBoot JPA進行自定義的儲存及批量儲存Spring Boot
- Jpa使用詳解
- springboot(十五):springboot+jpa+thymeleaf增刪改查示例Spring Boot
- 使用SpringBoot-JPA進行自定義的儲存及批量儲存Spring Boot
- SpringBoot使用JPA根據實體類自動生成相應表-mysqlSpring BootMySql
- Kotlin + SpringBoot + JPA 服務端開發KotlinSpring Boot服務端
- JPA EntityManager使用說明
- SpringBoot第九篇:整合Spring Data JPASpring Boot
- Spring Data JPA(二):SpringBoot整合H2Spring Boot
- Spring全家桶系列–SpringBoot之入門JPASpring Boot
- springboot~jpa審計欄位的自動填充Spring Boot
- Spring Data JPA 的使用Spring
- 【極簡版】SpringBoot+SpringData JPA 管理系統Spring Boot
- SpringBoot + JPA的自學之路(三)多表連線查詢Spring Boot
- springboot 專案引入tk或者jpa 訪問報錯Spring Boot
- SpringBoot2.0應用(四):SpringBoot2.0之spring-data-jpaSpring Boot
- Spring Data JPA簡單使用Spring
- 使用JPA連線資料庫資料庫
- SpringBoot JPA查詢對映到自定義實體類Spring Boot
- SpringBoot2.x Data JPA 多資料來源爬坑Spring Boot
- 帶你搭一個SpringBoot+SpringData JPA的環境Spring Boot
- SpringBoot2.0原始碼分析(四):spring-data-jpa分析Spring Boot原始碼
- MySQL-SpringBoot整合JPA實現資料讀寫分離MySqlSpring Boot
- hibernate及SpringBoot整合Jpa實現對資料庫操作Spring Boot資料庫