简单使用
pom.xml配置文件
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.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kinda</groupId>
<artifactId>kindaNotify</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kindaNotify</name>
<description>kindaNotify</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建实体类
package com.kinda.kindanotify.pojo.entity;
import jakarta.persistence.*;
import lombok.Builder;
@Entity
@Builder
@Table(name = "event")
public class Event {
private Integer id;
private String title;
private String message;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name = "message")
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Event() {
}
public Event(Integer id, String title, String message) {
this.id = id;
this.title = title;
this.message = message;
}
@Override
public String toString() {
return "Event{" +
"id=" + id +
", title='" + title + '\'' +
", message='" + message + '\'' +
'}';
}
}
创建dao/repository层
package com.kinda.kindanotify.repository;
import com.kinda.kindanotify.pojo.entity.Event;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EventRepo extends JpaRepository<Event, Integer> {
}
测试
package com.kinda.kindanotify;
import com.kinda.kindanotify.pojo.entity.Event;
import com.kinda.kindanotify.repository.EventRepo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class KindaNotifyApplicationTests {
@Autowired
private EventRepo eventRepo;
// @Test
// void contextLoads() {
// System.out.println("初始化数据库表");
// }
// 测试JPA提供的方法
/**
* 测试添加事件
*/
@Test
void testAddEvent(){
Event event = new Event();
event = event.builder().title("测试标题").message("测试信息").build();
System.out.println(event);
eventRepo.save(event);
}
}