H2
H2是一个开源的嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一
个十分方便的web控制台用于操作和管理数据库内容。H2还提供兼容模式,可以兼容一些主
流的数据库,具有比较完备的数据库特性,如支client/server连接,能够支持标准的SQL语
句,支持存储过程等。因此采用H2作为开发期、测试期和演示的数据库非常方便,不太适合
作为大规模生产数据库。
特点
好了这边直接开始
这边采用Springboot + h2+mybatis-plus +swagger来实现这个例子
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.2
com.zkb
h2-test
0.0.1-SNAPSHOT
h2-test
h2-test
1.8
3.3.2
1.5.22
2.9.2
1.9.1
1.2.47
org.springframework.boot
spring-boot-starter-web
com.h2database
h2
runtime
org.projectlombok
lombok
true
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus.version}
io.springfox
springfox-swagger2
${springfox.version}
io.swagger
swagger-annotations
io.swagger
swagger-models
io.swagger
swagger-annotations
${swagger-ui.version}
io.swagger
swagger-models
${swagger-ui.version}
io.springfox
springfox-swagger-ui
${springfox.version}
com.github.xiaoymin
swagger-bootstrap-ui
${swagger-bootstrap-ui.version}
com.alibaba
fastjson
${fastjson.version}
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
其实与我们使用mysql数据库类似,只不过这边会自带一个h2 server
package com.zkb.conf;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.Arrays;
import java.util.List;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi1() {
return new Docket(DocumentationType.SWAGGER_2).enable(true).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.apis(RequestHandlerSelectors.basePackage("com.zkb.controller"))
.paths(PathSelectors.any()).build().securitySchemes(apiKeyList()).groupName("系统接口");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("系统接口文档")
.description("这是系统接口文档说明")
.contact(new Contact("h2", "", ""))
.version("1.0")
.build();
}
private List apiKeyList() {
return Arrays.asList(new ApiKey("登录token", "token", In.HEADER.name()),
new ApiKey("设备类型(android,ios,pc)---必填", "deviceType", In.HEADER.name()));
}
}
package com.zkb.controller;
import com.zkb.entity.Student;
import com.zkb.mapper.StudentMapper;
import com.zkb.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("/test")
@RestController
@Api(value = "Student", tags = "Student")
public class TestController {
@Autowired
StudentService studentService;
@GetMapping("list")
@ApiOperation(value = "获取列表")
public List getList(){
return studentService.list();
}
}
package com.zkb.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("student")
public class Student extends Model {
private Integer id;
private String name;
private Integer age;
}
package com.zkb.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zkb.entity.Student;
public interface StudentMapper extends BaseMapper {
}
package com.zkb.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkb.entity.Student;
import com.zkb.mapper.StudentMapper;
import com.zkb.service.StudentService;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl extends ServiceImpl implements StudentService {
}
package com.zkb.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zkb.entity.Student;
public interface StudentService extends IService {
}
package com.zkb;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@MapperScan("com.zkb.mapper")
@EnableSwagger2
public class H2TestApplication {
public static void main(String[] args) {
SpringApplication.run(H2TestApplication.class, args);
}
}
data.sql
INSERT INTO student VALUES (1,'张三',10);
INSERT INTO student VALUES (2,'李四',16);
schema.sql
CREATE TABLE student(
id int not null,
name varchar(20) null,
age int null
);
server:
port: 8888
spring:
datasource:
driverClassName: org.h2.Driver
password: 123456
# url: jdbc:h2:E:/h2/testdb;MODE=MYSQL
url: jdbc:h2:mem:mybatis
username: root
h2:
console:
enabled: true
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
sql:
init:
schema-locations: classpath:db/schema.sql
data-locations: classpath:db/data.sql
logging:
level:
com:
fs: debug
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapping/*.xml
global-config:
# 逻辑删除配置
db-config:
# 删除前
logic-not-delete-value: 0
# 删除后
logic-delete-value: 1
看你自己的实际情况选择连接方式 该数据库支持多种连接方式和连接设置。 这是使用不同的数据库 URL 实现的。 URL 中的设置不区分大小写。
以上就是全部代码了,结构什么的,按照第一个图来就好
好了run一下看看
可以看到db/文件夹下的两个sql会直接执行
sql:
init:
schema-locations: classpath:db/schema.sql
data-locations: classpath:db/data.sql
这里可以对应的初始化sql文件
这里可以看到已经能正常使用H2数据库了,
demo下载地址h2嵌入式数据库例子springboot+h2+mybatisplus+swagger使用例子-Java文档类资源-CSDN下载