基于SpringBoot3和JDK17,集成H2数据库和jpa
基于SpringBoot3和JDK17集成H2数据库和jpa学会用H2数据库为了快速写出需要处理数据关系的demo。文章目录基于SpringBoot3和JDK17集成H2数据库和jpa工程配置pom.xml文件application.properties文件练习H2数据库的操作h2数据库的建表自增主键控制练习动态调整日志引入依赖打开管理apiapplication.properties查看类的目前的日志级别修改类的日志级别测试类工程配置完整demohttps://download.csdn.net/download/weixin_43820556/89385337pom.xml文件?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.2.6/versionrelativePath//parentgroupIdcom.donny.demo/groupIdartifactIdh2/artifactIdversion0.0.1-SNAPSHOT/versionnameh2/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version17/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdcom.h2database/groupIdartifactIdh2/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/projectapplication.properties文件spring.application.nameh2 spring.jpa.show-sqltrue spring.jpa.hibernate.ddl-autoupdate spring.jpa.database-platformorg.hibernate.dialect.H2Dialect spring.datasource.driver-class-nameorg.h2.Driver# 内存模式#spring.datasource.urljdbc:h2:mem:test# 文件持久化模式spring.datasource.urljdbc:h2:./db/testdb;AUTO_SERVERTRUE;spring.datasource.usernamesaspring.datasource.passwordsa#spring.sql.init.schema-locationsclasspath:db/schema.sqlspring.h2.console.path/h2spring.h2.console.enabledtruelogging.configclasspath:logback-spring.xmlmanagement.endpoints.web.exposure.includeloggers练习H2数据库的操作h2数据库的建表/*学生*/DROPTABLEIFEXISTSstudent;CREATETABLEstudent(idINTEGERauto_incrementprimarykey,namevarchar(50)notnull);/*老师,一个老师可以交好几个班级*/DROPTABLEIFEXISTSteacher;CREATETABLEteacher(idINTEGERauto_incrementprimarykey,namevarchar(50)notnull,subjectvarchar(50)notnull);/*班级一个学生只能在一个班级一个班级有多名老师和学生*/DROPTABLEIFEXISTSgrade;CREATETABLEgrade(idINTEGERauto_incrementprimarykey,student_idINTEGER,teacher_idINTEGER,namevarchar(50));-- ------------------------------ Records of student-- ----------------------------INSERTINTOstudentVALUES(0,张三);INSERTINTOstudentVALUES(1,少杰);INSERTINTOstudentVALUES(10,赵子龙);-- ------------------------------ Records of student-- ----------------------------INSERTINTOteacherVALUES(0,张老师,语文);INSERTINTOteacherVALUES(1,王老师,数学);INSERTINTOteacherVALUES(2,陈老师,英语);-- ------------------------------ Records of student-- ----------------------------INSERTINTOgradeVALUES(1,0,0,1班);INSERTINTOgradeVALUES(2,1,2,2班);INSERTINTOgradeVALUES(3,10,1,Z班);自增主键控制如果单独插入了数据则不会与JPA的数据插入时主键相关联例如在建表的同时写入了id为1的数据则JPA在新增的时候也会不会新增id为2的数据还是新增id为1的数据此时即报错。本人并未解决这种问题而是将插入数据的操作全部交由JPA来做是实现主键自增的统一如果有网友可以解决此问题可以留言。通过JPA来操作数据库多对多关系建议拆解成多个1对多的关系进行表设计。通过注解控制外键关系OneToOne双向关系实体键的关系是一对一此注解用在单端上 ManyToOne双向关系实体键的关系是多对一此注解用在单端上练习动态调整日志引入依赖dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency打开管理apiapplication.propertiesmanagement.endpoints.web.exposure.includeloggers查看类的目前的日志级别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}测试类packagecom.donny.demo.h2.controller;importlombok.extern.slf4j.Slf4j;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;/** * author 1792998761qq.com * version 1.0 */RestControllerRequestMapping(/log)Slf4jpublicclassLogController{GetMapping(/logger)publicStringloggerLevel(){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);returnsuccess;}}开启日志级别为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。说明生效了。