- 连接配置数据库
- 编写controller
- 测试文件
- 启动主类
- 前端调用接口,查询数据库成功
导入依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test
application.yaml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.cj.jdbc.Driver
编写controller
@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
@GetMapping("/user")
public List
测试文件
@SpringBootTest
class Springboot05DataApplicationTests {
//获取数据源
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
//查看默认数据源,hikari
System.out.println(dataSource.getClass());
//获得数据库连接
Connection connection = dataSource.getConnection();
System.out.println(connection);
// xxxx Template:springboot已经配置好的模板bean,拿来即用
//关闭连接
connection.close();
}
}
启动主类
@SpringBootApplication
public class Springboot05DataApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot05DataApplication.class, args);
}
}
前端调用接口,查询数据库成功



