风起云卷_ 2021-07-19 20:29 采纳率: 82.1%
浏览 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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛