• springboot嵌入式数据库H2初探


    H2 

    H2是一个开源的嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一

    个十分方便的web控制台用于操作和管理数据库内容。H2还提供兼容模式,可以兼容一些主

    流的数据库,具有比较完备的数据库特性,如支client/server连接,能够支持标准的SQL语

    句,支持存储过程等。因此采用H2作为开发期、测试期和演示的数据库非常方便,不太适合

    作为大规模生产数据库。

    特点

    • 非常快,开源,JDBC API
    • 嵌入式和服务器模式;基于磁盘或内存中的数据库
    • 事务支持,多版本并发
    • 基于浏览器的控制台应用程序
    • 加密数据库
    • 全文搜索
    • 占用空间小的纯 Java:大约 2.5 MB 的 jar 文件大小
    • ODBC 驱动程序

     好了这边直接开始

       

    这边采用Springboot + h2+mybatis-plus +swagger来实现这个例子

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. 4.0.0
    5. org.springframework.boot
    6. spring-boot-starter-parent
    7. 2.5.2
    8. com.zkb
    9. h2-test
    10. 0.0.1-SNAPSHOT
    11. h2-test
    12. h2-test
    13. 1.8
    14. 3.3.2
    15. 1.5.22
    16. 2.9.2
    17. 1.9.1
    18. 1.2.47
    19. org.springframework.boot
    20. spring-boot-starter-web
    21. com.h2database
    22. h2
    23. runtime
    24. org.projectlombok
    25. lombok
    26. true
    27. com.baomidou
    28. mybatis-plus-boot-starter
    29. ${mybatis-plus.version}
    30. io.springfox
    31. springfox-swagger2
    32. ${springfox.version}
    33. io.swagger
    34. swagger-annotations
    35. io.swagger
    36. swagger-models
    37. io.swagger
    38. swagger-annotations
    39. ${swagger-ui.version}
    40. io.swagger
    41. swagger-models
    42. ${swagger-ui.version}
    43. io.springfox
    44. springfox-swagger-ui
    45. ${springfox.version}
    46. com.github.xiaoymin
    47. swagger-bootstrap-ui
    48. ${swagger-bootstrap-ui.version}
    49. com.alibaba
    50. fastjson
    51. ${fastjson.version}
    52. org.springframework.boot
    53. spring-boot-maven-plugin
    54. org.projectlombok
    55. lombok

    其实与我们使用mysql数据库类似,只不过这边会自带一个h2 server

    1. package com.zkb.conf;
    2. import io.swagger.annotations.ApiOperation;
    3. import io.swagger.models.auth.In;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. import springfox.documentation.builders.ApiInfoBuilder;
    7. import springfox.documentation.builders.PathSelectors;
    8. import springfox.documentation.builders.RequestHandlerSelectors;
    9. import springfox.documentation.service.ApiInfo;
    10. import springfox.documentation.service.ApiKey;
    11. import springfox.documentation.service.Contact;
    12. import springfox.documentation.spi.DocumentationType;
    13. import springfox.documentation.spring.web.plugins.Docket;
    14. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    15. import java.util.Arrays;
    16. import java.util.List;
    17. @Configuration
    18. @EnableSwagger2
    19. public class SwaggerConfig {
    20. @Bean
    21. public Docket createRestApi1() {
    22. return new Docket(DocumentationType.SWAGGER_2).enable(true).apiInfo(apiInfo()).select()
    23. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
    24. .apis(RequestHandlerSelectors.basePackage("com.zkb.controller"))
    25. .paths(PathSelectors.any()).build().securitySchemes(apiKeyList()).groupName("系统接口");
    26. }
    27. private ApiInfo apiInfo() {
    28. return new ApiInfoBuilder()
    29. .title("系统接口文档")
    30. .description("这是系统接口文档说明")
    31. .contact(new Contact("h2", "", ""))
    32. .version("1.0")
    33. .build();
    34. }
    35. private List apiKeyList() {
    36. return Arrays.asList(new ApiKey("登录token", "token", In.HEADER.name()),
    37. new ApiKey("设备类型(android,ios,pc)---必填", "deviceType", In.HEADER.name()));
    38. }
    39. }
    1. package com.zkb.controller;
    2. import com.zkb.entity.Student;
    3. import com.zkb.mapper.StudentMapper;
    4. import com.zkb.service.StudentService;
    5. import io.swagger.annotations.Api;
    6. import io.swagger.annotations.ApiOperation;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.web.bind.annotation.GetMapping;
    9. import org.springframework.web.bind.annotation.RequestMapping;
    10. import org.springframework.web.bind.annotation.RestController;
    11. import java.util.List;
    12. @RequestMapping("/test")
    13. @RestController
    14. @Api(value = "Student", tags = "Student")
    15. public class TestController {
    16. @Autowired
    17. StudentService studentService;
    18. @GetMapping("list")
    19. @ApiOperation(value = "获取列表")
    20. public List getList(){
    21. return studentService.list();
    22. }
    23. }
    1. package com.zkb.entity;
    2. import com.baomidou.mybatisplus.annotation.TableName;
    3. import com.baomidou.mybatisplus.extension.activerecord.Model;
    4. import lombok.Data;
    5. import lombok.EqualsAndHashCode;
    6. @Data
    7. @EqualsAndHashCode(callSuper = false)
    8. @TableName("student")
    9. public class Student extends Model {
    10. private Integer id;
    11. private String name;
    12. private Integer age;
    13. }
    1. package com.zkb.mapper;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import com.zkb.entity.Student;
    4. public interface StudentMapper extends BaseMapper {
    5. }
    1. package com.zkb.service.impl;
    2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    3. import com.zkb.entity.Student;
    4. import com.zkb.mapper.StudentMapper;
    5. import com.zkb.service.StudentService;
    6. import org.springframework.stereotype.Service;
    7. @Service
    8. public class StudentServiceImpl extends ServiceImpl implements StudentService {
    9. }
    1. package com.zkb.service;
    2. import com.baomidou.mybatisplus.extension.service.IService;
    3. import com.zkb.entity.Student;
    4. public interface StudentService extends IService {
    5. }
    1. package com.zkb;
    2. import org.mybatis.spring.annotation.MapperScan;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    6. @SpringBootApplication
    7. @MapperScan("com.zkb.mapper")
    8. @EnableSwagger2
    9. public class H2TestApplication {
    10. public static void main(String[] args) {
    11. SpringApplication.run(H2TestApplication.class, args);
    12. }
    13. }

     data.sql

    1. INSERT INTO student VALUES (1,'张三',10);
    2. INSERT INTO student VALUES (2,'李四',16);

    schema.sql

    1. CREATE TABLE student(
    2. id int not null,
    3. name varchar(20) null,
    4. age int null
    5. );
    1. "1.0" encoding="UTF-8"?>
    2. "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. "com.zkb.mapper.StudentMapper">
    4. "BaseResultMap" type="com.zkb.entity.Student">
    5. "id" property="id" />
    6. "name" property="name" />
    7. "age" property="age" />
    1. server:
    2. port: 8888
    3. spring:
    4. datasource:
    5. driverClassName: org.h2.Driver
    6. password: 123456
    7. # url: jdbc:h2:E:/h2/testdb;MODE=MYSQL
    8. url: jdbc:h2:mem:mybatis
    9. username: root
    10. h2:
    11. console:
    12. enabled: true
    13. jackson:
    14. date-format: yyyy-MM-dd HH:mm:ss
    15. time-zone: GMT+8
    16. sql:
    17. init:
    18. schema-locations: classpath:db/schema.sql
    19. data-locations: classpath:db/data.sql
    20. logging:
    21. level:
    22. com:
    23. fs: debug
    24. mybatis-plus:
    25. configuration:
    26. map-underscore-to-camel-case: true
    27. auto-mapping-behavior: full
    28. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    29. mapper-locations: classpath:mapping/*.xml
    30. global-config:
    31. # 逻辑删除配置
    32. db-config:
    33. # 删除前
    34. logic-not-delete-value: 0
    35. # 删除后
    36. logic-delete-value: 1

     看你自己的实际情况选择连接方式 该数据库支持多种连接方式和连接设置。 这是使用不同的数据库 URL 实现的。 URL 中的设置不区分大小写。

    以上就是全部代码了,结构什么的,按照第一个图来就好

    好了run一下看看

     

    可以看到db/文件夹下的两个sql会直接执行

    1. sql:
    2. init:
    3. schema-locations: classpath:db/schema.sql
    4. data-locations: classpath:db/data.sql

     这里可以对应的初始化sql文件

     

     这里可以看到已经能正常使用H2数据库了,

    demo下载地址h2嵌入式数据库例子springboot+h2+mybatisplus+swagger使用例子-Java文档类资源-CSDN下载

  • 相关阅读:
    String 地址引用问题
    Spring MVC 入门指南
    MongoDB聚合运算符:$sampleRate
    分享:选择一颗晶振,怎么看晶振的主要参数?
    JAVA学习笔记
    如何评估大语言模型
    【12. 文件系统管理】
    ES6 - 剩余参数,Array的扩展方法,String的扩展方法
    序设计·RB(AT&T汇编)_笔记_第10章:处理字符串
    java计算机毕业设计智能推荐的医药知识推广平台统源码+数据库+系统+lw文档
  • 原文地址:https://blog.csdn.net/qq_14926283/article/details/126906262