spring-data-jpa簡介和springboot整合spring-data-jpa
在我們的項(xiàng)目開發(fā)中,數(shù)據(jù)庫的訪問及存儲都是最為核心的部分,SpringBoot為我們提供了多種數(shù)據(jù)庫來做數(shù)據(jù)的存儲及讀取。目前企業(yè)開發(fā)中應(yīng)用最為廣泛的數(shù)據(jù)庫有问拘,關(guān)系型數(shù)據(jù)庫MySQL丐吓,oracle娄徊,sqlserver瀑罗,非關(guān)系型數(shù)據(jù)庫redis胸嘴,mongodb等。
本章將通過使用SpringBoot訪問MySQL結(jié)合SpringDataJPA完成CRUD(Create,Read,Update,Delete)簡單操作斩祭。
一:什么是JPA
JPA(Java Persistence API)是一種Java持久化解決方案劣像,負(fù)責(zé)把數(shù)據(jù)保存到數(shù)據(jù)庫中,實(shí)際上它是一種Java提供的一種標(biāo)準(zhǔn)摧玫、規(guī)范耳奕。而不是一種實(shí)現(xiàn)技術(shù)。JPA屬于重量級的诬像,因?yàn)樗枰\(yùn)行在JAVA EE容器中屋群,而Spring-Data-Jpa 提供了輕量級的實(shí)現(xiàn),在任何Servlet容器中都能運(yùn)行坏挠。
Spring-Data-Jpa底層使用的是Hibernate的JPA技術(shù)實(shí)現(xiàn)芍躏。——Hibernate和Mybatis在ORM實(shí)現(xiàn)上的區(qū)別
二:什么是spring-data-jpa
Spring-Data是Spring家
族一個(gè)用于簡化數(shù)據(jù)庫訪問,并且支持云服務(wù)的開源框架降狠,主要目標(biāo)是使訪問數(shù)據(jù)庫更便捷对竣,Spring-Data包含多個(gè)子項(xiàng)目,Srping-Data-Jpa是其中之一榜配,還包括Spring-Data-Redis,Spring-Data-MongoDb等
三:springboot整合spring-data-jpa
- 添加mysql支持
<!--引入MySQL的依賴關(guān)系-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
- 添加JPA依賴支持
<!--引入JPA的依賴關(guān)系-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
- 新建數(shù)據(jù)庫和application配置
-新建數(shù)據(jù)庫
create database is not exists data_jpa default charset utf8 collate utf8_general_ci;
grant all privileges on data_jpa.* to database-user@'localhost';
flush privileges;
-配置spring-boot application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/data_jpa?characterEncoding=utf-8
username: database-name
password: your password
driver-class-name: com.mysql.jdbc.Driver
freemarker:
cache: false
#jpa配置
jpa:
database: mysql
show-sql: true
- 生成數(shù)據(jù)表實(shí)體類
Intellij IDEA可以通過添加JPA插件來生成數(shù)據(jù)庫表對象否纬。
-添加Project-Structure Modules中JPA的支持。
生成的實(shí)體類
@Entity
@Table(name = "t_user", schema = "data_jpa", catalog = "")
public class TUserEntity {
private int tId;
private String tName;
private byte tAge;
private String tAddress;
@Id
@Column(name = "t_id")
public int gettId() {
return tId;
}
public void settId(int tId) {
this.tId = tId;
}
...
}
- 創(chuàng)建Model的JPA接口
package cn.orcish.springdatajpademo.jpa;
import cn.orcish.springdatajpademo.model.TUserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.io.Serializable;
public interface UserJPA extends JpaRepository<TUserEntity,Long>,JpaSpecificationExecutor<TUserEntity>,Serializable {
}
6)創(chuàng)建Controller
@RestController
@RequestMapping(value = "/user")
public class JPAController {
@Autowired
private UserJPA userJPA;
@RequestMapping(value="/save",method= RequestMethod.POST)
public TUserEntity save(TUserEntity user){
return userJPA.save(user);
}
@RequestMapping(value = "/findAll",method = RequestMethod.GET)
public List<TUserEntity> list(){
return userJPA.findAll();
}
@RequestMapping("/delete")
public List<TUserEntity> delete(Long id){
userJPA.deleteById(id);
return userJPA.findAll();
}
}
- 請求測試
Intellij IDEA 提供了一個(gè)Rest Client工具對接口進(jìn)行測試蛋褥,當(dāng)然也可以使用PostMan,這里介紹下Rest Client的安裝和使用临燃。
-
file->Settings->Plugins搜索Rest Client,
安裝Rest Client
使用
成功返回