溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot集成JPA的示例代碼

發(fā)布時(shí)間:2020-09-11 19:05:53 來源:腳本之家 閱讀:168 作者:千里明月 欄目:編程語言

本文介紹了SpringBoot集成JPA的示例代碼,分享給大家,具體如下:

1.創(chuàng)建新的maven項(xiàng)目

SpringBoot集成JPA的示例代碼

2. 添加必須的依賴

  <!--springboot的必須依賴-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>

  <dependencies>
    <!--啟動springmvc的相關(guān)配置,springboot的自動配置-->
    <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ū)動-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
  </dependencies>

3. 新建springboot啟動類

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
  1. update:Hibernate根據(jù)給定的Entity結(jié)構(gòu)改變數(shù)據(jù)庫。
  2. create: 每次都會創(chuàng)建數(shù)據(jù)庫,關(guān)閉時(shí)不會刪除
  3. none: mysql的默認(rèn)設(shè)置 , 不改變數(shù)據(jù)結(jié)構(gòu)
  4. create-drop: 創(chuàng)建數(shù)據(jù)庫,但是每次sessionFactory關(guān)閉后都會刪除

5. 新建實(shí)體類User

這個時(shí)候其實(shí)已經(jīng)可以啟動springboot, 但是不會生成數(shù)據(jù)表,因?yàn)檫€沒有配置實(shí)體類的jpa

SpringBoot集成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.
 */
//表明這是個需要生成數(shù)據(jù)表的類
@Entity
public class User {
//  定義主鍵id
  @Id
//  聲明一個策略通用生成器,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í)候啟動項(xiàng)目,就會在指定位置下生成一個user數(shù)據(jù)表

SpringBoot集成JPA的示例代碼

6. 實(shí)現(xiàn)CRUD

CrudRepository是一個提供了普通增刪改查方法的接口,由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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI