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

Spring boot2.x中集成H2數(shù)據(jù)庫(kù)代碼實(shí)例

 更新時(shí)間:2019年12月03日 10:40:47   作者:慕塵  
這篇文章主要介紹了Spring boot2.x中集成H2數(shù)據(jù)庫(kù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Spring boot2.x中集成H2數(shù)據(jù)庫(kù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

在spring boot中集成

1.添加依賴

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

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
</dependency>

2.添加H2相關(guān)配置,修改application.properties文件

spring.jpa.database=h2
spring.jpa.show-sql=true
#ddl執(zhí)行方式,update create 等

spring.datasource.url=jdbc:h2:./data/test;AUTO_SERVER=TRUE
spring.jpa.hibernate.ddl-auto=update
spring.datasource.username=sa
spring.datasource.password=123456
spring.datasource.driverClassName=org.h2.Driver

spring.h2.console.path=/h2-console
spring.h2.console.enabled=true

說(shuō)明:

spring.datasource.url

數(shù)據(jù)庫(kù)文件

(1)內(nèi)存數(shù)據(jù)庫(kù)

jdbc:h2:mem:DBName

內(nèi)存數(shù)據(jù)庫(kù)的數(shù)據(jù)存在內(nèi)存中,當(dāng)程序停止時(shí),不會(huì)被保存會(huì)丟失

eg:

spring.datasource.url=jdbc:h2:mem:test

(2)文件數(shù)據(jù)庫(kù)

jdbc:h2:file:{FilePath} 也可以簡(jiǎn)化為 jdbc:h2:{FilePath}

FilePath的格式

  • a) ./{path}/{fileName} 在當(dāng)前程序的根目錄下創(chuàng)建目錄和數(shù)據(jù)庫(kù)文件
  • b) ~/{path}/{fileName} 在當(dāng)前用戶的根目錄下創(chuàng)建目錄和數(shù)據(jù)庫(kù)文件
  • c) C:/{path}/{fileName} 在指定盤(pán)符的指定目錄下創(chuàng)建數(shù)據(jù)庫(kù)文件

(3)遠(yuǎn)程數(shù)據(jù)庫(kù)

jdbc:h2:tcp://<{IP|hostname}>[:{Port}]/[]<{dbName}>

附加參數(shù):

  • AUTO_SERVER=TRUE 啟動(dòng)自動(dòng)混合模式,允許開(kāi)啟多個(gè)連接,該參數(shù)不支持在內(nèi)存中運(yùn)行模式
  • DB_CLOSE_ON_EXIT=FALSE,當(dāng)虛擬機(jī)退出時(shí)并不關(guān)閉數(shù)據(jù)庫(kù)

3.代碼

domain層,即User類(entity)

package com.example.demo.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(name = "user")
@Data
public class User {
  @Id
  @GeneratedValue(strategy= GenerationType.AUTO)
  private int id;
  private String name;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

dao層,即UserRepository 接口

package com.example.demo.dao;

import com.example.demo.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
  List<User> getUsersByName(String Name);
}

controller層,即Demo

package com.example.demo.controller;

import com.example.demo.dao.UserRepository;
import com.example.demo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class Demo {
  @Autowired
  private UserRepository repo;

  @RequestMapping("find")
  public List<User> find() {
    return (List<User>) repo.findAll();
  }
}

編寫(xiě)DemoApplication

package com.example.demo;

import com.example.demo.dao.UserRepository;
import com.example.demo.domain.User;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

  @Bean
  InitializingBean saveData(UserRepository repo){

    return ()->{
      User u = new User();
      u.setName("abc");
      repo.save(u);
      User u1 = new User();
      u1.setName("zyx");
      repo.save(u1);

    };
  }

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

}

啟動(dòng)項(xiàng)目,打開(kāi)瀏覽器訪問(wèn)http://localhost:8080/find

訪問(wèn)http://localhost:8080/h2-console/

連接上后查詢數(shù)據(jù)

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

相關(guān)文章

  • springboot啟動(dòng)過(guò)程中常用的回調(diào)示例詳解

    springboot啟動(dòng)過(guò)程中常用的回調(diào)示例詳解

    springboot提供非常豐富回調(diào)接口,利用這些接口可以做非常多的事情,本文通過(guò)實(shí)例代碼給大家介紹springboot啟動(dòng)過(guò)程中常用的回調(diào)知識(shí)感興趣的朋友跟隨小編一起看看吧
    2022-01-01
  • SpringBoot EasyPoi動(dòng)態(tài)導(dǎo)入導(dǎo)出的兩種方式實(shí)現(xiàn)方法詳解

    SpringBoot EasyPoi動(dòng)態(tài)導(dǎo)入導(dǎo)出的兩種方式實(shí)現(xiàn)方法詳解

    項(xiàng)目里使用的是EasyPoi來(lái)處理導(dǎo)入導(dǎo)出功能的。近日因業(yè)務(wù)需求調(diào)整,一些導(dǎo)出功能的導(dǎo)出列需要根據(jù)不同的條件動(dòng)態(tài)導(dǎo)出
    2022-09-09
  • SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù)

    SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù)

    本文主要介紹了SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Spring用AspectJ開(kāi)發(fā)AOP(基于Annotation)

    Spring用AspectJ開(kāi)發(fā)AOP(基于Annotation)

    這篇文章主要介紹了Spring用AspectJ開(kāi)發(fā)AOP(基于Annotation),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 你真的會(huì)使用Java的方法引用嗎

    你真的會(huì)使用Java的方法引用嗎

    這篇文章主要給大家介紹了關(guān)于Java方法引用的相關(guān)資料,方法引用是Java8的新特性,方法引用其實(shí)也離不開(kāi)Lambda表達(dá)式,本文通過(guò)示例代碼介紹的很詳細(xì),需要的朋友可以參考下
    2021-08-08
  • Mybatis-Plus3.2.0 MetaObjectHandler 無(wú)法進(jìn)行公共字段全局填充

    Mybatis-Plus3.2.0 MetaObjectHandler 無(wú)法進(jìn)行公共字段全局填充

    這篇文章主要介紹了Mybatis-Plus3.2.0 MetaObjectHandler 無(wú)法進(jìn)行公共字段全局填充,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • SpringBoot集成Quartz實(shí)現(xiàn)持久化定時(shí)接口調(diào)用任務(wù)

    SpringBoot集成Quartz實(shí)現(xiàn)持久化定時(shí)接口調(diào)用任務(wù)

    Quartz是功能強(qiáng)大的開(kāi)源作業(yè)調(diào)度庫(kù),幾乎可以集成到任何?Java?應(yīng)用程序中,從最小的獨(dú)立應(yīng)用程序到最大的電子商務(wù)系統(tǒng),本文將通過(guò)代碼示例給大家介紹SpringBoot集成Quartz實(shí)現(xiàn)持久化定時(shí)接口調(diào)用任務(wù),需要的朋友可以參考下
    2023-07-07
  • Java實(shí)現(xiàn)經(jīng)典俄羅斯方塊游戲

    Java實(shí)現(xiàn)經(jīng)典俄羅斯方塊游戲

    俄羅斯方塊是一個(gè)最初由阿列克謝帕吉特諾夫在蘇聯(lián)設(shè)計(jì)和編程的益智類視頻游戲。本文將利用Java實(shí)現(xiàn)這一經(jīng)典的小游戲,需要的可以參考一下
    2022-01-01
  • 詳解MyBatis中column屬性的總結(jié)

    詳解MyBatis中column屬性的總結(jié)

    在MyBatis的映射中有column這么一個(gè)屬性,我一直以為它映射的是數(shù)據(jù)庫(kù)表中的列名,但經(jīng)過(guò)學(xué)習(xí)發(fā)現(xiàn)他似乎映射的是SQL語(yǔ)句中的列名,或者說(shuō)是查詢結(jié)果所得到的表的列名,這篇文章主要介紹了MyBatis中column屬性的總結(jié),需要的朋友可以參考下
    2022-09-09
  • Spring?@Cacheable讀取配置常量方式

    Spring?@Cacheable讀取配置常量方式

    這篇文章主要介紹了Spring?@Cacheable讀取配置常量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論

南京市| 海兴县| 宿州市| 宿州市| 酉阳| 太保市| 加查县| 舞钢市| 唐海县| 云梦县| 于田县| 河北省| 正镶白旗| 丰原市| 枣阳市| 芦山县| 灵台县| 高密市| 集贤县| 金门县| 股票| 清徐县| 肥乡县| 临安市| 南靖县| 商河县| 卫辉市| 临沂市| 清丰县| 车险| 台山市| 峨眉山市| 疏勒县| 霞浦县| 乐至县| 铜鼓县| 柯坪县| 绍兴市| 汉源县| 隆子县| 卓资县|