zoukankan      html  css  js  c++  java
  • Spring

    1. 概述

    1. 在 Spring 开发中, 引入 H2 做辅助测试数据库

    2. 场景

    1. 复习 Spring, 复习到 持久化 部分

      1. 需要一个 数据库 来做测试
    2. 方案

      1. 方案1: 搭建 MySQL 实例

        1. 虽然现在有 docker 环境, 但我还是懒得弄
          1. 真的懒...
      2. 方案2: 使用 H2 数据库

        1. 使用 embedded 版本
          1. 可以使用 内存数据库
          2. 随用随启
          3. 数据每次都是新的
        2. 嗯, 决定就是它了
    3. 思路

      1. H2 的使用, 也特别简单
        1. maven 添加好依赖
        2. 简单配置
        3. 启动项目
        4. 验证启动即可

    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. 引入依赖

    1. 概述

      1. 在 pom 文件里引入依赖
    2. 代码如下

      <!-- JdbcTemplate -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>
      
      <!-- h2 -->
      <dependency>
          <groupId>com.h2database</groupId>
          <artifactId>h2</artifactId>
          <scope>runtime</scope>
      </dependency>
      
    3. 解释

      1. 引入 JdbcTemplate
        1. 与 H2 交互的时候, 真的需要驱动
      2. 引入 H2
        1. 使用数据库
    4. 注意

      1. 引入后一定要确保 lib 下下来
        1. 我没有确认, 然后傻等了好久
        2. 都开始找 bug 了

    2. 配置

    1. 概述

      1. H2 数据库的启动, 打开 web console
        1. 因为启动在 spring 的同个 jvm 里, 不太好确认
          1. 可以通过 run > endpoint > health 下看到 有个 db
    2. 配置文件

      # dataSource: H2
      ## H2: 开启 console
      ### 开启 console
      spring.h2.console.enabled=true
      ### 设定 console 路径
      spring.h2.console.path=/console
      

    3. 验证

    1. 重启项目

    2. 打开浏览器, 进入 console

      # 如果能打开, 说明启动成功了
      localhost:8080/console
      

    ps

    1. ref

      1. Spring Boot – Spring Data JPA with Hibernate and H2 Web Console
        1. 这里用的是 jpa 配合 hibernate 的方案, 可以看看
    2. 问题

      1. 我们要测试的数据, 如何写入
      2. 我们写入后, 如何通过这个 console 查看
      3. 这些问题, 我会在后面的博客里继续说明
    尽量尝试解释清楚; 自己校对能力有限, 如果有错误欢迎指出
  • 相关阅读:
    文件上传漏洞之js验证
    文件上传漏洞靶机upload-labs(1到10)
    URI/URL/URN都是什么
    解压jdk报错gzip: stdin: not in gzip format
    burpsuite常见问题
    C/C++字符串反转的N种方法
    转 二叉树之Java实现二叉树基本操作
    MySQL 面试基础
    转 MySQL中的行级锁,表级锁,页级锁
    MySQL问题排查工具介绍
  • 原文地址:https://www.cnblogs.com/xy14/p/11759240.html
Copyright © 2011-2022 走看看