java h2 内存模式,h2数据库作为内存型与springboot+mybatis的案例

1)嵌入式模式(使用 JDBC 的本地连接)

2)服务器模式(使用 JDBC 或 ODBC 在 TCP/IP 上的远程连接)

3)混合模式(本地和远程连接同时进行)

该案例为嵌入式模式

242429bb80383daa02b7522cc4a55114.png

二.springboot创建项目

首先,给大家看一下我的项目结构

9454cc8e676cda00f8ed339856796e04.png

springboot的版本为:2.1.9 具体可以看一下我的pom文件

2.1 依赖pom文件

以下就是我该演示项目的所有依赖,h2的版本交给springboot去进行确定

1.8

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.1

com.h2database

h2

runtime

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

src/main/java

**/*.xml

true

2.2 application.yml

需要关注 h2 web操作界面的路径 和 console打印的sql语句,以下的配置是本文精华部分

spring:

h2:

console:

path:/h2-console #进入h2 web操作界面的路径

enabled:true#开启h2 web界面

datasource:

driver-class-name: org.h2.Driver

schema: classpath:db/schema-h2.sql

data: classpath:db/data-h2.sql

url: jdbc:h2:mem:test

username: root

password: test

mybatis:

configuration:

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 在console打印sql语句

server:

port:8081

2.3 sql语句

data-h2.sql,需要放置在resource/db下

INSERT INTO cat (id, name, age, color,score) VALUES(1, "Jone", 18, "黃色",0.4),

(2, "Jack", 20, "白色",0.5),

(3, "Tom", 28, "金色",0.1),

(4, "Sandy", 21, "紅色",0.8),

(5, "Billie", 24, "綠色",0.7);

schema-h2.sql,需要放置在resource/db下

DROP TABLE IF EXISTScat;CREATE TABLEcat

(

idBIGINT(20) NOT NULL COMMENT "主键ID",

nameVARCHAR(30) NULL DEFAULT NULL COMMENT "姓名",

ageINT(11) NULL DEFAULT NULL COMMENT "年龄",

colorVARCHAR(50) NULL DEFAULT NULL COMMENT "颜色",

scoreDOUBLE NULL DEFAULT NULL COMMENT "分数",PRIMARY KEY(id)

);

2.4 启动类上配上mapper扫描

/***@authorCC-CAN*/@SpringBootApplication

@MapperScan("com.springboot.*.mapper")public classMybatisApplication {public static voidmain(String[] args) {

SpringApplication.run(MybatisApplication.class, args);

}

}

2.5 mapper.java和mapper.xml

配置后,由mybatis进行实体类的映射

public interfaceCatMapper {

ListselectAll();

}

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

SELECT * from cat

2.6 案例的entity

与数据库的实体对应

@Datapublic classCat {privateLong id;privateString name;privateInteger age;privateString color;privateDouble score;

}

2.7 service 和 impl

public interfaceCatService {/*** 喵叫

*@return

*/String meow();

Listlist();

}

@Servicepublic class CatServiceImpl implementsCatService {

@AutowiredprivateCatMapper catMapper;

@OverridepublicString meow() {return "瞄";

}

@Overridepublic Listlist() {returncatMapper.selectAll();

}

}

2.8 建立测试类

在test创建 springboot 的测试类

@RunWith(SpringRunner.class)

@SpringBootTestpublic classMybatisApplicationTests {

@AutowiredprivateCatService catService;

@Testpublic voidcontextLoads() {

List list =catService.list();

list.forEach(System.out::println);

}

}

三 测试结果

Creating a newSqlSession

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6caf7803] was not registeredforsynchronization because synchronization is not active

JDBC Connection [HikariProxyConnection@408543908 wrapping conn0: url=jdbc:h2:mem:test user=ROOT] will not be managed by Spring==> Preparing: SELECT *from cat==>Parameters:<==Columns: ID, NAME, AGE, COLOR, SCORE<== Row: 1, Jone, 18, 黃色, 0.4

<== Row: 2, Jack, 20, 白色, 0.5

<== Row: 3, Tom, 28, 金色, 0.1

<== Row: 4, Sandy, 21, 紅色, 0.8

<== Row: 5, Billie, 24, 綠色, 0.7

<== Total: 5Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6caf7803]

Cat(id=1, name=Jone, age=18, color=黃色, score=0.4)

Cat(id=2, name=Jack, age=20, color=白色, score=0.5)

Cat(id=3, name=Tom, age=28, color=金色, score=0.1)

Cat(id=4, name=Sandy, age=21, color=紅色, score=0.8)

Cat(id=5, name=Billie, age=24, color=綠色, score=0.7)

b1cc6289f6343565909bd34b7d075cfa.png

四 打开h2的web浏览页面

运行main方法

f8863a90810977f399707ee9d680cac3.png

浏览器输入

http://localhost:8081/h2-console

63ff930ca57d57120e9b4bfefe3fde67.png

账号 root 密码 test,进去

查看

ae7ee8c9a45a87c9946ef305074e88d7.png

结语

本人长期从事java开发,如果有什么疑问,可以留言,我会及时解答

附录

e800111b55250b772c39f175edaf7a8f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值