H2 是一个用 Java 开发的嵌入式数据库,它的主要特性使其成为嵌入式应用程序的理想选择。H2 仅是一个类库,可以直接嵌入到应用项目中,而无需独立安装客户端和服务器端。
常用的开源数据库包括 H2、Derby、HSQLDB、MySQL 和 PostgreSQL。相比之下,H2 和 HSQLDB 非常适合作为嵌入式数据库使用,而其他数据库大多需要安装独立的客户端和服务器端。
特点:
适用场景:
连接方式:
jdbc:h2:~/test
或 jdbc:h2:file:/data/sample
。特点:
适用场景:
连接方式:
jdbc:h2:tcp://localhost/~/test
。java -cp h2*.jar org.h2.tools.Server -tcp -tcpAllowOthers -tcpPort 9092
特点:
适用场景:
连接方式:
jdbc:h2:mem:test
。我这次用的是本地文件模式,数据库仅能由同一应用程序实例访问,所以比较难搞。
官网下载即可:H2 Database Engine
CREATE TABLE students (
roll_num BIGINT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
age INT NOT NULL
);
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.6.2version>
<relativePath/>
parent>
<groupId>com.lmgroupId>
<artifactId>SH2MartifactId>
<version>1.0-SNAPSHOTversion>
<name>SH2Mname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>17java.version>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<version>2.2.224version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.xml.bindgroupId>
<artifactId>jaxb-apiartifactId>
dependency>
dependencies>
<build>
<finalName>SH2MfinalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
<configuration>
<mainClass>com.lm.ApplicationmainClass>
configuration>
plugin>
plugins>
build>
project>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
@Data
@Entity
@Table(name="students")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_seq")
@SequenceGenerator(name = "student_seq", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1)
@Column(name="roll_num")
private Long rollNum;
@Column(name="name")
private String name;
@Column(name="age")
private int age;
//setters and getters
}
import com.lm.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
@Component
public interface StudentRepository extends JpaRepository<Student, Long> {
Student findByName(String name);
}
import com.lm.entity.Student;
import java.util.List;
public interface IStudentService {
List<Student> getAllStudents();
Student getStudentByRollNum(Long rollNum);
Student getStudentByName(String name);
boolean addStudent(Student student);
void updateStudent(Student student);
void deleteStudent(Long rollNum);
}
import com.lm.entity.Student;
import com.lm.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service
public class StudentService implements IStudentService {
@Autowired
private StudentRepository studentRepository;
@Override
public Student getStudentByRollNum(Long rollNum) {
Student obj = studentRepository.findById(rollNum).get();
return obj;
}
@Override
public Student getStudentByName(String name) {
Student byName = studentRepository.findByName(name);
return byName;
}
@Override
public List<Student> getAllStudents() {
List<Student> list = new ArrayList<>();
studentRepository.findAll().forEach(e -> list.add(e));
return list;
}
@Override
public boolean addStudent(Student student) {
studentRepository.save(student);
return true;
}
@Override
public void updateStudent(Student student) {
studentRepository.save(student);
}
@Override
public void deleteStudent(Long rollNum) {
studentRepository.delete(getStudentByRollNum(rollNum));
}
}
# Tomcat
server.port=8081
#Datasource Configuration
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=123456
#JPA Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
#Connection Pool Configuration
spring.datasource.hikari.connection-timeout=20000
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=12
spring.datasource.hikari.idle-timeout=300000
spring.datasource.hikari.max-lifetime=1200000
spring.h2.console.path=/h2
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=true
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:file:./data/demo;AUTO_RECONNECT=TRUE;AUTO_SERVER=FALSE;FILE_LOCK=SOCKET
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
launch4j链接:https://sourceforge.net/projects/launch4j/files/latest/download
jre链接:https://www.alipan.com/s/wCRbfXzSWVM
提取码:6hd1
注意:输出文件位置要写到文件的具体名称如:C:/asd.exe
java download URL: http://java.com/download
这时候你们应该会有这个文件
现在就可以运行了,但是它的运行是不显示的,只在后台运行。
点击exe文件,等待一会儿再访问相应接口
我们需要一个手动关闭的脚本
链接:https://www.alipan.com/s/4ox9JH3gJZH
提取码:00xf
现在点击这个就可以手动关闭。
或者通过接口http://localhost:8081/shutdown也可以进行关闭。