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

h2database在springboot中的使用教程

 更新時(shí)間:2020年10月14日 08:30:48   作者:夢(mèng)想家haima  
這篇文章主要介紹了h2database在springboot中的使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

h2為輕量級(jí)數(shù)據(jù)庫(kù),使用特別方便,它可以不使用數(shù)據(jù)庫(kù)服務(wù)器,直接嵌入到j(luò)ava程序中??梢耘渲贸志没瑯右部梢圆怀志没〝?shù)據(jù)在內(nèi)存中)進(jìn)程結(jié)束后,數(shù)據(jù)就釋放,用做測(cè)試和演示特別方便。自帶后臺(tái)管理,非常方便,開(kāi)源免費(fèi)

  • 類(lèi)庫(kù),使用maven簡(jiǎn)易安裝
  • 可以同應(yīng)用程序打包在一起發(fā)布
  • 可持久化,也可以直接基于內(nèi)存不保留數(shù)據(jù),適合于做單元測(cè)試

maven依賴(lài)

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

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

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

    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>runtime</scope>
      <version>1.4.193</version><!--低版本,支持訪問(wèn)內(nèi)存數(shù)據(jù)庫(kù)-->
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.1</version>
    </dependency>
</dependencies>

application.yml配置

server:
 port: 8080

spring:
 datasource:
  driver-class-name: org.h2.Driver
#  schema: classpath:db/schema-h2.sql #初始化建表
#  data: classpath:db/data-h2.sql #初始化數(shù)據(jù)
#  url: jdbc:h2:mem:test  #不持久化,放在內(nèi)存中
  url: jdbc:h2:~/test
  username: root
  password: test
 h2:
  console:
   path: /h2-console
   enabled: true #必須配置,不然無(wú)法訪問(wèn)
  • 配置中提供了初始化數(shù)據(jù)庫(kù)語(yǔ)句schema: classpath:db/schema-h2.sql
  • 配置中提供數(shù)據(jù)初始化語(yǔ)句data: classpath:db/data-h2.sql
  • 當(dāng)然你也可以不初始化數(shù)據(jù)和表,在程序啟動(dòng)后,可以通過(guò) localhost:8080/h2-console訪問(wèn)數(shù)據(jù)庫(kù)管理后臺(tái)。通過(guò)后臺(tái)操作h2數(shù)據(jù)庫(kù)
  • 持久化與否url: jdbc:h2:mem:test代表數(shù)據(jù)放置于內(nèi)存中,這種適合做單元測(cè)試,一次性使用
  • url: jdbc:h2:~/test 代表數(shù)據(jù)存放于 家目錄/test

啟動(dòng)Springboot應(yīng)用類(lèi),訪問(wèn)http://localhost:8080/h2-console就可以使用數(shù)據(jù)庫(kù)管理后臺(tái)了

測(cè)試查詢(xún)功能

完整代碼參考:https://gitee.com/haimama/java-study/tree/master/h2db-demo-simple

Application.java

package demosimple.h2;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("demosimple.h2.mapper")
public class Application {

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

TestController.java

package demosimple.h2.controller;

import demosimple.h2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

  @Autowired
  UserMapper userMapper;

  @GetMapping("/test")
  public Object test(){
    return userMapper.getById(1L);
  }
}

UserMapper.java

package demosimple.h2.mapper;

import demosimple.h2.pojo.User;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {
  @Select("select * from user where id=#{id}")
  public User getById(Long id);
}

User.java

package demosimple.h2.pojo;

import lombok.Data;

@Data
public class User {
  private Long id;
  private String name;
  private Integer age;
  private String email;
}

訪問(wèn)http://localhost:8080/test

返回結(jié)果{"id":1,"name":"Jone","age":18,"email":"test1@baomidou.com"}

問(wèn)題收集

jdbc鏈接

控制臺(tái)默認(rèn)鏈接是jdbc:h2:~/test,如果我們使用的是內(nèi)存jdbc:h2:mem:test,需要將鏈接改為jdbc:h2:mem:test

內(nèi)存鏈接報(bào)錯(cuò)

當(dāng)我們使用jdbc:h2:mem:test鏈接時(shí),報(bào)如下錯(cuò)誤

Database "mem:test" not found, and IFEXISTS=true, so we cant auto-create it [90146-199] 90146/90146 (Help)

這句話(huà)的意思是說(shuō)數(shù)據(jù)庫(kù)未找到。經(jīng)查詢(xún),高版本的h2不再允許遠(yuǎn)程訪問(wèn)內(nèi)存數(shù)據(jù)庫(kù),可以將maven依賴(lài)添加一個(gè)低版本的

 <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
    <version>1.4.193</version> <!--低版本,支持訪問(wèn)內(nèi)存數(shù)據(jù)庫(kù)-->
  </dependency>

初始化sql執(zhí)行

  • 如果持久化到文件,也就是url: jdbc:h2:~/test,當(dāng)應(yīng)用再次啟動(dòng)時(shí),初始化的sql不會(huì)再執(zhí)行,并且操作后新增減的數(shù)據(jù)狀態(tài)將一直保存
  • 如果數(shù)據(jù)庫(kù)選擇的是url: jdbc:h2:mem:test,每次啟動(dòng)時(shí),數(shù)據(jù)都會(huì)重新初始化
  • 這里再補(bǔ)充一點(diǎn)兒前提,只有maven配置了 mybatis-spring-boot-starter 時(shí),初始化的sql才會(huì)執(zhí)行

到此這篇關(guān)于h2database在springboot中的使用教程的文章就介紹到這了,更多相關(guān)springboot使用h2database內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java實(shí)現(xiàn)微信公眾號(hào)掃一掃

    java實(shí)現(xiàn)微信公眾號(hào)掃一掃

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微信公眾號(hào)掃一掃,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • SpringBoot?Test的webEnvironment源碼解讀

    SpringBoot?Test的webEnvironment源碼解讀

    這篇文章主要為大家介紹了SpringBoot?Test的webEnvironment源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 利用Java理解sql的語(yǔ)法(實(shí)例講解)

    利用Java理解sql的語(yǔ)法(實(shí)例講解)

    下面小編就為大家分享一篇利用Java理解sql的語(yǔ)法(實(shí)例講解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • Spring Boot簡(jiǎn)介與快速搭建詳細(xì)步驟

    Spring Boot簡(jiǎn)介與快速搭建詳細(xì)步驟

    SpringBoot其本身沒(méi)有添加什么新的技術(shù),就是整合了一些現(xiàn)有的框架,并提供了一些默認(rèn)的配置,就是這些默認(rèn)的配置,極大的提高了我們的開(kāi)發(fā)效率。這篇文章主要介紹了Spring Boot簡(jiǎn)介與快速搭建,需要的朋友可以參考下
    2021-05-05
  • RocketMQ?Namesrv架構(gòu)工作原理詳解

    RocketMQ?Namesrv架構(gòu)工作原理詳解

    這篇文章主要為大家介紹了RocketMQ?Namesrv架構(gòu)工作原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • springboot如何通過(guò)URL方式訪問(wèn)外部資源

    springboot如何通過(guò)URL方式訪問(wèn)外部資源

    這篇文章主要介紹了springboot如何通過(guò)URL方式訪問(wèn)外部資源,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 詳解Java的繼承

    詳解Java的繼承

    大家好,本篇文章主要講的是詳解Java的繼承,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽
    2022-01-01
  • mybatis判斷int是否為空的時(shí)候,需要注意的3點(diǎn)

    mybatis判斷int是否為空的時(shí)候,需要注意的3點(diǎn)

    這篇文章主要介紹了mybatis判斷int是否為空的時(shí)候,需要注意的3點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • springMVC不掃描controller中的方法問(wèn)題

    springMVC不掃描controller中的方法問(wèn)題

    這篇文章主要介紹了springMVC不掃描controller中的方法問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java反射機(jī)制詳解

    Java反射機(jī)制詳解

    Java的反射機(jī)制是在運(yùn)行狀態(tài)中,對(duì)于任何一個(gè)類(lèi),都可以知道這個(gè)類(lèi)的所有屬性和方法,對(duì)于任何一個(gè)對(duì)象,都可以調(diào)用它所有的方法和屬性,修改部分類(lèi)型信息,這種動(dòng)態(tài)獲取信息以及動(dòng)態(tài)調(diào)用對(duì)象方法的功能稱(chēng)為Java的反射機(jī)制
    2022-09-09

最新評(píng)論

麦盖提县| 从化市| 时尚| 大厂| 罗甸县| 康定县| 黔西县| 五指山市| 常德市| 杂多县| 文昌市| 贵定县| 东丰县| 丰县| 内乡县| 凤庆县| 紫金县| 鞍山市| 建水县| 淳安县| 视频| 图们市| 遵义县| 宁南县| 乐东| 永靖县| 普兰店市| 鹿邑县| 吉木乃县| 青川县| 栖霞市| 谷城县| 贵州省| 全州县| 芜湖市| 茶陵县| 西和县| 景谷| 上高县| 洱源县| 盐亭县|