spring boot ----> jpa鏈接和操做mysql數據庫

環境:java

centos6.8,jdk1.8.0_172,maven3.5.4,vim,spring boot 1.5.13,mysql-5.7.23mysql

一、引入jpa起步依賴和mysql驅動jar包spring

 1 <dependency>
 2       <groupId>org.springframework.boot</groupId>
 3       <artifactId>spring-boot-starter-data-jpa</artifactId>
 4 </dependency>
 5 
 6 <dependency>
 7         <groupId>mysql</groupId>
 8         <artifactId>mysql-connector-java</artifactId>
 9         <scope>runtime</scope>
10 </dependency>

Note:sql

引入spring-boot-starter-data-jpa,實際上該jar包的內容是這樣的,包含4個文件:數據庫

關注一下pom.xml文件,內容是這樣的:集合了各類依賴的jar包的gav。包含了spring-boot-starter, spring-boot-starter-aop, spring-boot-starter-jdbc, hibernate-core, hibernate-entitymanager, javax.transaction-api, spring-data-jpa, spring-aspects。vim

 

二、配置appliaction.properties 文件,主要是配置datasource和jpa。centos

1 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2 spring.datasource.url=jdbc:mysql:///你的數據庫名
3 spring.datasource.username=你的用戶名
4 spring.datasource.password=你的密碼
5 spring.jpa.hibernate.ddl-auto=update
6 spring.jpa.show-sql=true

三、定義一個接口,該接口繼承JpaRepository接口,而後定義一個操做數據庫的接口方法。在服務層經過@Autowired註解或者構造方法注入該接口就能夠用來實現相關的業務邏輯。jpa用到的註解和有關的類在spring-data-jpa-1.11.12.jar和hibernate-jpa-2.1-api-1.0.0.final.jar中api

四、最後啓動main方法,main方法有註解@SpringBootApplication。數組

 

2018-10-03app

可操做數據庫

原生jdbc API、數據庫鏈接池(c3p0,dbcp,druid等)、hibernate、jpa等。

 

jpa和hibernate的關係

jpa是規範,jpa是hibernate的抽象,而hibernate是jpa的實現。

 

jpa必知必會三個方面

①orm(xml和註解) ②API ③jpql

 

ORM(object-relational mapping) 對象關係映射。

ORM包括兩個方面的內容:

①對象的狀態映射成數據庫的列

②經過對象查詢

 

兩種類型的註解

①邏輯方面的註解:從對象模型視圖中描述實體模型,與領域模型和元數據的分類牢牢綁定在一塊兒。

②物理方面的註解:和數據庫具體的數據模型相關,處理表,列,約束和其它數據庫級別的組件。

Persistence註解能使用在三個不一樣的位置:class,method,field

 

java type和jdbc type的轉換???

java type ----> check經過----> jdbc type ----> jdbc driver

                       check不經過 ----> 拋出異常

 

經常使用註解,分jpa1.x和jpa2.x

jpa1.x

@Entity

@Id

註解@Entity和@Id組合能夠建立和映射一個實體到數據庫的表。

@GeneratedValue

@GenerateValue(strategy=GenerationType.AUTO)

註解@GenerateValue能夠指定標識符生成器的生成策略。有4種,TABLE,  SEQUENCE,  IDENTITY,  AUTO,默認是AUTO。

identity適用於數據庫支持自動增加。例如mysql。數據庫表的id字段最終會有auto_increment的約束。

@Table

@Table(name="tableName", schema="schemaName")

註解@Table能夠重命名默認的表名,還能夠重命名數據庫的schama(用於區分不一樣的表)和catalog

@Basic

@Basic(fetch=FetchType.LAZY)

註解@Basic表示該屬性能夠被持久化,也就是java type能與jdbc type對應轉換。

fetch=FetchType.LAZY表示延遲加載。

@Lob

註解@Lob和@Basic,@Column一塊兒使用。

lob(large object) 大對象;

clob(character large object) 字符大對象,包括char[],Character[],String。

blob(binary large object) 二進制大對象,包括byte[],Byte[],Serializable。

@Enumerated

註解@Enumerated(EnumType.ORDINAL),這是默認的enum類型,在數據庫表中將被映射成Integer。能夠設置成STRING。

@TableGenerator

@Column

@Conlumn(name="columnName")

註解@Column能夠覆蓋原來的column名字

註解@Column的屬性:

String name() default ""; // 重命名column的名字
boolean unique() default false;
boolean nullable() default true; // 設置是否容許空值,默認是容許的,true
boolean insertable() default true;
boolean updatable() default true;
String columnDefinition() default "";  // 能夠自定義column在數據庫中的表現
String table() default "";
int length() default 255; // 指定類型爲String, char[], or Character[]的長度
int precision() default 0;  // 和scale屬性一塊兒使用,能夠指定數字的精度
int scale() default 0; // 和precision屬性一塊兒使用,能夠指定數字的精度

@Temporal

@Temporal(TemporalType.TIMESTAMP)。

註解@Temporal有三種可映射時間類型:DATE,  TIME,  TIMESTAMP,它們是java.sql類型。

@Transient

註解@Transient能夠把不須要被持久化的字段不用被持久化到數據庫表

 

關係概念:

roles,directionality,cardinality,ordinality

 

single-valued association

source ----> target[one]

@ManyToOne

註解@ManyToOne屬性:沒有mappedBy,其餘和註解@OneToOne屬性同樣

@JoinColumn

@JoinColumn(name="fk_name")

註解@JoinColumn表示A表的外鍵關聯B表的主鍵,也就是指定關聯的外鍵

@OneToOne

@OneToOne(mappedBy="attributeName", fetch=FetchType.LAZY) // 雙向one-to-one映射時使用。

註解@OneToOne屬性:

Class targetEntity() default void.class;
CascadeType[] cascade() default {}; // 5個可選的CascadeType數組元素:ALL,  PERSIST,  MERGE,  REMOVE,  REFRESH;
FetchType fetch() default FetchType.EAGER; // 兩個可選的FetchType:LAZY,  EAGER;
boolean optional() default true;
String mappedBy() default "";

collection-valued association

@OneToMany

mappedBy="attributeName",該屬性用於雙向映射

註解@OneToMany屬性:沒有option,其餘和註解@OneToOne屬性同樣

@ManyToMany

mappedBy="attributeName",該屬性用於雙向映射

註解@ManyToMany屬性:沒有option,其餘和註解@OneToOne屬性同樣

@JoinTable

註解@JionTable和@ManyToMany結合使用,用於存儲兩張表的外鍵。

 

@Embeddable

註解@Embeddable用在類中,這個類沒有惟一標識符,表示這個類能夠被其餘類重用。

@Embedded

註解@Embedded用在字段中,表示這個字段引用了@Embeddable註解的類。

@AttributeOverrides

@AttributeOverride

註解@AttributeOverrides能夠嵌套註解@AttributeOverride,覆蓋默認的字段。

jpa2.x

@Access

access mode 訪問模式,指的是對象狀態的訪問方式。它有兩種註解方式,一種是在字段上註解,此種方法能夠經過反射的方法訪問到field;一種是在getter方法上註解,此種方式是直接調用getter和setter方法訪問field。它們能夠被進一步定義爲feild access和property access。對於property access,規定getter方法的返回類型要跟setter方法傳入的參數的類型一致;getter方法的訪問修飾符必須是public或者protect;註解必須在getter方法上。若是使用property access方式,那麼映射到數據庫中的列將取決於getter和setter方法後面的名詞。

@ElementCollection

@CollectionTable

@OrderColumn

@MapKeyColumn

@MapKeyEnumerated

@MapKeyTemporal

@MapKeyJoinColumn

@MapKeyClass

相關文章
相關標籤/搜索