添加依赖包
pom.xml --------------------------------------------------------------- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.9</version> </dependency>配置文件
application.properties --------------------------------------------------------------- # pgsql数据库连接配置 spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/hrjlk spring.datasource.username=hrjlk spring.datasource.password=hrJlk?aPp@测试连接
controller包 --> new --> class --> HelloSpringBootTest --------------------------------------------------------------- package com.hrjlk.controller; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloSpringBootTest { @Autowired JdbcTemplate jdbcTemplate; @GetMapping("/jdbc") @ResponseBody private Object jdbc() { List<Map<String, Object>> maplist=jdbcTemplate.queryForList("select * from test"); return maplist; } }访问测试
http//:localhost:8080/jdbc