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);
    }
}

运行结果:

数据库数据:

相关推荐
fzb5QsS1p37 分钟前
MySQL 事务的二阶段提交是什么?
数据库·mysql
清风徐来QCQ4 小时前
Lombok/SSM/devTools
数据库
LaughingZhu4 小时前
Product Hunt 每日热榜 | 2026-04-05
前端·数据库·人工智能·经验分享·神经网络
2601_949814694 小时前
使用mysql报Communications link failure异常解决
数据库·mysql
搜佛说4 小时前
02-第2章-核心概念与架构
数据库·物联网·微服务·架构·边缘计算·iot
爱敲代码的小鱼5 小时前
springboot(2)从基础到项目创建:
java·spring boot·spring
#六脉神剑6 小时前
MySQL参数调优:十个关键参数助力数据库性能数倍提升
运维·mysql
C'ᴇsᴛ.小琳 ℡6 小时前
高性能NoSQL
数据库·nosql
i220818 Faiz Ul7 小时前
动漫商城|基于springboot + vue动漫商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·动漫商城系统
iNgs IMAC7 小时前
redis 使用
数据库·redis·缓存