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

运行结果:

数据库数据:

相关推荐
就叫_这个吧3 小时前
Redis从基础命令到核心机制简单介绍
数据库·redis·缓存
三十岁老牛再出发3 小时前
07.27&7.28每日总结
c语言·mysql
dear_bi_MyOnly3 小时前
【MyBatis 操作数据库】
java·数据库·学习·mybatis·学习方法
2601_953720823 小时前
【计算机毕业设计】基于Spring Boot的心理健康咨询与评测系统的设计与实现
java·spring boot·后端
DBA_G4 小时前
南大通用GBase 8s数据库新存储引擎核心能力二
数据库·微服务·架构
whyfail5 小时前
前端学 Spring Boot(8):接口为什么越用越慢?
前端·spring boot·后端
衣乌安、5 小时前
数据库事务原理与回滚机制
数据库
Muscleheng6 小时前
Spring Boot 3.x 集成 DeepSeek 实现 Function Calling(工具调用)
人工智能·spring boot·后端·ai·spring ai·deepseek
@航空母舰6 小时前
SpringBoot通过Map实现天然的策略模式
java·spring boot·后端
CodexDave6 小时前
PostgreSQL 明明有索引却选了 Nested Loop:从行数误判修正执行计划
数据库·postgresql·执行计划·扩展统计·nestedloop