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

运行结果:

数据库数据:

相关推荐
OceanRoy5 小时前
数据库表的设计
数据库·内连接·外连接·er图·表的约束·三大范式·多表连接
Pniubi6 小时前
怎么排查慢SQL,怎么解决?
java·数据库·sql
Decay__10 小时前
VMware 安装 SQL Server 双节点 AlwaysOn 可用性组教程 —— WSFC、AG 与侦听器完整部署(系列核心篇)
运维·数据库·sql·mysql·架构
茶栀(*´I`*)10 小时前
SQL 性能分析:从慢查询日志到执行计划的完整定位方法
数据库·sql
艺杯羹11 小时前
1Panel 装好后 ufw 仍是 inactive:一次服务器安全盲区复盘
服务器·数据库·安全·网络安全·系统架构·实战·1panel
code2cat12 小时前
【随笔】MCP工具重试:任务已经创建,响应丢了怎么办
数据库·mcp
wuyk55513 小时前
Spider-第10讲-SQLite持久化与去重
数据库·爬虫·sqlite
乌暮15 小时前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring
小此方16 小时前
Re:Mysql数据库基础篇(十二):什么是事务、事务解决了什么问题、事务的四大属性、事务的操作方式
数据库·mysql
csdn_aspnet16 小时前
macOS 10 、Docker 玩轉 MySQL
运维·mysql·macos·docker·容器·mac