本文主要是介绍基于SpringBoot3和JDK17,集成H2数据库和jpa,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基于SpringBoot3和JDK17,集成H2数据库和jpa
学会用H2数据库,为了快速写出需要处理数据关系的demo。
文章目录
- 基于SpringBoot3和JDK17,集成H2数据库和jpa
- 工程配置
- pom.xml文件
- `application.properties`文件
- 练习H2数据库的操作
- h2数据库的建表
- 自增主键控制
- 练习动态调整日志
- 引入依赖
- 打开管理api(application.properties)
- 查看类的目前的日志级别
- 修改类的日志级别
- 测试类
工程配置
完整demo:https://download.csdn.net/download/weixin_43820556/89385337
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.6</version><relativePath/></parent><groupId>com.donny.demo</groupId><artifactId>h2</artifactId><version>0.0.1-SNAPSHOT</version><name>h2</name><description>Demo project for Spring Boot</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
application.properties
文件
spring.application.name=h2
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.driver-class-name=org.h2.Driver
# 内存模式
#spring.datasource.url=jdbc:h2:mem:test
# 文件持久化模式
spring.datasource.url=jdbc:h2:./db/testdb;AUTO_SERVER=TRUE;
spring.datasource.username=sa
spring.datasource.password=sa
#spring.sql.init.schema-locations=classpath:db/schema.sql
spring.h2.console.path=/h2
spring.h2.console.enabled=true
logging.config=classpath:logback-spring.xml
management.endpoints.web.exposure.include=loggers
练习H2数据库的操作
h2数据库的建表
/*学生*/
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`
(`id` INTEGER auto_increment primary key,`name` varchar(50) not null
);/*老师,一个老师可以交好几个班级*/
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher`
(`id` INTEGER auto_increment primary key,`name` varchar(50) not null,`subject` varchar(50) not null
);/*班级,一个学生只能在一个班级,一个班级有多名老师和学生*/
DROP TABLE IF EXISTS `grade`;
CREATE TABLE `grade`
(`id` INTEGER auto_increment primary key,`student_id` INTEGER,`teacher_id` INTEGER,`name` varchar(50)
);-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student`
VALUES (0, '张三');
INSERT INTO `student`
VALUES (1, '少杰');
INSERT INTO `student`
VALUES (10, '赵子龙');
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `teacher`
VALUES (0, '张老师', '语文');
INSERT INTO `teacher`
VALUES (1, '王老师', '数学');
INSERT INTO `teacher`
VALUES (2, '陈老师', '英语');
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `grade`
VALUES (1, 0, 0, '1班');
INSERT INTO `grade`
VALUES (2, 1, 2, '2班');
INSERT INTO `grade`
VALUES (3, 10, 1, 'Z班');
自增主键控制
如果单独插入了数据,则不会与JPA的数据插入时主键相关联,例如,在建表的同时,写入了id为1的数据,则JPA在新增的时候,也会不会新增id为2的数据,还是新增id为1的数据,此时即报错。本人并未解决这种问题,而是将插入数据的操作,全部交由JPA来做,是实现主键自增的统一,如果有网友可以解决此问题,可以留言。
通过JPA来操作数据库
多对多关系,建议拆解成多个1对多的关系,进行表设计。
通过注解控制外键关系
@OneToOne:双向关系,实体键的关系是一对一,此注解用在单端上
@ManyToOne:双向关系,实体键的关系是多对一,此注解用在单端上
练习动态调整日志
引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
打开管理api(application.properties)
management.endpoints.web.exposure.include=loggers
查看类的目前的日志级别
GET请求:http://127.0.0.1:8080/actuator/loggers/com.donny.demo.h2.controller.LogController
结果
{"effectiveLevel":"DEBUG"}
修改类的日志级别
POST请求:
http://127.0.0.1:8080/actuator/loggers/com.donny.demo.h2.controller.LogController 请求体 {"configuredLevel":"info"}
重新查询
{"configuredLevel":"ERROR","effectiveLevel":"ERROR"}
结果会是在LogController中增加一个配置项。若要取消configuredLevel
,只要将请求体改为 {"configuredLevel":null}
测试类
package com.donny.demo.h2.controller;import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author 1792998761@qq.com* @version 1.0* @since 2024年06月01日 上午11:56*/
@RestController
@RequestMapping("/log")
@Slf4j
public class LogController {@GetMapping("/logger")public String loggerLevel() {log.trace("logger level is trace");log.info("logger level is info");log.debug("logger level is debug");log.warn("logger level is warn");log.error("logger level is error");return "success";}
}
开启日志级别为debug时,输出的是[2024-06-01 15:24:25,180][http-nio-8080-exec-5][ERROR][com.donny.demo.h2.controller.LogController:23] logger level is error
。说明生效了。
这篇关于基于SpringBoot3和JDK17,集成H2数据库和jpa的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!