请教下,通过连接sql数据库,并将内容显示在网页上,比如:
这个是我的目录:
运行启动项是这样的错误:
helloController:
package example.springbootmysql05a;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
public class helloController {
@Autowired
JdbcTemplate jdbcTemplate;
@ResponseBody
@RequestMapping("getList")
public List<Map<String, Object>> getList(){
return jdbcTemplate.queryForList("Select * from department");
}
}
application.yml:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 12345678
url: jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC #数据库路径
schema:
-classpath:people.sql
数据库中的表格people.sql:
create table people
(
id int(2) not null,
name varchar(10) null,
constraint people_pk
primary key (id)
)charset = utf8;
insert into people(id,name) values (1,'fe');
insert into people(id,name) values (2,'frg');
单元测试applicationtest:
package com.example.springbootmysql05a;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootMysql05aApplicationTests {
@Resource
private DataSource dataSource;
@Test
public void teatJdbc() throws SQLException {
Connection connection=dataSource.getConnection();
System.out.println(connection.getClass());
System.out.println(connection);//打印对象
connection.close();//关闭对象
}
}