Spring boot2.x中集成H2数据库代码实例
这篇文章主要介绍了Springboot2.x中集成H2数据库代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在springboot中集成
1.添加依赖
org.springframework.boot spring-boot-starter-data-jpa com.h2database h2 runtime org.projectlombok lombok
2.添加H2相关配置,修改application.properties文件
spring.jpa.database=h2 spring.jpa.show-sql=true #ddl执行方式,updatecreate等 spring.datasource.url=jdbc:h2:./data/test;AUTO_SERVER=TRUE spring.jpa.hibernate.ddl-auto=update spring.datasource.username=sa spring.datasource.password=123456 spring.datasource.driverClassName=org.h2.Driver spring.h2.console.path=/h2-console spring.h2.console.enabled=true
说明:
spring.datasource.url
数据库文件
(1)内存数据库
jdbc:h2:mem:DBName
内存数据库的数据存在内存中,当程序停止时,不会被保存会丢失
eg:
spring.datasource.url=jdbc:h2:mem:test
(2)文件数据库
jdbc:h2:file:{FilePath}也可以简化为jdbc:h2:{FilePath}
FilePath的格式
- a)./{path}/{fileName}在当前程序的根目录下创建目录和数据库文件
- b)~/{path}/{fileName}在当前用户的根目录下创建目录和数据库文件
- c)C:/{path}/{fileName}在指定盘符的指定目录下创建数据库文件
(3)远程数据库
jdbc:h2:tcp://<{IP|hostname}>[:{Port}]/[]<{dbName}>
附加参数:
- AUTO_SERVER=TRUE启动自动混合模式,允许开启多个连接,该参数不支持在内存中运行模式
- DB_CLOSE_ON_EXIT=FALSE,当虚拟机退出时并不关闭数据库
3.代码
domain层,即User类(entity)
packagecom.example.demo.domain;
importlombok.AllArgsConstructor;
importlombok.Data;
importlombok.NoArgsConstructor;
importjavax.persistence.*;
@Entity
@Table(name="user")
@Data
publicclassUser{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
privateintid;
privateStringname;
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
dao层,即UserRepository接口
packagecom.example.demo.dao; importcom.example.demo.domain.User; importorg.springframework.data.jpa.repository.JpaRepository; importorg.springframework.stereotype.Repository; importjava.util.List; @Repository publicinterfaceUserRepositoryextendsJpaRepository{ List getUsersByName(StringName); }
controller层,即Demo
packagecom.example.demo.controller;
importcom.example.demo.dao.UserRepository;
importcom.example.demo.domain.User;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
importjava.util.List;
@RestController
publicclassDemo{
@Autowired
privateUserRepositoryrepo;
@RequestMapping("find")
publicListfind(){
return(List)repo.findAll();
}
}
编写DemoApplication
packagecom.example.demo;
importcom.example.demo.dao.UserRepository;
importcom.example.demo.domain.User;
importorg.springframework.beans.factory.InitializingBean;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.context.annotation.Bean;
@SpringBootApplication
publicclassDemoApplication{
@Bean
InitializingBeansaveData(UserRepositoryrepo){
return()->{
Useru=newUser();
u.setName("abc");
repo.save(u);
Useru1=newUser();
u1.setName("zyx");
repo.save(u1);
};
}
publicstaticvoidmain(String[]args){
SpringApplication.run(DemoApplication.class,args);
}
}
启动项目,打开浏览器访问http://localhost:8080/find
访问http://localhost:8080/h2-console/
连接上后查询数据
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。