Loading...
Image

springboot嵌入式数据库derby初探

首页 / 新闻资讯 / 正文

Apache Derby 是100% Java 编写的内存数据库,属于 Apache 的一个开源项目。并且是一个容易管理的关系数据库管理系统
Apache Derby 是一个与平台无关的数据库引擎,它以 Java 类库的形式对外提供服务。与其他难以部署的数据库不同, Derby 数据库体积小、安装非常简单

主要特点

1.程序小巧,基础引擎和内嵌的JDBC驱动总共大约2MB。

2.基于Java、JDBC和SQL标准。

3.提供内嵌的JDBC驱动,你可把Derby嵌入到基于Java的应用程序中。

4.支持客户端/服务器模式。

5.安装、布置和使用简单。

Derby 数据库的两种运行模式

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<ApiKey> 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<Student> 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<Student> {     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<Student> {     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<StudentMapper, Student> 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<Student> {     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<Student> 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);     }  } 
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zkb.mapper.StudentMapper">      <!-- 通用查询映射结果 -->     <resultMap id="BaseResultMap" type="com.zkb.entity.Student">         <id column="id" property="id" />         <result column="name" property="name" />         <result column="age" property="age" />     </resultMap>       <update id="createStudentTable">         CREATE TABLE student(             id int not null,             name varchar(20),             age int         )     </update>  </mapper> 
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 
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.5.2</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>com.zkb</groupId>     <artifactId>derby-test</artifactId>     <version>0.0.1-SNAPSHOT</version>     <name>derby-test</name>     <description>derby-test</description>     <properties>         <java.version>1.8</java.version>         <mybatis-plus.version>3.3.2</mybatis-plus.version>         <swagger-ui.version>1.5.22</swagger-ui.version>         <springfox.version>2.9.2</springfox.version>         <swagger-bootstrap-ui.version>1.9.1</swagger-bootstrap-ui.version>         <fastjson.version>1.2.47</fastjson.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>          <!--derby内嵌数据库依赖-->         <dependency>             <groupId>org.apache.derby</groupId>             <artifactId>derby</artifactId>             <version>10.13.1.1</version>         </dependency>         <!--druid数据源依赖-->         <dependency>             <groupId>com.alibaba</groupId>             <artifactId>druid</artifactId>             <version>1.0.31</version>         </dependency>         <dependency>             <groupId>org.projectlombok</groupId>             <artifactId>lombok</artifactId>             <optional>true</optional>         </dependency>         <dependency>             <groupId>com.baomidou</groupId>             <artifactId>mybatis-plus-boot-starter</artifactId>             <version>${mybatis-plus.version}</version>         </dependency>           <dependency>             <groupId>io.springfox</groupId>             <artifactId>springfox-swagger2</artifactId>             <version>${springfox.version}</version>             <exclusions>                 <exclusion>                     <groupId>io.swagger</groupId>                     <artifactId>swagger-annotations</artifactId>                 </exclusion>                 <exclusion>                     <groupId>io.swagger</groupId>                     <artifactId>swagger-models</artifactId>                 </exclusion>             </exclusions>         </dependency>         <dependency>             <groupId>io.swagger</groupId>             <artifactId>swagger-annotations</artifactId>             <version>${swagger-ui.version}</version>         </dependency>          <dependency>             <groupId>io.swagger</groupId>             <artifactId>swagger-models</artifactId>             <version>${swagger-ui.version}</version>         </dependency>           <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->         <dependency>             <groupId>io.springfox</groupId>             <artifactId>springfox-swagger-ui</artifactId>             <version>${springfox.version}</version>         </dependency>          <dependency>             <groupId>com.github.xiaoymin</groupId>             <artifactId>swagger-bootstrap-ui</artifactId>             <version>${swagger-bootstrap-ui.version}</version>         </dependency>         <dependency>             <groupId>com.alibaba</groupId>             <artifactId>fastjson</artifactId>             <version>${fastjson.version}</version>         </dependency>       </dependencies>      <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>                 <configuration>                     <excludes>                         <exclude>                             <groupId>org.projectlombok</groupId>                             <artifactId>lombok</artifactId>                         </exclude>                     </excludes>                 </configuration>             </plugin>         </plugins>     </build>  </project> 

运行demo后会产生一个MyDB的文件夹  以上便是该文件夹里面的内容

 demo里面我写了一个初始化的方法,第二次运行要把对应的内容注释掉,不然会报表已存在错误

 

 可以看到,我已经查从derby数据库查出数据了,到这里demo就结束了

demo地址:https://download.csdn.net/download/qq_14926283/86543026