二、配置
官方建议使用.yaml后缀的配置文件,所以我们也使用.yaml后缀名的配置文件
创建application.yaml配置文件
链接数据库
spring:
datasource:
username:
password:
#时区问题+servertimezone=Asia/Shanghai
url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8
#mysql8以上使用com.mysql.cj.jdbc.Driver
driver-class-name: com.mysql.jdbc.Driver
三、测试
在tests运行类中测试
package com.xyh;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
@SpringBootTest
class Springboot04DataApplicationTests {
//自动配置数据源
@Autowired
DataSource dataSource;
@Test
void contextLoads() {
//查看默认数据源
System.out.println(dataSource.getClass());
}
}
四、CRUD
package com.xyh.Controller;
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.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JDBCcontroller {
//springboot已经配置好的模板bean
@Autowired
JdbcTemplate jdbcTemplate;
//查询数据库的所有信息
@GetMapping("userList")
public List



