Springboot配置MySQL数据库

Springboot配置MySQL数据库

一、创建springboot项目,并添加如下依赖

xml 复制代码
<dependency>
       <groupId>com.mysql</groupId>
       <artifactId>mysql-connector-j</artifactId>
       <scope>runtime</scope>
</dependency>

二、在application.properties中配置连接数据库信息

properties 复制代码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名称
spring.datasource.username=root
spring.datasource.password=密码

三、在启动类中创建代码,并运行测试:

java 复制代码
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context =SpringApplication.run(DemoApplication.class, args);
        JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
        List<Map<String, Object>> result =
                jdbcTemplate.queryForList("SELECT * FROM student");
        System.out.println(result);
    }
}

运行结果:

数据库数据:

相关推荐
卓怡学长12 分钟前
w272基于springboot便民医疗服务小程序
java·spring boot·spring·小程序·intellij-idea
霸道流氓气质15 分钟前
Harness Engineering 从理论到实战:基于 Spring AI Alibaba 的完整实现指南
数据库·人工智能·spring·harness
linuxoffer17 分钟前
MYSQL回收碎片空间
数据库·mysql
豆瓣鸡39 分钟前
Spring Boot 全局异常处理与参数校验
spring boot·后端
名字还没想好☜41 分钟前
Go 并发实战:用 channel 实现 worker pool
java·数据库·后端·golang·go
Arya_aa1 小时前
植物病虫害识别系统–––登录模块
spring boot
万亿少女的梦1681 小时前
基于Spring Boot与Vue的繁星技术论坛系统设计与实现
java·spring boot·mysql·vue·系统设计
源图客1 小时前
Claude Code基础使用
服务器·前端·数据库
白帽小丑11 小时前
# 一次 MySQL DELETE 误操作的数据恢复尝试实录
数据库·mysql
Dxy123931021611 小时前
MySQL索引完整教程:创建、查看、修改、删除与日常管理
前端·javascript·mysql