导包
<!-- h2数据库-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!--JPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
application.yml配置
spring:
h2:
console:
enabled: true # 是否启用h2控制台
path: /h2-console # 访问的路径
settings:
trace: false # 开启h2 console 跟踪 方便调试 默认 false
web-allow-others: true # 允许console 远程访问 默认false
datasource:
url: jdbc:h2:file:./sddm-pm;MODE=MYSQL
username: user
password: 123456
driver-class-name: org.h2.Driver
jpa:
show-sql: true
# hibernate:
# ddl-auto: update #自动创建表
database-platform: org.hibernate.dialect.PostgreSQLDialect
properties:
hibernate:
format_sql: true
sql:
init:
platform: h2
mvc:
pathmatch:
matching-strategy: ant_path_matcher
使用
在idea上运行了可以直接访问
http://localhost:8080/h2-console/login.jsp
要创建h2数据库也可以
打开
创建h2
创建成功
登陆
然后成功了
表映射
package com.startdima.pm.entity;
import lombok.Data;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import javax.persistence.*;
import java.sql.Timestamp;
/**
* 用于自动创建表,这里用不上
* @author xiejiangcan
* @version 1.0
* @date 2022/8/12
* @Description
*/
@Entity
@Table(name = "t_user")
@Data
public class User {
//用户id 唯一主键
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id",nullable = false,unique = true)
private Integer userId;
//用户名
@Column(name = "user_name",nullable = false,unique = true)
private String userName;
//姓名
@Column(name = "alias")
private String alias;
//密码
@Column(name = "password")
private String password;
//性别 0=女 1=男
@Column(name = "sex")
private Integer sex;
//出生日期
@Column(name = "birthday")
private Timestamp birthday;
//备注说明
@Column(name = "remark")
private String remark;
//是否有效 逻辑删除标志位
@Column(name = "active")
private Integer active;
//乐观锁字段
@Version
@Column(name = "version")
private Long version;
//创建时间
@Column(name = "insert_time")
private Timestamp insertTime;
//更新时间
@Column(name = "update_time")
private Timestamp updateTime;
}