SQL部分
CREATE TABLE test( id int(10) primary key, name varchar(50) not null, age int(10), address varchar(50) ); insert into test values(1,'zs',20,'bj'); insert into test values(2,'ls',20,'sh'); insert into test values(3,'ww',20,'bj');
1.application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver server.port=8080 server.tomcat.uri-encoding=UTF-8
2.pom.xml
<!--mysql依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency> <!--jdbc依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
pom.xml中有关artifactId找不到
1).file -> settings -> 搜索maven -> always update snapshots 打钩 -> OK
2).右下角选择 import...
3.JdbcDbController
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; @RestController public class JdbcDbController { @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping("/test") public List test(){ String sql = "select * from test"; List list = jdbcTemplate.queryForList(sql);
return list; } }
访问地址
http://localhost:8080/test