zoukankan      html  css  js  c++  java
  • Spring

    1. 概述

    1. 之前讲到了 H2 的引入
    2. 这下我想说说 H2 启动时的 数据导入

    2. 场景

    1. 需求
      1. 启动项目后, H2 启动起来
      2. 环境数据会自动注入 H2 数据库
      3. 可以验证是否成功

    3. 环境

    1. os

      1. win10
    2. jdk

      1. 1.8
    3. ide

      1. ida 2018.1
    4. spring

      1. spring boot
        1. 2.1.7 release
      2. 组件
        1. thymeleaf
        2. starter-web
        3. devtool
        4. starter-test
    5. browser

      1. firefox
        1. 70.0
    6. H2

      1. 1.4.197
    7. ref

      1. spring in action 5th

    4. 步骤

    1. 准备好 sql 文件

    1. schema.sql

      create table if not exists Ingredient (
        id varchar(4) not null,
        name varchar(25) not null,
        type varchar(10) not null
      );
      
    2. data.sql

      delete from Ingredient;
      insert into Ingredient (id, name, type)
                      values ('FLTO', 'Flour Tortilla', 'WRAP');
      

    2. 配置

    1. 概述

      1. 配置 H2 相关的 sql 配置
    2. 前提

      1. 上一篇博文写到的配置, 已经配好了
      2. 这次只给出 增量
      3. application.properties
    3. 内容

      # H2: 写入数据
      spring.datasource.url=jdbc:h2:mem:test_db
      spring.datasource.driverClassName=org.h2.Driver
      spring.datasource.username=sa
      spring.datasource.password=sa
      #spring.datasource.schema=classpath:schema.sql
      #spring.datasource.data=classpath:data.sql
      
    4. 解释

      1. url

        1. 内存数据库
        2. 最后的 库名, 可以自己起
          1. 老实说, 我也没弄清这块的规则, 但是现在能用
      2. driverClassName

        1. 驱动类
      3. username & password

        1. 默认值
      4. schema & data

        1. 启动时默认执行的 sql 文件
        2. 默认位置
          1. resources 目录下
          2. 这个 path 可以改, 文件名也可以改
        3. 默认顺序
          1. schema
          2. data

    3. 验证

    1. 概述
      1. 配置好后, 验证一下即可

    1. 步骤

    1. 重启项目

    2. 进入 浏览器 console

      1. url

        localhost:8080/console
        
    3. 登录

      1. jdbc url

        # 和 之前的配置一样就行
        jdbc:h2:mem:test_db
        
      2. username & password

        1. sa/sa
    4. 验证

      1. 确认表在
      2. 查看数据是否一致

    ps

    1. ref

      1. SpringBoot整合系列-整合H2
    2. 其他

      1. 暴露了 sql 只是的贫乏
      2. H2 总算起来了, 可以继续往下走了
    尽量尝试解释清楚; 自己校对能力有限, 如果有错误欢迎指出
  • 相关阅读:
    day_2:re
    day_1:Requests
    CTF-PHP漏洞总结(持续更新)
    BugkuCTF-Web- flag在index里 80
    中缀表达式转换为后缀表达式(C语言实现)
    逆波兰计算器1.0 (c语言 栈实现)支持小数计算
    二进制数转化为十进制数(栈的学习练习)
    数据结构:统计学生信息(c++动态链表完成)
    数据结构:删除数组中的元素(c++)链表形式
    约瑟夫环 (c++循环链表方法书写)
  • 原文地址:https://www.cnblogs.com/xy14/p/11760228.html
Copyright © 2011-2022 走看看