SpringBoot+Spring Data JPA整合H2数据库的示例代码
作者:花伤情犹在 发布时间:2024-01-28 19:58:37
前言
H2数据库是一个开源的关系型数据库。H2采用java语言编写,不受平台的限制,同时支持网络版和嵌入式版本,有比较好的兼容性,支持相当标准的sql标准
提供JDBC、ODBC访问接口,提供了非常友好的基于web的数据库管理界面
官网:http://www.h2database.com/
Maven依赖
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--h2-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
Conroller
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@RequestMapping("/list")
public List<User> findAll(){
List<User> userList = userRepository.findAll();
return userList;
}
@RequestMapping("/save")
public String save(User user){
userRepository.save(user);
return "保存成功";
@RequestMapping("/update")
public String update(User user){
return "更新成功";
@RequestMapping("/delete")
public String delete(Integer id){
userRepository.deleteById(id);
return "删除成功";
}
实体类
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Data
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
private Integer gender;
}
Repository
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
数据库脚本文件
架构 (DDL) 脚本资源引用schema.sql
drop table if exists user;
create table user(
`id` int primary key auto_increment,
`name` varchar(255) not null,
`age` int not null,
`gender` int not null
);
数据 (DML) 脚本资源引用
insert into user (id,name,age,gender) values (null, '张三',18,1);
insert into user (id,name,age,gender) values (null, '李四',19,1);
insert into user (id,name,age,gender) values (null, '王五',20,1);
insert into user (id,name,age,gender) values (null, '李六',21,1);
配置文件
#---------服务器配置-----------
server.port=8080
#---------数据源配置-----------
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:./data;AUTO_SERVER=TRUE
spring.datasource.username=sa
spring.datasource.password=
#架构 (DDL) 脚本资源引用
spring.datasource.schema=classpath:db/schema.sql
#数据 (DML) 脚本资源引用
spring.datasource.data=classpath:db/data.sql
#SQL脚本编码
spring.datasource.sql-script-encoding=UTF-8
#初始化模式
spring.datasource.initialization-mode=ALWAYS
#如果在初始化数据库时发生错误,是否停止
spring.datasource.continue-on-error=true
#---------JPA配置-------------
#要操作的目标数据库
spring.jpa.database=h2
#控制台显示SQL语句
spring.jpa.show-sql=true
#更新或者创建数据表结构
spring.jpa.hibernate.ddl-auto=update
#物理命名策略的完全限定名称
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
#是否在启动时初始化架构
spring.jpa.generate-ddl=true
#----------H2配置--------------
#http://localhost:8080/h2-console
spring.h2.console.path=/h2-console
#启用控制台
spring.h2.console.enabled=true
启动项目
访问H2数据库
访问:http://localhost:8080/h2-console
查看全部数据
由于设置了数据库脚本,所以SpringBoot项目每次启动都会运行一遍sql文件
#架构 (DDL) 脚本资源引用
spring.datasource.schema=classpath:db/schema.sql
#数据 (DML) 脚本资源引用
spring.datasource.data=classpath:db/data.sql
#SQL脚本编码
spring.datasource.sql-script-encoding=UTF-8
#初始化模式
spring.datasource.initialization-mode=ALWAYS
#如果在初始化数据库时发生错误,是否停止
spring.datasource.continue-on-error=true
H2数据库文件
数据库文件位置通过spring.datasource.url
来指定
spring.datasource.url=jdbc:h2:file:./data;AUTO_SERVER=TRUE
运行方式
1.在内存中运行
数据库只在内存中运行,关闭连接后数据库将被清空,适合测试环境
连接字符串:
jdbc:h2:mem:DBName;DB_CLOSE_DELAY=-1
2.嵌入式
数据库持久化存储为单个文件
连接字符串:
jdbc:h2:file:~/.h2/DBName;AUTO_SERVER=TRUE
3.服务模式
H2支持三种服务模式:
web server:此种运行方式支持使用浏览器访问H2 Console
TCP server:支持客户端/服务器端的连接方式
PG server:支持PostgreSQL客户端
启动tcp服务连接字符串示例:
jdbc:h2:tcp://localhost/~/test 使用用户主目录
jdbc:h2:tcp://localhost//data/test 使用绝对路径
4.连接字符串参数
DB_CLOSE_DELAY
:要求最后一个正在连接的连接断开后,不要关闭数据库MODE=MySQL
:兼容模式,H2兼容多种数据库,该值可以为:DB2、Derby、HSQLDB、MSSQLServer、MySQL、Oracle、PostgreSQLAUTO_RECONNECT=TRUE
:连接丢失后自动重新连接AUTO_SERVER=TRUE
:启动自动混合模式,允许开启多个连接,该参数不支持在内存中运行模式TRACE_LEVEL_SYSTEM_OUT、TRACE_LEVEL_FILE
:输出跟踪日志到控制台或文件, 取值0为OFF,1为ERROR(默认值),2为INFO,3为DEBUGSET TRACE_MAX_FILE_SIZE mb
:设置跟踪日志文件的大小,默认为16M
来源:https://blog.csdn.net/qq_31762741/article/details/122967384
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 继续练手,根据之前获取汽油价格的方式获取了金价,暂时没钱投资,看看而已#!/usr/bin/env python# -*- coding:
- 一、pyqt5中动画的继承关系图二、关于QAbstractAnimation父类的认识1、主要作用继承此类, 实现一些自定义动画所有动画共享
- git和svn有什么区别呢?git采用分布式版本库管理,而svn采用集中式版本库管理。集中式版本库管理需要有一台存放版本库的服务器,开发人员
- Memoization 是一种将函数返回值缓存起来的方法,在 Lisp, Ruby, Perl, Python 等语言中使用非常广泛。随着
- 页面中无法看见页面,指向的连接网页无法显示 解决方法:1、首先在Dreamweaver中不能中文作为文件名。连目录名也最好是英文的。2、如果
- python中有指定的代码进行输入操作,所以今天就由小编来为大家介绍python怎么实现键盘输入。第一首先打开电脑的python编辑工具。再
- 过滤器模板层对变量的操作实际还有很多,过滤器就是其中一种。学过Linux系统的一定知道管道操作符,其可以将上一步输出直接作为下一步输入进行处
- 本文实例讲述了ASP.NET数据库操作类。分享给大家供大家参考,具体如下:using System;using System.Data;us
- status = show status like ‘%%' [例:show status like 'Com_select
- 一、Hive介绍hive: 由 Facebook 开源用于解决海量结构化日志的数据统计工具。Hive 是基于 Hadoop 的一个数据仓库工
- 作为酷爱编程的老程序员,实在按耐不下这个冲动,Python真的是太火了,不断撩拨我的心。我是对Python存有戒备之心的,想当年我基于Dru
- 今天我升级MYSQL到5.1的时候遇到的。写出来共享以下。1、[root@localhost mysql]# scripts/mysql_i
- 数据库优化有很多可以讲,按照支撑的数据量来分可以分为两个阶段:单机数据库和分库分表,前者一般可以支撑500W或者10G以内的数据,超过这个值
- 前言随着网站的内容的增多和用户访问量的增多,网站加载会越来越慢,受限于带宽和服务器同一时间的请求次数的限制,,我们往往需要在此时对我们的网站
- 索引与切片在Tensorflow中使用的频率极其高,可以用来提取部分数据。1.索引在 TensorFlow 中,支持基本的[𝑖][𝑗]…标准
- win10系统本地安装MySQL8.0.20,亲测可用,也是参考了其他大神的操作1. 下载Mysql ,官网下载地址:MySQL官网:链接直
- 智能合约1. 是什么智能合约是一种由计算机程序编写的自动化合约,它可以在没有第三方干预的情况下执行交易和契约条款。智能合约使用区块链技术实现
- functools模块是Python的标准库的一部分,它是为高阶函数而实现的。高阶函数是作用于或返回另一个函数或多个函数的函数。一般来说,对
- #!/bin/sh#code by scpman#功能:检查并修复mysql数据库表#将此脚本加到定时中,脚本执行时,等会读库,列出要修复的
- 本文实例讲述了python进程池实现的多进程文件夹copy器。分享给大家供大家参考,具体如下:应用:文件夹copy器(多进程版)import