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

Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫(kù)表

 更新時(shí)間:2021年09月11日 15:25:01   作者:Insist_on_progress  
這篇文章主要介紹了Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫(kù)表的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Springboot 實(shí)體類生成數(shù)據(jù)庫(kù)表

JPA:springboot -jpa:數(shù)據(jù)庫(kù)的一系列的定義數(shù)據(jù)持久化的標(biāo)準(zhǔn)的體系

學(xué)習(xí)的目的是:

利用springboot實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的操作

第一步:添加springboot-data-jpa和數(shù)據(jù)庫(kù)的依賴關(guān)系

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
       </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

第二步:編寫yml文件的配置

server:
  port: 8001
spring:
  application:
    name: jih-manage
  datasource:
    name: test
    url: jdbc:mysql://111.231.231.56/jih
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

第三步:實(shí)體類中使用的注解

  • @Entity 實(shí)體類的注解
  • @Id 映射到表格中id的屬性
  • @Gernertervalue 添加其自增的屬性

第四步:?jiǎn)?dòng)項(xiàng)目是否生成表格

補(bǔ)充的知識(shí)點(diǎn):

根據(jù)實(shí)體類生成數(shù)據(jù)庫(kù)的表配置文件有倆種方式分別是yml和properties文件進(jìn)行配置

yml文件:

spring:
    datasource:
        driver-class-name:  com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/facemap
        username: root
        password: root
    jpa:
        hibernate:
            ddl-auto: update
            show-sql: true

properties文件的寫法:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/dbgirl?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql= true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jackson.serialization.indent_output=false

有更加詳細(xì)介紹

參考網(wǎng)址:

//m.fzitv.net/article/222622.htm

實(shí)體類的寫法:

package com.example.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@Entity //實(shí)體類的注解
public class Girl {
    @Id //@id注意選擇這個(gè)javax.persistence
    @GeneratedValue
    private  Integer  id;
    private  String   cupSize;
    private  Integer   age;
    public Girl() {
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getCupSize() {
        return cupSize;
    }
    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

第五步:?jiǎn)?dòng)項(xiàng)目即可

完成~

springboot繼承JPA根據(jù)實(shí)體類生成數(shù)據(jù)庫(kù)中的表

首先搭建springboot框架。搭建完成之后:

1. pom中添加的依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
 
        <!--mysql-connection-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2. application.yml中配置jpa配置

server:
  port: 8080
 
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/h5mall?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
    hikari:
      minimum-idle: 5
      idle-timeout: 180000
      maximum-pool-size: 10
      auto-commit: true
      pool-name: MyHikariCP
      connection-timeout: 30000
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

其中jpa下的jpa.hibernate.ddl-auto屬性值有如下:

  • ddl-auto:create (每次運(yùn)行該程序,沒(méi)有表格會(huì)新建表格,表內(nèi)有數(shù)據(jù)會(huì)清空)
  • ddl-auto:create-drop (每次程序結(jié)束的時(shí)候會(huì)清空表)
  • ddl-auto:update (每次運(yùn)行程序,沒(méi)有表格會(huì)新建表格,表內(nèi)有數(shù)據(jù)不會(huì)清空,只會(huì)更新)
  • ddl-auto:validate(運(yùn)行程序會(huì)校驗(yàn)數(shù)據(jù)與數(shù)據(jù)庫(kù)的字段類型是否相同,不同會(huì)報(bào)錯(cuò))

一般情況下選擇update,其他屬性值慎用!

定義用戶實(shí)體類,通過(guò)注解映射成數(shù)據(jù)庫(kù)中的表

 
import javax.persistence.*; 
@Entity
@Table(name = "user")
@Data
public class User {
 
    @Id
    @GeneratedValue
    private Long id;
 
    //name屬性為表的字段名。length為字段的長(zhǎng)度
    @Column(length = 30, name = "userId")
    private String userId;
 
    @Column(name = "userName", length = 20, columnDefinition="varchar(100) COMMENT '用戶名'")
    private String userName;
 
    @Column(name = "phone", length = 20)
    private String phone;
 
    @Column(name = "password", length = 30)
    private String password;
 
    @Column(name = "userRealName", length = 20)
    private String userRealName;
 
    @Column(name = "address", length = 20)
    private String address;
}

啟動(dòng)springboot項(xiàng)目

可看到控制臺(tái)上顯示了創(chuàng)建表中的

然后查看數(shù)據(jù)庫(kù)中是否生成了對(duì)應(yīng)的表:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • MyBatis主鍵自增的兩種實(shí)現(xiàn)方法

    MyBatis主鍵自增的兩種實(shí)現(xiàn)方法

    本文主要介紹了MyBatis主鍵自增的兩種實(shí)現(xiàn)方法,主要包括注解方式或配置文件方式來(lái)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • Spring @Bean注解的使用場(chǎng)景與案例實(shí)現(xiàn)

    Spring @Bean注解的使用場(chǎng)景與案例實(shí)現(xiàn)

    隨著SpringBoot的流行,我們現(xiàn)在更多采用基于注解式的配置從而替換掉了基于XML的配置,所以本篇文章我們主要探討基于注解的@Bean以及和其他注解的使用
    2023-03-03
  • SpringBoot實(shí)現(xiàn)token登錄的示例代碼

    SpringBoot實(shí)現(xiàn)token登錄的示例代碼

    在進(jìn)行登錄驗(yàn)證時(shí),我們需要session或cookie會(huì)話進(jìn)行驗(yàn)證,當(dāng)我們脫離瀏覽器用app等向服務(wù)端發(fā)請(qǐng)求就沒(méi)有session和cookie機(jī)制,這時(shí)我們就需要使用token令牌進(jìn)行登錄驗(yàn)證,本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2022-03-03
  • java簡(jiǎn)單實(shí)現(xiàn)復(fù)制 粘貼 剪切功能代碼分享

    java簡(jiǎn)單實(shí)現(xiàn)復(fù)制 粘貼 剪切功能代碼分享

    本文給大家分享了一段java編寫的簡(jiǎn)單實(shí)現(xiàn)復(fù)制粘貼剪切功能的代碼,需要的小伙伴可以直接拿走使用。如有更好的方案,也可以告之本人。
    2014-11-11
  • 如何在SpringBoot+Freemarker中獲取項(xiàng)目根目錄

    如何在SpringBoot+Freemarker中獲取項(xiàng)目根目錄

    這篇文章主要介紹了如何在SpringBoot+Freemarker中獲取項(xiàng)目根目錄的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringCloud使用Kafka Streams實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)處理

    SpringCloud使用Kafka Streams實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)處理

    使用Kafka Streams在Spring Cloud中實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)處理可以幫助我們構(gòu)建可擴(kuò)展、高性能的實(shí)時(shí)數(shù)據(jù)處理應(yīng)用,Kafka Streams是一個(gè)基于Kafka的流處理庫(kù),本文介紹了如何在SpringCloud中使用Kafka Streams實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)處理,需要的朋友可以參考下
    2024-07-07
  • Java中的并發(fā)工具類詳細(xì)解析

    Java中的并發(fā)工具類詳細(xì)解析

    這篇文章主要介紹了Java中的并發(fā)工具類詳細(xì)解析,CountDownLatch、 CyclicBarrier 和 Semaphore 工具類提供了一種并發(fā)流程控制的手段,Exchanger 工具類則提供了在線程間交換數(shù)據(jù)的一種手段,需要的朋友可以參考下
    2023-12-12
  • java實(shí)現(xiàn)多個(gè)文件壓縮成壓縮包

    java實(shí)現(xiàn)多個(gè)文件壓縮成壓縮包

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)多個(gè)文件壓縮成壓縮包,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • SpringBoot最常用的50個(gè)注解總結(jié)(全是干貨!)

    SpringBoot最常用的50個(gè)注解總結(jié)(全是干貨!)

    SpringBoot提供多種注解簡(jiǎn)化配置與啟動(dòng)流程,如@SpringBootAppication、@RestController、@RequestMapping等,這篇文章主要介紹了SpringBoot最常用的50個(gè)注解的相關(guān)資料,需要的朋友可以參考下
    2024-09-09
  • Java反射機(jī)制的簡(jiǎn)單講解

    Java反射機(jī)制的簡(jiǎn)單講解

    這篇文章主要介紹了Java反射機(jī)制的簡(jiǎn)單講解,本文講解了Java的高級(jí)概念反射機(jī)制,通過(guò)文字介紹案例該項(xiàng)概念和代碼的詳細(xì)展示,需要的朋友可以參考下
    2021-07-07

最新評(píng)論

平舆县| 南平市| 遂昌县| 略阳县| 北安市| 嵩明县| 手机| 定襄县| 外汇| 延津县| 芜湖县| 濮阳县| 麻城市| 银川市| 廉江市| 尤溪县| 汉源县| 灌云县| 闽清县| 淮南市| 汕头市| 龙岩市| 太康县| 磴口县| 青铜峡市| 长丰县| 卢氏县| 南昌县| 肥乡县| 尼勒克县| 昆明市| 常山县| 德州市| 都安| 四子王旗| 隆子县| 驻马店市| 泰和县| 禄劝| 邵阳县| 泰顺县|