h2爲輕量級數據庫,使用特別方便,它能夠不使用數據庫服務器,直接嵌入到java程序中。能夠配置持久化,一樣也能夠不持久化(數據在內存中)進程結束後,數據就釋放,用作測試和演示特別方便。自帶後臺管理,很是方便,開源免費java
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> <version>1.4.193</version><!--低版本,支持訪問內存數據庫--> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> </dependencies>
server: port: 8080 spring: datasource: driver-class-name: org.h2.Driver # schema: classpath:db/schema-h2.sql #初始化建表 # data: classpath:db/data-h2.sql #初始化數據 # url: jdbc:h2:mem:test #不持久化,放在內存中 url: jdbc:h2:~/test username: root password: test h2: console: path: /h2-console enabled: true #必須配置,否則沒法訪問
配置中提供了初始化數據庫語句schema: classpath:db/schema-h2.sql
git
配置中提供數據初始化語句data: classpath:db/data-h2.sql
web
固然你也能夠不初始化數據和表,在程序啓動後,能夠經過 localhost:8080/h2-console訪問數據庫管理後臺。經過後臺操做h2數據庫spring
持久化與否url: jdbc:h2:mem:test
表明數據放置於內存中,這種適合作單元測試,一次性使用
url: jdbc:h2:~/test
表明數據存放於 家目錄/test
中sql
啓動Springboot應用類,訪問http://localhost:8080/h2-console
就可使用數據庫管理後臺了
數據庫
完整代碼參考:https://gitee.com/haimama/java-study/tree/master/h2db-demo-simpleapache
package demosimple.h2; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("demosimple.h2.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
package demosimple.h2.controller; import demosimple.h2.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired UserMapper userMapper; @GetMapping("/test") public Object test(){ return userMapper.getById(1L); } }
package demosimple.h2.mapper; import demosimple.h2.pojo.User; import org.apache.ibatis.annotations.Select; public interface UserMapper { @Select("select * from user where id=#{id}") public User getById(Long id); }
package demosimple.h2.pojo; import lombok.Data; @Data public class User { private Long id; private String name; private Integer age; private String email; }
訪問http://localhost:8080/test
返回結果{"id":1,"name":"Jone","age":18,"email":"test1@baomidou.com"}
服務器
控制檯默認連接是jdbc:h2:~/test
,若是咱們使用的是內存jdbc:h2:mem:test
,須要將連接改成jdbc:h2:mem:test
mybatis
當咱們使用jdbc:h2:mem:test
連接時,報以下錯誤app
Database "mem:test" not found, and IFEXISTS=true, so we cant auto-create it [90146-199] 90146/90146 (Help)
這句話的意思是說數據庫未找到。經查詢,高版本的h2再也不容許遠程訪問內存數據庫,能夠將maven依賴添加一個低版本的
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> <version>1.4.193</version> <!--低版本,支持訪問內存數據庫--> </dependency>
url: jdbc:h2:~/test
,當應用再次啓動時,初始化的sql不會再執行,而且操做後新增減的數據狀態將一直保存url: jdbc:h2:mem:test
,每次啓動時,數據都會從新初始化