1. 连接JDBC
首先选择组件,注意必须选择这两个。然后idea连接数据库。
创建一个application.yaml配置文件存放连接数据库的数据:
spring:
datasource:
username: root
password: '123456'
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.cj.jdbc.Driver
写一个controller:
@RestController
public class testController {
@Autowired
JdbcTemplate template;
@RequestMapping("/show")
public List<Map<String, Object>> test(){
String sql="select * from mybatis.user";
List<Map<String, Object>> mapList = template.queryForList(sql);
return mapList;
}
}
数据就输出在页面上了。