Spring Boot學習日誌(二)JPA操作Mysql
阿新 • • 發佈:2019-01-07
Spring Boot學習日誌(二)JPA基本操作
什麼是JPA
Jpa簡單說就是一個ORM持久層框架,可以讓開發人員一行sql不寫,實現基本的增刪改查操作。
匯入依賴庫
在新建專案時,勾選Web,JPA和Mysql(因為我使用的Mysql測試,所以也要用到Mysql的依賴)
<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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
資料庫配置
#資料庫型別
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#資料庫連結地址
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 &serverTimezone=UTC&allowMultiQueries=true&&useSSL=false
#使用者名稱
spring.datasource.username = root
#密碼
spring.datasource.password = root
建立測試用到的表
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL,
`username` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
建立實體類
準備工作已經做完了,下面開始jpa相關的東西。
既然jpa是一個ORM框架,那麼一定會有一個實體類和表對應。
新建UserEntity類
@Entity
@Table(name = "tb_user")
public class UserEntity {
@Id
@Column(name = "id")
private Integer id;
@Column(name = "username")
private String username;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
- @Entity 註解表示該類是一個實體類
- @Table 用於設定該實體對應的表名稱
- @Id 用於標註該欄位是主鍵
- @Column 表示該欄位為資料庫一列
建立Jpa
建立一個名為UserJpa的介面,讓它繼承JpaRepository(SpringDataJPA簡單資料操作介面)
public interface UserJpa extends JpaRepository<UserEntity,Long>,Serializable{
}
做完了這個操作,就可以通過UserJpa來操作資料庫了。
操作資料庫
先建立一個Controller作為入口來使用Jpa
@RequestMapping(value = "/user")
@RestController
public class UserController {
@Autowired
private UserJpa jpa;
@RequestMapping(value = "/all")
public List<UserEntity> getAllUser(){
return jpa.findAll();
}
@RequestMapping(value = "/save")
public UserEntity saveUser(UserEntity userEntity){
return jpa.save(userEntity);
}
@RequestMapping(value = "/delete")
public void deleteAll(UserEntity userEntity){
jpa.delete(userEntity);
}
}
我們在使用Jpa的時候,不需要我們自己初始化,在宣告jpa的地方加上@Autowired註解,這時候我們就可以使用他自帶的方法了
如下
T save(T entity);//儲存單個實體
Iterable<T> save(Iterable<? extends T> entities);//儲存集合
T findOne(ID id);//根據id查詢實體
boolean exists(ID id);//根據id判斷實體是否存在
Iterable<T> findAll();//查詢所有實體
long count();//查詢實體數量
void delete(ID id);//根據Id刪除實體
void delete(T entity);//刪除一個實體
void delete(Iterable<? extends T> entities);//刪除一個實體的集合
void deleteAll();//刪除所有實體
這裡先使用簡單的增|、刪、改、查。
增加資料
@RequestMapping(value = "/save")
public UserEntity saveUser(UserEntity userEntity){
return jpa.save(userEntity);
}
jpa.save會將我們傳進來的實體物件儲存在資料庫中,並將操作的結果返回給我們。
查詢資料
@RequestMapping(value = "/all")
public List<UserEntity> getAllUser(){
return jpa.findAll();
}
使用findAll會得到一個list物件,通過Controller的RequestMapping註解訪問這個方法會直接得到一個json物件。
更新資料
更新資料和新增資料一樣,都是使用jpa.save
如果該主鍵的資料不存在,會自動向資料庫新增該資料,如果該主鍵已經存在,會根據該主鍵更新該資料其他欄位。
刪除資料
@RequestMapping(value = "/delete")
public void deleteAll(UserEntity userEntity){
jpa.delete(userEntity);
}
通過jap.delete會自動根據userEntity的主鍵刪掉對應資料。