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

运行结果:

数据库数据:

相关推荐
nice_lcj5203 小时前
【从零理解 Spring AI · 14】RAG 的四个环节:加载、切分、向量化、检索
java·spring boot·embedding·向量数据库·rag·spring ai·ai应用开发
学长毕设5 小时前
基于springboot科创竞赛信息平台-附源码
java·spring boot·后端
学长毕设6 小时前
基于SpringBoot公务员考试交流平台-附源码
java·spring boot·后端
SelectDB6 小时前
让变更流出 Doris:Apache Doris 5.0 基于增量计算的 CDC 技术解析
数据库
这个DBA有点耶6 小时前
MCP隔离层+行级语义审计+输出校验:AI访问数据库的安全边界拆解
数据库·人工智能·ai编程
这个DBA有点耶7 小时前
从反向扫描O(n)到正向扫描O(1):降序索引的物理机制与混合排序匹配规则
数据库
2501_933923259 小时前
解读Redis:单线程模型
数据库·redis·缓存
AI开源软件9 小时前
SpringBoot3+AI Agent 智能报销:怎样把对话接进现有财务审批系统
spring boot·ai agent·人工确认·ruoyi office·agentos·智能报销·财务审批
学长毕设9 小时前
基于SpringBoot的高校后勤报修管理系统-附源码
java·spring boot·后端
Tapdata10 小时前
被忽略的第三类上下文:Agent 进入真实业务后,还缺什么?
数据库·人工智能·架构