访问H2数据库的SpringBoot工程
JDK:1.8.0_212
IDE:STS4(Spring Tool Suit4 Version: 4.3.2.RELEASE)
工程下载:https://files.cnblogs.com/files/xiandedanteng/RestTeamplate20190929.rar
工程还是使用Spring Starter project创建,添加Web依赖,之后在pom.xml添加JPA和H2的依赖。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.hy</groupId> <artifactId>RestTeamplate</artifactId> <version>0.0.1-SNAPSHOT</version> <name>RestTeamplate</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Data JPA依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 嵌入式数据库H2依赖 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在src/main/resources下添加文件schema.sql
drop table emp if exists; create table emp(id bigint generated by default as identity,name varchar(30),primary key(id));
在src/main/resources下添加文件data.sql
insert into emp(id,name) values (1,'孟郊andy'); insert into emp(id,name) values (2,'bill'); insert into emp(id,name) values (3,'cindy'); insert into emp(id,name) values (4,'douglas');
改写配置文件application.properties如下:
server.port=8080 spring.jpa.generate-ddl=false spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=none spring.datasource.platform=h2 spring.datasource.schema=classpath:schema.sql spring.datasource.data=classpath:data.sql
然后是实体类:
package com.example.demo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Emp { @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id; @Column private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
DAO:
package com.example.demo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface EmpRepo extends JpaRepository<Emp,Long>{ }
控制器:
package com.example.demo; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Ctrl { @Autowired private EmpRepo empRepo; @RequestMapping("/test") public String test() { return "你好"; } @RequestMapping("/emp/{id}") public Object findById(@PathVariable Long id) { Emp e=null; Optional<Emp> opt =this.empRepo.findById(id); if(opt.isPresent()) { e=opt.get(); } return e; } }
然后运行启动类,在浏览器里输入http://localhost:8080/emp/1,你将看到:
--END-- 2019年9月29日10:51:10