SpringBoot集成JPA的示例代碼
本文介紹了SpringBoot集成JPA的示例代碼,分享給大家,具體如下:
1.創(chuàng)建新的maven項(xiàng)目

2. 添加必須的依賴
<!--springboot的必須依賴-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!--啟動(dòng)springmvc的相關(guān)配置,springboot的自動(dòng)配置-->
<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驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
3. 新建springboot啟動(dòng)類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
4. 在resources跟目錄下新建application.properties
#建立/更新數(shù)據(jù)表的配置 spring.jpa.hibernate.ddl-auto=update #數(shù)據(jù)庫地址 spring.datasource.url=jdbc:mysql://localhost:3306/qian?useUnicode=true&characterEncoding=utf-8 #數(shù)據(jù)庫用戶名 spring.datasource.username=root #數(shù)據(jù)庫密碼 spring.datasource.password=123
- update:Hibernate根據(jù)給定的Entity結(jié)構(gòu)改變數(shù)據(jù)庫。
- create: 每次都會(huì)創(chuàng)建數(shù)據(jù)庫,關(guān)閉時(shí)不會(huì)刪除
- none: mysql的默認(rèn)設(shè)置 , 不改變數(shù)據(jù)結(jié)構(gòu)
- create-drop: 創(chuàng)建數(shù)據(jù)庫,但是每次sessionFactory關(guān)閉后都會(huì)刪除
5. 新建實(shí)體類User
這個(gè)時(shí)候其實(shí)已經(jīng)可以啟動(dòng)springboot, 但是不會(huì)生成數(shù)據(jù)表,因?yàn)檫€沒有配置實(shí)體類的jpa

先新建user.java
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by Andy on 2018/1/20.
*/
//表明這是個(gè)需要生成數(shù)據(jù)表的類
@Entity
public class User {
// 定義主鍵id
@Id
// 聲明一個(gè)策略通用生成器,name為”system-uuid”,策略strategy為”uuid”。
@GenericGenerator(name = "system-uuid", strategy ="uuid")
// 用generator屬性指定要使用的策略生成器。
@GeneratedValue(generator = "system-uuid")
private String id;
private String name;
private Integer age;
private Boolean sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
}
這時(shí)候啟動(dòng)項(xiàng)目,就會(huì)在指定位置下生成一個(gè)user數(shù)據(jù)表

6. 實(shí)現(xiàn)CRUD
CrudRepository是一個(gè)提供了普通增刪改查方法的接口,由spring內(nèi)部提供,我們只需調(diào)用即可
@NoRepositoryBean
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
<S extends T> S save(S var1);
<S extends T> Iterable<S> save(Iterable<S> var1);
T findOne(ID var1);
boolean exists(ID var1);
Iterable<T> findAll();
Iterable<T> findAll(Iterable<ID> var1);
long count();
void delete(ID var1);
void delete(T var1);
void delete(Iterable<? extends T> var1);
void deleteAll();
}
新建UserRepository.java
public interface UserRepository extends CrudRepository<User, String> {
}
7. 實(shí)現(xiàn)controller控制
新建UserController.java
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/add")
public User add(String name){
User user = new User();
user.setName(name);
return userRepository.save(user);
}
@RequestMapping("/list")
public Iterable<User> list(){
Iterable<User> all = userRepository.findAll();
return all;
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot集成Spring Data JPA及讀寫分離
- SpringBoot JPA 表關(guān)聯(lián)查詢實(shí)例
- SpringBoot JPA實(shí)現(xiàn)增刪改查、分頁、排序、事務(wù)操作等功能示例
- SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進(jìn)行操作
- SpringBoot整合JPA的實(shí)例代碼
- springboot使用JPA時(shí)間類型進(jìn)行模糊查詢的方法
- IDEA+maven+SpringBoot+JPA+Thymeleaf實(shí)現(xiàn)Crud及分頁
- SpringBoot入門系列之JPA mysql
- SpringBoot Data JPA 關(guān)聯(lián)表查詢的方法
- SpringBoot JPA實(shí)現(xiàn)查詢多值
相關(guān)文章
Springboot升級(jí)到2.7.2結(jié)合nacos遇到的坑及解決
這篇文章主要介紹了Springboot升級(jí)到2.7.2結(jié)合nacos遇到的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Java 設(shè)置Excel條件格式示例代碼(高亮條件值、應(yīng)用單元格值/公式/數(shù)據(jù)條等類型)
這篇文章主要介紹了Java 設(shè)置Excel條件格式示例代碼(高亮條件值、應(yīng)用單元格值/公式/數(shù)據(jù)條等類型),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理
這篇文章主要介紹了關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
mybatis 批量將list數(shù)據(jù)插入到數(shù)據(jù)庫的實(shí)現(xiàn)
這篇文章主要介紹了mybatis 批量將list數(shù)據(jù)插入到數(shù)據(jù)庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
基于指針pointers和引用references的區(qū)別分析
本篇文章介紹了,基于指針pointers和引用references的區(qū)別分析。需要的朋友參考下2013-05-05
Java多線程定時(shí)器Timer原理及實(shí)現(xiàn)
這篇文章主要介紹了Java多線程定時(shí)器Timer原理及實(shí)現(xiàn),涉及Timer的schedule的使用,定時(shí)器Timer的schedule等相關(guān)內(nèi)容以及代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

