SpingBoot3整合Hibernate
环境:JDK版本21
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.0.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xw</groupId>
<artifactId>01-provider-8081</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>01-provider-8081</name>
<description>01-provider-8081</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<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>
yml配置
server:
port: 8081
spring:
jpa:
# 指定是否在spring容器启动的时候自动创建表,默认是false
generate-ddl: true
# 是否打印sql 默认是false
show-sql: true
# 指定应用重启不更新表
hibernate:
ddl-auto: none
# 数据源
datasource:
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://192.168.233.133:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
username: root
password: password
# 日志
logging:
pattern:
console: level-%level %msg%n
level:
# 控制springboot启动时显示的日志级别
root: info
# 控制hibernate运行时显示的日志级别
org.hibernate: info
# 控制自己的代码运行是显示的日志级别
com.xw: debug
# 在show-sql为true时,显示SQL中的动态参数值
org.hibernate.type.descriptor.sql.BasicBinder: trace
# 在show-sql为true时,显示查询结果
org.hibernate.type.descriptor.sql.BasicExtractor: trace
实体类
@Data
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer","handler","fieldHandler"})
public class Depart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 主键自动递增
private Integer id;
private String name;
}
repository
package com.xw.repository;
import com.xw.bean.Depart;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DepartRepository extends JpaRepository<Depart, Integer> {
}
接口
package com.xw.controller;
import com.xw.bean.Depart;
import com.xw.service.IDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequestMapping("/provider/depart")
@RestController
public class DepartController {
@Autowired
private IDepartService departService;
@PostMapping("/save")
public boolean saveDepart(@RequestBody Depart depart) {
return departService.saveDepart(depart);
}
@DeleteMapping("/del/{id}")
public boolean removeDepartById(@PathVariable("id") Integer id) {
return departService.removeDepartById(id);
}
@PutMapping("/update")
public boolean update(@RequestBody Depart depart) {
return departService.modifyDepart(depart);
}
@GetMapping("/get/{id}")
public Depart getDepartById(@PathVariable("id") Integer id) {
return departService.getDepartById(id);
}
@GetMapping("/list")
public List<Depart> listAllDeparts() {
return departService.listAllDeparts();
}
}