H2 的使用

阅读更多

 

1.配置application.yml

datasource:
  url: jdbc:h2:mem:console
  username: sa
  password:
  driver-class-name: org.h2.Driver
  schema: classpath:schema.sql
  data: classpath:data.sql

jpa:
  show-sql: false
  hibernate:
    ddl-auto: create
  database: h2

h2:
  console:
    enabled: true
    path: /h2

 

2.pom.xml 配置


   org.springframework.boot
   spring-boot-starter-data-jpa


   mysql
   mysql-connector-java
   runtime


   com.h2database
   h2
   runtime


   org.springframework.boot
   spring-boot-autoconfigure


   org.projectlombok
   lombok

 

3.schema.sql

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',
`name` varchar(64) NOT NULL DEFAULT '0' COMMENT '用户名',
`password` varchar(64) NOT NULL DEFAULT '' COMMENT '密码',
`create_time` datetime NOT NULL COMMENT '创建时间'
)ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

 

4.data.sql

INSERT INTO user(`id`,`name`,`password`,`create_time`) values (1,'root','root','2018-10-16 16:20:20');

 

5. User.java

@Entity
@Table(name = "user")
@Data
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String password;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Timestamp createTime;
}

 

6.UserRepository.java

@Repository
public interface UserRepository extends JpaRepository, Long> {
}

 

7.service 和controller 略

 

8. main

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
public class AdministrativeConsoleApplication extends VcreditApplication {

    public static void main(String[] args) {
        run(AdministrativeConsoleApplication.class, args);
}
}

  启动项目后会自动创建库和表。

 

 

 

你可能感兴趣的:(H2 的使用)