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

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条回答 默认 最新

  • 夜郎king 2022博客之星IT其它领域TOP 12 2023-02-18 10:09
    关注

    jdbctemplate这个类是不是没注册到spring的运行容器中,需要看些代码

    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵