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中使用Hibernate/JPA的@NaturalId?Spring Boot
- springboot之jpa支援Spring Boot
- Spring Data JPA系列2:SpringBoot整合JPA詳細教程,快速在專案中熟練使用JPASpring Boot
- ElasticSearch與SpringBoot的整合與JPA方法的使用ElasticsearchSpring Boot
- SpringBoot整合Spring Data JPASpring Boot
- SpringBoot整合系列-整合JPASpring Boot
- 【SpringBoot Demo】MySQL + JPA + Hibernate + Springboot + Maven DemoSpring BootMySqlMaven
- SpringBoot資料訪問(二) SpringBoot整合JPASpring Boot
- SpringBoot中JPA的學習Spring Boot
- SpringBoot Jpa多條件查詢Spring Boot
- 使用SpringBoot JPA進行自定義的儲存及批量儲存Spring Boot
- Kotlin + SpringBoot + JPA 服務端開發KotlinSpring Boot服務端
- SpringBoot使用JPA根據實體類自動生成相應表-mysqlSpring BootMySql
- 使用SpringBoot-JPA進行自定義的儲存及批量儲存Spring Boot
- springboot使用jpa啟動報錯consider defining a bean of type '*.*.**Repository' in your configuration....Spring BootIDEBean
- SpringBoot第九篇:整合Spring Data JPASpring Boot
- Spring Data JPA(二):SpringBoot整合H2Spring Boot
- Spring全家桶系列–SpringBoot之入門JPASpring Boot
- JPA中@ElementCollection使用
- Jpa使用詳解
- SpringBoot2.0應用(四):SpringBoot2.0之spring-data-jpaSpring Boot
- springboot~jpa審計欄位的自動填充Spring Boot
- 【極簡版】SpringBoot+SpringData JPA 管理系統Spring Boot
- Spring Data JPA 的使用Spring
- JPA EntityManager使用說明
- springboot 專案引入tk或者jpa 訪問報錯Spring Boot
- Spring Data JPA簡單使用Spring
- springboot~jpa優雅的處理isDelete的預設值Spring Bootdelete
- hibernate及SpringBoot整合Jpa實現對資料庫操作Spring Boot資料庫
- SpringBoot JPA查詢對映到自定義實體類Spring Boot
- SpringBoot2.0原始碼分析(四):spring-data-jpa分析Spring Boot原始碼
- SpringBoot + JPA的自學之路(三)多表連線查詢Spring Boot
- 帶你搭一個SpringBoot+SpringData JPA的環境Spring Boot
- MySQL-SpringBoot整合JPA實現資料讀寫分離MySqlSpring Boot
- SpringBoot2.x Data JPA 多資料來源爬坑Spring Boot
- 如何使用JPA的@Formula註解?ORM
- 使用JPA連線資料庫資料庫