SpringBoot學習(三)--構建RESTFUL API並用spring-data-jpa進行儲存&&使用IDEA反向生成帶註釋的例項
阿新 • • 發佈:2018-12-07
構造User物件(/domain),如果有資料庫的表可以直接使用IDEA反向生成例項的類。過程如下:
開啟IDEA:View-ToolWindows-Database。新增資料來源,選擇資料庫型別,輸入資料庫的ip。如果不是預設Windows登陸資料庫,則取消勾選然後填入使用者名稱,密碼。如果Test Connection是暗的,則看看左下角有沒有提醒下載驅動,有的話就下載後Test。成功後就添加了。
在resource資料夾下新建檔案“hibernate.cfg.xml”
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://xxx;database=xxx</property >
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<!--
<property name="connection.pool_size">1</property>
-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--
<property name="hbm2ddl.auto">update</property>
-->
</session-factory>
</hibernate-configuration>
我這裡用的是SQLServer,如果別的資料庫,記得修改方言等屬性。connection url在之前新增的資料庫中可以找到。儲存後在ToolWindows中就可以找到Persistence(沒有新增Hibernate.cfg.xml前是沒有的)。
右鍵Persistence視窗中的專案
選擇資料來源(也就是剛剛新增過的)然後選擇要生成的表。
如圖勾選。Generate Separate XML per Entity勾選的話每個生成的例項類都會同時生成一個相應的配置檔案UserEntity.hbm.xml 檔案。Generate Column Properties是利用標註的方式進行資料庫表與實體類的屬性的匹配。這裡選擇第二種方式。
生成的例項類如下
package com.changhe.Domain;
import javax.persistence.*;
import java.util.Objects;
@Entity
public class UserEntity {
private int id;
private String name;
private Integer age;
@Id
@Column(name = "id")
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "age")
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserEntity that = (UserEntity) o;
return id == that.id &&
Objects.equals(name, that.name) &&
Objects.equals(age, that.age);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age);
}
}
自己寫也行。
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Integer age;
//getter setter 省略
}
接下來建立實體類介面
package com.changhe.Domain;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<UserEntity,Integer> {
}
這是最通用的介面。裡面全是預設的方法。jpa可以不實現然後定義很多方法。比如想按名字查詢則
public UserEntity findByName(String Name);
就可以用了。如果沒有宣告則用不了。
這樣就可以直接對資料庫操作了。
之後就是RESTFUL的操作。跟上一篇一樣即可。主要的不同是jdbcTemplate和jpa的不同而已。對外介面一致。