风起云卷_ 2021-07-19 20:29 采纳率: 88.5%
浏览 65

controller缺少bean问题

请教下,通过连接sql数据库,并将内容显示在网页上,比如:

img

img

这个是我的目录:

运行启动项是这样的错误:
img
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();//关闭对象
    }

}
  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 修改了问题 7月19日
      • 修改了问题 7月19日
      • 修改了问题 7月19日
      • 修改了问题 7月19日
      • 展开全部

      悬赏问题

      • ¥15 easyclick重启app
      • ¥15 ros系统里Python版本问题
      • ¥15 不兼容驱动程序删不了,内核隔离打不开
      • ¥15 Centos7重启网卡自动down,只能手动up
      • ¥15 multisim用74ls148做一个5号病号呼叫系统
      • ¥15 单片机,传感器,LCD1602
      • ¥15 关于adams 批处理仿真时间太长的问题
      • ¥15 fate部署问题请求帮助
      • ¥15 关于#arcpy#的问题:arcpy栅格计算器批处理(语言-python)
      • ¥15 stata logit的备选项自变量不同