spring boot配置mysql

 时间:2026-02-13 23:31:07

1、创建spring boot项目,如下图所示

spring boot配置mysql

2、添加maven依赖,代码如下

<dependency>           

<groupId>org.springframework.boot</groupId>           

<artifactId>spring-boot-starter-jdbc</artifactId>       

</dependency>       

<dependency>           

<groupId>mysql</groupId>           

<artifactId>mysql-connector-java</artifactId>           

<scope>runtime</scope>       

</dependency>

spring boot配置mysql

3、修改application.properties配置文件,添加如下代码

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring boot配置mysql

4、添加测试代码,在启动方法中添加如下代码,用JdbcTemplate操作数据库

@SpringBootApplication

public class SpringBootMysqlApplication {

         public static void main(String[] args) {

        ConfigurableApplicationContext context =

                SpringApplication.run(SpringBootMysqlApplication.class, args);

        JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);

        List<Map<String, Object>> result =

                jdbcTemplate.queryForList("SELECT * FROM USER");

        System.out.println(result);

         }

}

spring boot配置mysql

5、点击启动按钮,启动程序

spring boot配置mysql

6、在控制台打印了user表的所有信息,如下图所示,mysql配置成功,数据连接池也创建成功

spring boot配置mysql

  • Python生成随机数
  • MATLAB2018怎么下载安装与激活
  • Python如何批量读取图片
  • 超声波局放仪SDT340检测高压配电柜局部放电
  • 树莓派3B远程登录桌面失败的解决方法
  • 热门搜索
    扁桃体切除有什么影响 如什么如什么的成语 mds是什么病 吗啡是什么 薛定谔的猫什么意思 什么是有机奶粉 优柔寡断是什么意思 卒是什么意思 心要让你听见爱要你看见什么歌 丝光棉是什么面料