准备开发一个小应用程序,数据要能保存起来,数据量不大,所以不想使用常用的mysql/oracle数据库,毕竟要搭建mysql/oracle数据库感觉挺麻烦的,就想到了用内存数据库derby,迁移部署的时候方便,只需把derby指定的库文件夹复制过去,修改下配置文件的路径就行了。但之前没搞过springboot+mybatis+derby,网上找了下资料,折腾了两小时,简单的增删改查跑通了,记录一下。
在之前折腾过springboot+mybatis+mysql(看之前的文章),想着只是把mysql替换为derby而已,所以直接在之前的基础上修改。
JDK使用1.8。derby的10.13.1.1版本支持分页查询了.
项目文件结构
pom.xml文件
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fei</groupId>
<artifactId>springboot-mybatis-derby</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 依赖仓库 设置从aliyun仓库下载-->
<repositories>
<repository>
<id>alimaven</id>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<!-- 插件依赖仓库 -->
<pluginRepositories>
<pluginRepository>
<id>alimaven</id>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
<properties>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<!-- <version>10.13.1.1</version> -->
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!-- <version>3.1</version> -->
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
springboot的1.5.2.RELEASE版本下,默认使用的derby是10.13.1.1。为了便于直观操作derby,可以在官网上下载derby,使用里面的ij工具。官网下载地址http://db.apache.org/derby/derby_downloads.html,选择10.13.1.1版本下载
解压后,bin文件夹里有ij.bat工具
配置环境变量:
DERBY_HOME:D:\db-derby-10.13.1.1-bin
path:加上%DERBY_HOME%\bin
classpath:加上%DERBY_HOME%\lib\derby.jar;%DERBY_HOME%\lib\derbytools.jar
有整个demo代码都放在github上了,这里就不全部一一贴出来了,只贴一部分。
application.yml
logging:
config: classpath:logback.xml
path: d:/logs
server:
port: 80
session-timeout: 60
dbBaseDir: e:/derbyData #数据库的根路径
mybatis:
mapperLocations: classpath:/com/fei/dao/*.xml
typeAliasesPackage: com.fei.dao
mapperScanPackage: com.fei.dao
configLocation: classpath:/mybatis-config.xml
spring:
datasource:
name: fabric
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:derby:${dbBaseDir}/fabric;create=true
username: root
password: root
driver-class-name: org.apache.derby.jdbc.EmbeddedDriver
minIdle: 5
maxActive: 100
initialSize: 10
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1 from sysibm.sysdummy1
testWhileIdle: true