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

运行结果:

数据库数据:

相关推荐
RuoyiOffice2 分钟前
SpringBoot3+OnlyOffice 自动保存版本:定时回存、间隔合并与 50 版上限怎么配
spring boot·在线文档·onlyoffice·版本管理·spring boot 3·自动保存·ruoyi office
极限实验室3 分钟前
INFINI Easysearch 向量搜索实战(一)
数据库·搜索引擎
枫叶丹46 分钟前
数据库上 Kubernetes 后,KES-Operator 如何简化集群运维
数据库
huaweichenai9 分钟前
spring boot使用hutool实现调用外部接口
java·spring boot
蓝速科技21 分钟前
数字人一体机芯片选型:RK3576 与 X86 场景化对比指南丨蓝速科技
大数据·运维·数据库·人工智能·科技
IvorySQL27 分钟前
AI 时代,PostgreSQL 正在发生什么变化?
数据库·人工智能·postgresql
隔窗听雨眠32 分钟前
分析型数据库与事务型数据库:核心差异与选型决策深度解析
数据库
dkbnull1 小时前
Spring Boot配置文件敏感信息加密解密
spring boot·后端
白宇横流学长1 小时前
图书个性化推荐系统的设计与实现【源码+文档】
spring boot
RuoyiOffice1 小时前
SpringBoot3+Vue3+Flowable 企业审批平台全链路:从表单设计、节点权限到移动审批怎么落地
spring boot·vue3·uniapp·springboot3·flowable·审批系统·ruoyi office