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

运行结果:

数据库数据:

相关推荐
北极糊的狐6 分钟前
MySQL常见报错分析及解决方案总结(42)---ERROR 1142 (42000): SELECT command denied
java·mysql·adb·myeclipse
西柚小萌新7 分钟前
【MySQL数据库】--借助AI快速画数据库ER图
数据库·mysql
EnCi Zheng8 分钟前
1. AI数据库工具对比 [特殊字符]
数据库·人工智能
升鲜宝供应链及收银系统源代码服务14 分钟前
升鲜宝生鲜配送供应链管理系统生产加工子模块的详细表设计说明
java·大数据·前端·数据库·bootstrap·供应链系统·生鲜配送
一只会奔跑的小橙子15 分钟前
性能压测问题排查思路
jvm·数据库
Gauss松鼠会16 分钟前
openGauss数据库源码解析系列文章--openGauss简介(上)
数据库·性能优化·database·opengauss
V1ncent Chen16 分钟前
从零学SQL 05 基础查询
数据库·sql·mysql·数据分析
老迟聊架构18 分钟前
完全基于对象存储的数据库引擎:SlateDB
数据库·后端·架构
阿蒙Amon23 分钟前
C#常用类库-详解CsvHelper
开发语言·数据库·c#