1.增加依赖 <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> 2.初始化sql(resources/init.sql) spring.sql.init.schema-locations=classpath:init.sql 3.注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; @GetMapping("/") public String index() { List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from users"); return maps.toString(); }
4.启用/h2-console控制台页面
spring.h2.console.enabled=true
相互学习,共同进步!