#-----------h2数据库配置 http://localhost:8080/h2-console
#---控制台配置
spring.h2.console.path=/h2-console
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
#-----驱动配置 druid https://github.com/alibaba/druid
spring.datasource.url = jdbc:h2:file:F:/ideaPro/demo1/src/main/resources/db/testdb;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE
#spring.datasource.url = jdbc:h2:file:~/.h2/testdb
spring.datasource.username = sa
spring.datasource.password = sa
spring.datasource.driverClassName = org.h2.Driver
# 配置初始化大小、最小、最大
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=5
# 配置获取连接等待超时的时间 毫秒
spring.datasource.druid.max-wait=60000
spring.datasource.druid.validation-query=SELECT 1
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.test-while-idle=true
######Druid监控配置######
#下面配置说明请参考Druid Github Wiki,配置_配置WebStatFilter
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
#下面配置说明请参考Druid Github Wiki,配置_StatViewServlet配置
spring.datasource.druid.stat-view-servlet.loginUsername=druid
spring.datasource.druid.stat-view-servlet.loginPassword=druid
#mapper文件扫描
mybatis.mapperLocations = classpath*:/mapper/*.xml
pom依赖
<!-- 内存数据库h2-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot mapper文件扫描支持-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
然后就可以直接启动你的项目了
h2数据库有提供web页面,浏览器输入:http://localhost:8080/h2-console
h2-console 是 spring.h2.console.path 属性
druid也提供有数据库监控的web页面,浏览器输入: http://localhost:8080/druid
帐号密码 :spring.datasource.druid.stat-view-servlet.loginUsername=druid
spring.datasource.druid.stat-view-servlet.loginPassword=druid
当然有不习惯h2的也可以改成mysql的
#mysql jdbc spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://你的IP:3306/数据库名?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&rewriteBatchedStatements=TRUE&zeroDateTimeBehavior=convertToNull
zeroDateTimeBehavior=convertToNull: 没有赋值的时间类型 转换为空 如果没有这个属性jpa做数据添加时可能会报错哦
pom.xml 把 h2 的换成mysql的驱动
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
哦, #mapper文件扫描
mybatis.mapperLocations = classpath*:/mapper/*.xml
扫描二维码关注公众号,回复:
12380664 查看本文章
![](/qrcode.jpg)
这个是扫描mapper文件的 mapper 是要在 resources 下创建的
最后有需要购买服务器玩的,请戳→
https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=tqpvz8yl
领取优惠券.