轻松使用hsqldb小型数据库
1、在pom.xml中引入相关jar包
1 <!-- spring data jpa --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-data-jpa</artifactId> 5 </dependency> 6 7 8 <!-- 内存数据库hsqldb --> 9 <dependency> 10 <groupId>org.hsqldb</groupId> 11 <artifactId>hsqldb</artifactId> 12 <scope>runtime</scope> 13 </dependency>
2、配置yml,将数据写入硬盘
1 spring: 2 jpa: 3 show-sql: false 4 hibernate: 5 ddl-auto: update 6 datasource: 7 url: jdbc:hsqldb:file:C:/rct/db/rct-db 8 username: admin 9 password: admin 10 driverClassName: org.hsqldb.jdbcDriver
3、使用jpa注解,并设定自增主键,如果想某些属性不存入数据库,应使用@Transient注解
1 import javax.persistence.Entity; 2 import javax.persistence.GeneratedValue; 3 import javax.persistence.Id; 4 5 @Entity 6 public class Update { 7 @Id 8 @GeneratedValue 9 protected Long id; 10 String version;//当前版本 11 12 public String getVersion() { 13 return version; 14 } 15 16 public void setVersion(String version) { 17 this.version = version; 18 } 19 }
4、创建dao
1 import com.smart.rct.domain.Update; 2 import org.springframework.data.repository.CrudRepository; 3 4 public interface UpdateDao extends CrudRepository<Update, Long> { 5 }
5、保存数据
6、创建接口,获取数据
1 @ApiOperation(value = "查询历史版本") 2 @GetMapping("findAllVersion") 3 public List<Update> findAllVersion() { 4 return (List<Update>) updateDao.findAll(); 5 }
6、查看结果
相关文章