简介
1、H2数据库是一个开源的关系型数据库。H2采用java语言编写,不受平台的限制,同时支持网络版和嵌入式版本,有比较好的兼容性,支持相当标准的sql标准
2、提供JDBC、ODBC访问接口,提供了非常友好的基于web的数据库管理界面
官网:
http://www.h2database.com/
配置
添加项目依赖:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
还可以添加一些额外的工具jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
前者是必备jar包,后者是辅助jar包,用于查看内存数据库。
application.properties添加配置
#这里用的是内存数据库
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.h2.username=sa
spring.datasource.h2.password=
spring.datasource.h2.driver-class-name=org.h2.Driver
spring.h2.console.path=/h2
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
后记:这些可以在..\spring-boot-autoconfigure-2.0.5.RELEASE.jar!\org\springframework\boot\autoconfigure\h2\H2ConsoleProperties.class中找到对应配置源码
之后可以在
http://localhost:8080/h2/
登录数据库的管理后台。
内存数据库改成存储在文件中:
spring.datasource.url=jdbc:h2:D:\springbootdemo\src\main\resources\h2database
之后再停止项目,数据还在
DROP TABLE IF EXISTS TEST;
CREATE TABLE TEST(ID INT PRIMARY KEY,
NAME VARCHAR(255));
INSERT INTO TEST VALUES(1, 'Hello');
INSERT INTO TEST VALUES(2, 'World');
SELECT * FROM TEST ORDER BY ID;
UPDATE TEST SET NAME='Hi' WHERE ID=1;
DELETE FROM TEST WHERE ID=2;
h2的CRUD
1.新建实体类
D:\springbootdemo\src\main\java\cn\greenleaf\domain\Employee.java
package cn.greenleaf.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String Name;
}
2.创建仓库
D:\springbootdemo\src\main\java\cn\greenleaf\domain\EmployeeRepository.java
package cn.greenleaf.domain;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeeRepository extends JpaRepository<Employee,Long> {
}
3.创建controller
D:\springbootdemo\src\main\java\cn\greenleaf\controller\EmployeeController.java
package cn.greenleaf.controller;
import cn.greenleaf.domain.Employee;
import cn.greenleaf.domain.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@GetMapping("/list")
public List<Employee> findAll(){
return employeeRepository.findAll();
}
@RequestMapping("/save")
public Boolean save(@RequestBody Employee employee){
employeeRepository.save(employee);
return Boolean.TRUE;
}
@RequestMapping("/update")
public Boolean update(@RequestBody Employee employee){
employeeRepository.save(employee);
return Boolean.TRUE;
}
@RequestMapping("/delete{id}")
public Boolean update(@PathVariable long id){
employeeRepository.deleteById(id);
return Boolean.TRUE;
}
}
注意点:
#这样配置以保证数据库实时更新
spring.datasource.url=jdbc:h2:D:\\springbootdemo\\src\\main\\resources\\h2database;DB_CLOSE_DELAY=-1;
spring.jpa.hibernate.ddl-auto=update
可以安装restToolkit插件测试。
springboot REST data
D:\springbootdemo\src\main\java\cn\greenleaf\domain\Person.java:
package com.example.accessingdatarest;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
D:\springbootdemo\src\main\java\cn\greenleaf\domain\PersonRepository.java:
package com.example.accessingdatarest;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
This repository is an interface and will allow you to perform various operations involving Person objects. It gets these operations by extending the PagingAndSortingRepository interface defined in Spring Data Commons.
At runtime, Spring Data REST will create an implementation of this interface automatically. Then it will use the @RepositoryRestResource annotation to direct Spring MVC to create RESTful endpoints at /people.
这两段代码配置可以自动实现REST接口进行数据库CRUD。
详见 https://spring.io/guides/gs/accessing-data-rest/
目前实践发现,启动时会自动创建person表,且不会保存。
问题
在domain上没有加@Data注解导致测试时数据一直是空的。
在实体类的编写过程中,常常需要应用大量的get、set方法,需要写大量的重复代码,即有的工具有自动生成功能,当时也会使实体类中产生大量冗余代码,使得代码变,springboot为我们提供了相应注解可以解决这类问题----@Data
接下来简明扼要的介绍一下@Data注解的功能与使用方法
注解功能
@Data可以为类提供读写功能,从而不用写get、set方法。
、他还会为类提供 equals()、hashCode()、toString() 方法。
使用方法--一下仅提供idea的使用方法
1下安装lombok插件
settings/plugins在插件库中搜索安装即可。
2重启idea
3在maven库中添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
4、在实体类上添加@Data注解即可生效
网友评论