Apache Derby 是100% Java 编写的内存数据库,属于 Apache 的一个开源项目。并且是一个容易管理的关系数据库管理系统
Apache Derby 是一个与平台无关的数据库引擎,它以 Java 类库的形式对外提供服务。与其他难以部署的数据库不同, Derby 数据库体积小、安装非常简单
1.程序小巧,基础引擎和内嵌的JDBC驱动总共大约2MB。
2.基于Java、JDBC和SQL标准。
3.提供内嵌的JDBC驱动,你可把Derby嵌入到基于Java的应用程序中。
4.支持客户端/服务器模式。
5.安装、布置和使用简单。
1. 嵌入式模式。Derby 在应用程序的 JVM中运行。在这种模式下,只有应用程序可以访问数据库,例如另一个用户/应用程序将无法访问数据库。
2. 服务器模式。如果 Derby 在服务器模式下运行,负责处理数据库请求的 Derby 网络服务器,可以把它当成类似mysql等的server服务器,可以供多个客户端应用程序访问
好了,我们直接例一个嵌入式模式下的例子吧,这个单独作为sql 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
{ - void createStudentTable();
- }
- 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 { - @Override
- public void createStudentTable() {
- baseMapper.createStudentTable();
- }
- }
- package com.zkb.service;
-
- import com.baomidou.mybatisplus.extension.service.IService;
- import com.zkb.entity.Student;
-
- public interface StudentService extends IService
{ - void createStudentTable();
- }
- package com.zkb;
-
- import com.zkb.entity.Student;
- import com.zkb.service.StudentService;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
-
- import javax.annotation.PostConstruct;
- import java.util.ArrayList;
- import java.util.List;
-
- @SpringBootApplication
- @MapperScan("com.zkb.mapper")
- @EnableSwagger2
- public class DerbyTestApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(DerbyTestApplication.class, args);
- }
-
- @Autowired
- StudentService studentService;
-
- @PostConstruct
- public void init(){
- studentService.createStudentTable();
-
- List
list= new ArrayList<>(); -
- Student student = new Student();
- student.setId(1);
- student.setName("张三");
- student.setAge(20);
-
- Student student1 = new Student();
- student1.setId(2);
- student1.setName("李四");
- student1.setAge(21);
-
- list.add(student);
- list.add(student1);
- studentService.saveBatch(list);
- }
-
- }
- "1.0" encoding="UTF-8"?>
- "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.zkb.mapper.StudentMapper"> -
-
-
"BaseResultMap" type="com.zkb.entity.Student"> -
"id" property="id" /> -
"name" property="name" /> -
"age" property="age" /> -
-
-
-
"createStudentTable"> - CREATE TABLE student(
- id int not null,
- name varchar(20),
- age int
- )
-
-
- server:
- port: 8888
- dbBaseDir: /tmp/derby
- spring:
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: org.apache.derby.jdbc.EmbeddedDriver
- url: jdbc:derby:${dbBaseDir}/MyDB;create=true
- username: root
- password: 123456
- jackson:
- date-format: yyyy-MM-dd HH:mm:ss
- time-zone: GMT+8
- 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
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
4.0.0 -
-
org.springframework.boot -
spring-boot-starter-parent -
2.5.2 -
-
-
com.zkb -
derby-test -
0.0.1-SNAPSHOT -
derby-test -
derby-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 -
-
-
-
-
org.apache.derby -
derby -
10.13.1.1 -
-
-
-
com.alibaba -
druid -
1.0.31 -
-
-
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 -
-
-
-
-
-
-

运行demo后会产生一个MyDB的文件夹 以上便是该文件夹里面的内容
demo里面我写了一个初始化的方法,第二次运行要把对应的内容注释掉,不然会报表已存在错误


可以看到,我已经查从derby数据库查出数据了,到这里demo就结束了
demo地址:https://download.csdn.net/download/qq_14926283/86543026