欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)了一些常用的方法:findone,findall,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)文章

  • Springboot實(shí)例講解實(shí)現(xiàn)專業(yè)材料認(rèn)證管理系統(tǒng)流程

    Springboot實(shí)例講解實(shí)現(xiàn)專業(yè)材料認(rèn)證管理系統(tǒng)流程

    這是一個(gè)基于java的畢業(yè)設(shè)計(jì)項(xiàng)目,畢設(shè)課題為springboot框架的知識(shí)產(chǎn)權(quán)服務(wù)平臺(tái)系統(tǒng),是一個(gè)采用b/s結(jié)構(gòu)的javaweb項(xiàng)目,需要的朋友可以參考下
    2022-06-06
  • JDK與JRE的下載和安裝以及配置JDK環(huán)境變量圖文教程

    JDK與JRE的下載和安裝以及配置JDK環(huán)境變量圖文教程

    JRE也就是(Java?RuntimeEnvironment)Java運(yùn)行環(huán)境,是運(yùn)行JAVA程序所必須的環(huán)境的集合,包含各種類庫(kù),下面這篇文章主要給大家介紹了關(guān)于JDK與JRE的下載和安裝以及配置JDK環(huán)境變量的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • 強(qiáng)烈推薦這些提升代碼效率的IDEA使用技巧

    強(qiáng)烈推薦這些提升代碼效率的IDEA使用技巧

    在平常的開(kāi)發(fā)中,發(fā)現(xiàn)一些同事對(duì)Idea 使用的不是很熟練,僅僅用來(lái)編輯,編譯,不能很好的發(fā)揮Idea 的神奇.整理了下我平常用的一些技巧,希望你能從中學(xué)習(xí)到一些.需要的朋友可以參考下
    2021-05-05
  • Springboot整合企業(yè)微信機(jī)器人助手推送消息的實(shí)現(xiàn)

    Springboot整合企業(yè)微信機(jī)器人助手推送消息的實(shí)現(xiàn)

    本文主要介紹了Springboot整合企業(yè)微信機(jī)器人助手推送消息的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Java中JFinal框架動(dòng)態(tài)切換數(shù)據(jù)庫(kù)的方法

    Java中JFinal框架動(dòng)態(tài)切換數(shù)據(jù)庫(kù)的方法

    這篇文章主要介紹了Java中JFinal框架動(dòng)態(tài)切換數(shù)據(jù)庫(kù)的方法,本文通過(guò)兩種方法結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • SpringBoot之通過(guò)BeanPostProcessor動(dòng)態(tài)注入ID生成器案例詳解

    SpringBoot之通過(guò)BeanPostProcessor動(dòng)態(tài)注入ID生成器案例詳解

    這篇文章主要介紹了SpringBoot之通過(guò)BeanPostProcessor動(dòng)態(tài)注入ID生成器案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • SpringBoot 改造成https訪問(wèn)的實(shí)現(xiàn)

    SpringBoot 改造成https訪問(wèn)的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot 改造成https訪問(wèn)的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 使用注解進(jìn)行Spring開(kāi)發(fā)的全過(guò)程

    使用注解進(jìn)行Spring開(kāi)發(fā)的全過(guò)程

    使用注解(Annotation)是一種在代碼級(jí)別進(jìn)行說(shuō)明和標(biāo)記的技術(shù),它從JDK 5.0開(kāi)始引入,并在現(xiàn)代Java開(kāi)發(fā)中得到了廣泛應(yīng)用,本文將詳細(xì)介紹Spring框架中常用的注解及示例,幫助開(kāi)發(fā)者快速掌握Spring注解開(kāi)發(fā)的要點(diǎn)和技巧,需要的朋友可以參考下
    2023-11-11
  • Java動(dòng)態(tài)線程池插件dynamic-tp集成zookeeper

    Java動(dòng)態(tài)線程池插件dynamic-tp集成zookeeper

    ZooKeeper是一個(gè)分布式的,開(kāi)放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個(gè)開(kāi)源的實(shí)現(xiàn),是Hadoop和Hbase的重要組件。它是一個(gè)為分布式應(yīng)用提供一致性的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等
    2023-03-03
  • IDEA 2023創(chuàng)建JSP項(xiàng)目的完整步驟教程

    IDEA 2023創(chuàng)建JSP項(xiàng)目的完整步驟教程

    這篇文章主要介紹了IDEA 2023創(chuàng)建JSP項(xiàng)目的完整步驟教程,創(chuàng)建項(xiàng)目需要經(jīng)過(guò)新建項(xiàng)目、設(shè)置項(xiàng)目名稱和路徑、選擇JDK版本、添加模塊和工件、配置Tomcat服務(wù)器等步驟,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-10-10

最新評(píng)論