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

SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù)的方法

 更新時(shí)間:2021年09月03日 15:27:07   作者:java干貨  
H2是Thomas Mueller提供的一個(gè)開(kāi)源的、純java實(shí)現(xiàn)的關(guān)系數(shù)據(jù)庫(kù)。本文主要介紹了SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的可以了解一下

H2是Thomas Mueller提供的一個(gè)開(kāi)源的、純java實(shí)現(xiàn)的關(guān)系數(shù)據(jù)庫(kù)。

前言

本篇文章引導(dǎo)你使用Spring Boot,Spring Data JPA集成H2內(nèi)存數(shù)據(jù)庫(kù)。更多關(guān)于H2數(shù)據(jù)參考:http://www.h2database.com/html/tutorial.html

準(zhǔn)備

  • JDK 1.8 或更高版本
  • Maven 3 或更高版本

技術(shù)棧

  • Spring Data JPA
  • Spring Boot

目錄結(jié)構(gòu)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>jpa-example</artifactId>
        <groupId>cn.merryyou</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>h2-webconsole</artifactId>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
 
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.196</version>
        </dependency>
 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

實(shí)體類 User

@Entity
@Table(name = "t_user")
@Data
public class User {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    private String name;
 
    private String url;
 
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", url='" + url + '\'' +
                '}';
    }
}
  • @Table聲明此對(duì)象映射到數(shù)據(jù)庫(kù)的數(shù)據(jù)表,通過(guò)它可以為實(shí)體指定表(talbe),目錄(Catalog)和schema的名字。該注釋不是必須的,如果沒(méi)有則系統(tǒng)使用默認(rèn)值(實(shí)體的短類名)。
  • @Id 聲明此屬性為主鍵。該屬性值可以通過(guò)應(yīng)該自身創(chuàng)建,但是Hibernate推薦通過(guò)Hibernate生成
  • @GeneratedValue 指定主鍵的生成策略。
    • TABLE:使用表保存id值
    • IDENTITY:identitycolumn
    • SEQUENCR :sequence
    • AUTO:根據(jù)數(shù)據(jù)庫(kù)的不同使用上面三個(gè)

@Column 聲明該屬性與數(shù)據(jù)庫(kù)字段的映射關(guān)系。

AddressRepository

public interface UserRepository extends JpaRepository<User, Integer> {
}

Spring Data JPA包含了一些內(nèi)置的Repository,實(shí)現(xiàn)了一些常用的方法:findonefindall,save等。

application.yml

spring:
  datasource:
    url: jdbc:h2:mem:h2test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    platform: h2
    username: sa
    password:
    driverClassName: org.h2.Driver
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        show_sql: true
        use_sql_comments: true
        format_sql: true
  h2:
    console:
      enabled: true
      path: /console
      settings:
        trace: false
        web-allow-others: false
logging:
  level: debug

連接配置

application.yml文件中對(duì)數(shù)據(jù)庫(kù)進(jìn)行連接配置

  • spring.datasource.url=jdbc:h2:mem:h2test,配置h2數(shù)據(jù)庫(kù)的連接地址
  • spring.datasource.driver-class-name=org.h2.Driver,配置JDBC Driver
  • spring.datasource.username=sa,配置數(shù)據(jù)庫(kù)用戶名
  • spring.datasource.password=,配置數(shù)據(jù)庫(kù)密碼

當(dāng)你完成依賴和連接配置這兩步之后,你就可以在程序種使用h2了。spring會(huì)自動(dòng)幫你完成DataSource的注入。

數(shù)據(jù)初始化配置

如果你需要在程序啟動(dòng)時(shí)對(duì)數(shù)據(jù)庫(kù)進(jìn)行初始化操作,則在application.properties文件中對(duì)數(shù)據(jù)庫(kù)進(jìn)接配置

  • spring.datasource.schema=classpath:db/schema.sql,進(jìn)行該配置后,每次啟動(dòng)程序,程序都會(huì)運(yùn)行resources/db/schema.sql文件,對(duì)數(shù)據(jù)庫(kù)的結(jié)構(gòu)進(jìn)行操作。
  • spring.datasource.data=classpath:db/data.sql,進(jìn)行該配置后,每次啟動(dòng)程序,程序都會(huì)運(yùn)行resources/db/data.sql文件,對(duì)數(shù)據(jù)庫(kù)的數(shù)據(jù)操作。

該配置非常適合開(kāi)發(fā)環(huán)境,我會(huì)把數(shù)據(jù)庫(kù)的結(jié)構(gòu)構(gòu)建sql放在resources/db/schema.sql,數(shù)據(jù)sql放在resources/db/data.sql中。這樣每次運(yùn)行程序我都可以得到一個(gè)新的數(shù)據(jù)庫(kù)。這樣就不需要我每次為了測(cè)試而修改數(shù)據(jù)中的內(nèi)容了。

h2 web consloe配置

h2 web consloe是一個(gè)數(shù)據(jù)庫(kù)GUI管理應(yīng)用,就和phpMyAdmin類似。程序運(yùn)行時(shí),會(huì)自動(dòng)啟動(dòng)h2 web consloe。當(dāng)然你也可以進(jìn)行如下的配置。

  • spring.h2.console.settings.web-allow-others=true,進(jìn)行該配置后,h2 web consloe就可以在遠(yuǎn)程訪問(wèn)了。否則只能在本機(jī)訪問(wèn)。
  • spring.h2.console.path=/h2-console,進(jìn)行該配置,你就可以通過(guò)YOUR_URL/h2-console訪問(wèn)h2 web consloe。YOUR_URL是你程序的訪問(wèn)URl。
  • spring.h2.console.enabled=true,進(jìn)行該配置,程序開(kāi)啟時(shí)就會(huì)啟動(dòng)h2 web consloe。當(dāng)然這是默認(rèn)的,如果你不想在啟動(dòng)程序時(shí)啟動(dòng)h2 web consloe,那么就設(shè)置為false。

UserRepositoryTest

@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class UserRepositoryTest {
 
    @Autowired
    private UserRepository userRepository;
 
    @Test
    public void saveTest() throws Exception {
        User user = new User();
        user.setName("鄭龍飛");
        user.setUrl("http://merryyou.cn");
        User result = userRepository.save(user);
        log.info(result.toString());
        Assert.assertNotNull(user.getId());
    }
 
    @Test
    public void findOneTest() throws Exception{
        User user = userRepository.findOne(1l);
        log.info(user.toString());
        Assert.assertNotNull(user);
        Assert.assertTrue(1l==user.getId());
    }
}

h2 web consloe

代碼下載

從我的 github 中下載,https://github.com/longfeizheng/jpa-example/tree/master/h2-webconsole

到此這篇關(guān)于SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù)的方法的文章就介紹到這了,更多相關(guān)SpringBoot集成H2內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java實(shí)現(xiàn)LRU緩存淘汰算法的方法

    java實(shí)現(xiàn)LRU緩存淘汰算法的方法

    LRU(Least recently used,最近最少使用)算法根據(jù)數(shù)據(jù)的歷史訪問(wèn)記錄來(lái)進(jìn)行淘汰數(shù)據(jù),其核心思想是“如果數(shù)據(jù)最近被訪問(wèn)過(guò),那么將來(lái)被訪問(wèn)的幾率也更高”。下面看下java實(shí)現(xiàn)LRU緩存淘汰算法的方法,一起看看吧
    2021-11-11
  • 使用@Value注入map、List,yaml格式方式

    使用@Value注入map、List,yaml格式方式

    這篇文章主要介紹了使用@Value注入map、List,yaml格式方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Netty分布式NioEventLoop優(yōu)化selector源碼解析

    Netty分布式NioEventLoop優(yōu)化selector源碼解析

    這篇文章主要介紹了Netty分布式NioEventLoop優(yōu)化selector源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • springBoot @Enable* 注解的使用

    springBoot @Enable* 注解的使用

    這篇文章主要介紹了springBoot @Enable* 注解的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • java list去重操作實(shí)現(xiàn)方式

    java list去重操作實(shí)現(xiàn)方式

    Java中的List是可以包含重復(fù)元素的(hash code 和equals),接下來(lái)將介紹兩種方式實(shí)現(xiàn)java list去重操作,感興趣的朋友可以參考下
    2012-12-12
  • JVM常用垃圾收集器及GC算法解讀

    JVM常用垃圾收集器及GC算法解讀

    這篇文章主要介紹了JVM常用垃圾收集器及GC算法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Spring Boot CORS 配置方法允許跨域請(qǐng)求的最佳實(shí)踐方案

    Spring Boot CORS 配置方法允許跨域請(qǐng)求的最佳實(shí)踐方案

    跨域請(qǐng)求在現(xiàn)代Web開(kāi)發(fā)中非常重要,特別是在涉及多個(gè)前端和后端服務(wù)時(shí),本文詳細(xì)介紹了跨域請(qǐng)求的背景、重要性以及如何解決跨域問(wèn)題,通過(guò)SpringBoot框架的CORS配置,可以有效地處理跨域請(qǐng)求,確保數(shù)據(jù)傳輸?shù)陌踩院陀脩趔w驗(yàn),感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • Java中Spring擴(kuò)展點(diǎn)詳解

    Java中Spring擴(kuò)展點(diǎn)詳解

    這篇文章主要介紹了Java中Spring技巧之?dāng)U展點(diǎn)的應(yīng)用,下文Spring容器的啟動(dòng)流程圖展開(kāi)其內(nèi)容的相關(guān)資料,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-06-06
  • SpringBoot?Schedule調(diào)度任務(wù)的動(dòng)態(tài)管理

    SpringBoot?Schedule調(diào)度任務(wù)的動(dòng)態(tài)管理

    Scheduled定時(shí)任務(wù)是Spring?boot自身提供的功能,所以不需要引入Maven依賴包,下面這篇文章主要給大家介紹了關(guān)于SpringBoot通過(guò)@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)以及問(wèn)題解決的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • java線程池的四種創(chuàng)建方式詳細(xì)分析

    java線程池的四種創(chuàng)建方式詳細(xì)分析

    這篇文章主要介紹了java線程池的四種創(chuàng)建方式詳細(xì)分析,連接池是創(chuàng)建和管理一個(gè)連接的緩沖池的技術(shù),這些連接準(zhǔn)備好被任何需要它們的線程使用
    2022-07-07

最新評(píng)論

石台县| 夏津县| 鲁甸县| 卓尼县| 遂川县| 确山县| 河南省| 会同县| 确山县| 资溪县| 安多县| 年辖:市辖区| 宜兴市| 文化| 台中市| 清涧县| 阜平县| 平顶山市| 慈溪市| 陈巴尔虎旗| 讷河市| 仲巴县| 龙江县| 彭州市| 榕江县| 泰来县| 灌云县| 边坝县| 屯昌县| 墨脱县| 义马市| 德江县| 黄山市| 濉溪县| 东乡族自治县| 余江县| 邵武市| 太湖县| 同德县| 十堰市| 永兴县|