最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成JPA的示例代碼

 更新時(shí)間:2018年01月23日 14:31:20   作者:千里明月  
本篇文章主要介紹了SpringBoot集成JPA的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Springboot升級(jí)到2.7.2結(jié)合nacos遇到的坑及解決

    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ù)條等類型)

    這篇文章主要介紹了Java 設(shè)置Excel條件格式示例代碼(高亮條件值、應(yīng)用單元格值/公式/數(shù)據(jù)條等類型),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java編程實(shí)現(xiàn)排他鎖代碼詳解

    Java編程實(shí)現(xiàn)排他鎖代碼詳解

    這篇文章主要介紹了Java編程實(shí)現(xiàn)排他鎖的相關(guān)內(nèi)容,敘述了實(shí)現(xiàn)此代碼鎖所需要的功能,以及作者的解決方案,然后向大家分享了設(shè)計(jì)源碼,需要的朋友可以參考下。
    2017-10-10
  • 關(guān)于Spring啟動(dòng)流程及Bean生命周期梳理

    關(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)

    這篇文章主要介紹了mybatis 批量將list數(shù)據(jù)插入到數(shù)據(jù)庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Spring中BeanFactory解析bean詳解

    Spring中BeanFactory解析bean詳解

    本篇文章主要介紹了Spring中BeanFactory解析bean詳解 ,詳細(xì)的介紹了使用BeanFactory對(duì)bean進(jìn)行解析的實(shí)例,有興趣的可以了解一下。
    2017-04-04
  • 基于指針pointers和引用references的區(qū)別分析

    基于指針pointers和引用references的區(qū)別分析

    本篇文章介紹了,基于指針pointers和引用references的區(qū)別分析。需要的朋友參考下
    2013-05-05
  • SpringCloud之Hystrix的詳細(xì)使用

    SpringCloud之Hystrix的詳細(xì)使用

    熔斷機(jī)制是應(yīng)對(duì)雪崩效應(yīng)的一種微服務(wù)鏈路保護(hù)機(jī)制,當(dāng)扇出鏈路的某個(gè)微服務(wù)出錯(cuò)不可用或者響應(yīng)時(shí)間太長(zhǎng),會(huì)進(jìn)行服務(wù)的降級(jí),進(jìn)而熔斷該節(jié)點(diǎn)微服務(wù)的調(diào)用,快速返回錯(cuò)誤的相應(yīng)信息,本文重點(diǎn)給大家介紹SpringCloud Hystrix使用,感興趣的朋友一起看看吧
    2022-01-01
  • Java多線程定時(shí)器Timer原理及實(shí)現(xiàn)

    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
  • 詳解Java Spring AOP

    詳解Java Spring AOP

    這篇文章主要為大家介紹了Java Spring AOP,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01

最新評(píng)論

榆树市| 钦州市| 稷山县| 新津县| 依安县| 稻城县| 长白| 龙江县| 阿巴嘎旗| 平乡县| 通榆县| 高雄市| 台湾省| 杭锦后旗| 南京市| 万年县| 明溪县| 天长市| 休宁县| 介休市| 二手房| 腾冲县| 汕尾市| 新闻| 汉中市| 泌阳县| 辛集市| 浏阳市| 芒康县| 沧州市| 乐陵市| 习水县| 大兴区| 大同县| 天祝| 耒阳市| 广元市| 固安县| 庆阳市| 合作市| 琼结县|