简记H2 Database内存数据踩过的坑

开发入门步骤:

一、maven中添加依赖项

 <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>

 

 二、配置文件application.properties

#端口服务配置
server.port = 9098
spring.application.name = member-dao-service

#注册配置
eureka.instance.prefer-ip-address = true
eureka.instance.instance-id = ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
eureka.client.registerWithEureka = true
eureka.client.fetchRegistry = true
eureka.client.serviceUrl.defaultZone = http://registerServer1:9010/eureka/,http://registerServer2:9010/eureka/

#数据源配置
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource


#************H2  Begin****************
#db schema
spring.datasource.schema=classpath:db/schema.sql
#db data
spring.datasource.data=classpath:db/data.sql

#remote visit
spring.h2.console.settings.web-allow-others=true
#console url
spring.h2.console.path=/h2-console
#default true
spring.h2.console.enabled=true
spring.h2.console.settings.trace=true

# url,default :jdbc:h2:mem:testdbsa
spring.datasource.url=jdbc:h2:mem:testdbsa
# driver default:org.h2.Driver
spring.datasource.driver-class-name=org.h2.Driver
# username default sa
spring.datasource.username=sa
# password default null
spring.datasource.password=
# 解决中文乱码 最好留着它
spring.datasource.sql-script-encoding = utf-8



spring.datasource.initialSize = 5
spring.datasource.minIdle = 5
spring.datasource.maxActive = 20
spring.datasource.maxWait = 60000
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.minEvictableIdleTimeMillis = 300000
spring.datasource.validationQuery = SELECT 1
spring.datasource.testWhileIdle = true
spring.datasource.testOnBorrow = false
spring.datasource.testOnReturn = false
spring.datasource.poolPreparedStatements = true
spring.datasource.maxPoolPreparedStatementPerConnectionSize = 20
spring.datasource.filters = stat,wall,log4j
spring.datasource.connectionProperties = druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.servlet.multipart.enabled = true

##日志配置
logging.file = ./log/${spring.application.name}.log
logging.level.root = info

#Mybatis
mybatis.configLocation = classpath:mybatis-config.xml
mybatis.mapperLocations = classpath:mapper/**/*.xml

 

三、在db目录下添加表结构和数据...项目测试结构如下 

注意H2有部分sql函数不支持...自己修改掉 ...可以参考:https://www.jianshu.com/p/4a613dcf182c

四、集成Apollo配置中心注意事项:

在做单元测试时.先要去掉apollo(即去掉META-INF/app.properties中的app,不用去掉@EnableApolloConfig)...
否则是读到apollo的配置信息,而不是本地的配置信息...其他的配置中心也要做类似处理

五、Spring boot使用h2内存数据库,定义好sql脚本后执行中文乱码问题

在配置中指定sql的执行编码,如下:

# 解决中文乱码 最好留着它
spring.datasource.sql-script-encoding = utf-8

 注意Spring boot读取配置的时候.对应的值不会去空格.....故写utf-8时不要写成'utf-8 '这样多出空格.否则springboot会报无法识别.

 

哈哈哈...全是坑.记录一下.以免再犯...

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值