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

运行结果:

数据库数据:

相关推荐
北邮刘老师12 分钟前
马斯克的梦想与棋盘:空天地一体的智能体互联网
数据库·人工智能·架构·大模型·智能体·智能体互联网
开开心心_Every14 分钟前
优化C盘存储:自定义软件文档保存路径工具
java·网络·数据库·typescript·word·asp.net·excel
黄俊懿18 分钟前
【深入理解SpringCloud微服务】Gateway简介与模拟Gateway手写一个微服务网关
spring boot·后端·spring·spring cloud·微服务·gateway·架构师
用户21903265273522 分钟前
别再到处try-catch了!SpringBoot全局异常处理这样设计
java·spring boot·后端
醉舞经阁半卷书122 分钟前
Etcd服务端参数详解
数据库·etcd
梁同学与Android26 分钟前
Android ---【经验篇】阿里云 CentOS 服务器环境搭建 + SpringBoot项目部署(二)
android·spring boot·后端
gugugu.26 分钟前
Redis持久化机制详解(一):RDB全解析
数据库·redis·缓存
用户21903265273534 分钟前
SpringBoot自动配置:为什么你的应用能“开箱即用
java·spring boot·后端
暗之星瞳39 分钟前
mysql表的链接
大数据·数据库·mysql
陌路2042 分钟前
redis持久化篇AOF与RDB详解
数据库·redis·缓存