SpringBoot集成H2内存数据库方法

网友投稿 975 2022-12-11

SpringBoot集成H2内存数据库的方法

SpringBoot集成H2内存数据库的方法

目录前言准备技术栈目录结构pom.xml实体类 UserAddressRepositoryapplication.yml连接配置数据初始化配置h2 web consloe配置代码-

H2是Thomas Mueller提供的一个开源的、纯java实现的关系数据库。

前言

本篇文章引导你使用Spring Boot,Spring Data JPA集成H2内存数据库。更多关于H2数据参考:http://h2database.com/html/tutorial.html

准备

JDK 1.8 或更高版本

Maven 3 或更高版本

技术栈

Spring Data JPA

Spring Boot

目录结构

pom.xml

xmlns:xsi="http://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">

jpa-example

cn.merryyou

1.0-SNAPSHOT

4.0.0

h2-webconsole

UTF-8

1.8

org.springframework.boot

spring-boot-starter-data-jpa

com.h2database

h2

1.4.196

org.projectlombok

lombok

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-compiler-plugin

3.6.1

1.8

1.8

xmlns:xsi="http://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">

jpa-example

cn.merryyou

1.0-SNAPSHOT

4.0.0

h2-webconsole

UTF-8

1.8

org.springframework.boot

spring-boot-starter-data-jpa

com.h2database

h2

1.4.196

org.projectlombok

lombok

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-maven-plugin

org.apache.maven.plugins

maven-compiler-plugin

3.6.1

1.8

1.8

实体类 User

@Entity

@Table(name = "t_user")

@Data

public class User {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;

private String name;

private String url;

@Override

public String toString() {

return "User{" +

"id=" + id +

", name='" + name + '\'' +

", url='" + url + '\'' +

'}';

}

}

@Table声明此对象映射到数据库的数据表,通过它可以为实体指定表(talbe),目录(Catalog)和schema的名字。该注释不是必须的,如果没有则系统使用默认值(实体的短类名)。

@Id 声明此属性为主键。该属性值可以通过应该自身创建,但是Hibernate推荐通过Hibernate生成

@GeneratedValue 指定主键的生成策略。

TABLE:使用表保存id值

IDENTITY:identitycolumn

SEQUENCR :sequence

AUTO:根据数据库的不同使用上面三个

@Column 声明该属性与数据库字段的映射关系。

AddressRepository

public interface UserRepository extends JpaRepository {

}

Spring Data JPA包含了一些内置的Repository,实现了一些常用的方法:findone,findall,save等。

application.yml

spring:

datasource:

url: jdbc:h2:mem:h2test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

platform: h2

username: sa

password:

driverClassName: org.h2.Driver

jpa:

database-platform: org.hibernate.dialect.H2Dialect

hibernate:

ddl-auto: update

properties:

hibernate:

show_sql: true

use_sql_comments: true

format_sql: true

h2:

console:

enabled: true

path: /console

settings:

trace: false

web-allow-others: false

logging:

level: debug

连接配置

在application.yml文件中对数据库进行连接配置

spring.datasource.url=jdbc:h2:mem:h2test,配置h2数据库的连接地址

spring.datasource.driver-class-name=org.h2.Driver,配置JDBC Driver

spring.datasource.username=sa,配置数据库用户名

spring.datasource.password=,配置数据库密码

当你完成依赖和连接配置这两步之后,你就可以在程序种使用h2了。spring会自动帮你完成DataSource的注入。

数据初始化配置

如果你需要在程序启动时对数据库进行初始化操作,则在application.properties文件中对数据库进接配置

spring.datasource.schema=classpath:db/schema.sql,进行该配置后,每次启动程序,程序都会运行resources/db/schema.sql文件,对数据库的结构进行操作。

spring.datasource.data=classpath:db/data.sql,进行该配置后,每次启动程序,程序都会运行resources/db/data.sql文件,对数据库的数据操作。

该配置非常适合开发环境,我会把数据库的结构构建sql放在resources/db/schema.sql,数据sql放在resources/db/data.sql中。这样每次运行程序我都可以得到一个新的数据库。这样就不需要我每次为了测试而修改数据中的内容了。

h2 web consloe配置

h2 web consloe是一个数据库GUI管理应用,就和phpMyAdmin类似。程序运行时,会自动启动h2 web consloe。当然你也可以进行如下的配置。

spring.h2.console.settings.web-allow-others=true,进行该配置后,h2 web consloe就可以在远程访问了。否则只能在本机访问。

spring.h2.console.path=/h2-console,进行该配置,你就可以通过YOUR_URL/h2-console访问h2 web consloe。YOUR_URL是你程序的访问URl。

spring.h2.console.enabled=true,进行该配置,程序开启时就会启动h2 web consloe。当然这是默认的,如果你不想在启动程序时启动h2 web consloe,那么就设置为false。

UserRepositoryTest

@SpringBootTest

@RunWith(SpringRunner.class)

@Slf4j

public class UserRepositoryTest {

@Autowired

private UserRepository userRepository;

@Test

public void saveTest() throws Exception {

User user = new User();

user.setName("郑龙飞");

user.setUrl("http://merryyou-");

User result = userRepository.save(user);

log.info(result.toString());

Assert.assertNotNull(user.getId());

}

@Test

public void findOneTest() throws Exception{

User user = userRepository.findOne(1l);

log.info(user.toString());

Assert.assertNotNull(user);

cEURKJbh Assert.assertTrue(1l==user.getId());

}

}

h2 web consloe

代码-

从我的 github 中-,https://github.com/longfeizheng/jpa-example/tree/master/h2-webconsole

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:浅谈JMeter engine启动原理
下一篇:一篇文章带你深入理解JVM虚拟机读书笔记
相关文章

 发表评论

暂时没有评论,来抢沙发吧~