spring boot配置mysql

编程知识 更新时间:2023-04-05 03:08:47

spring boot通过简单的配置就可以连接mysql,并且可以创建默认的连接池HikariDataSource,下面我们来看一看如何配置mysql

  1. 添加maven依赖,代码如下
        <!-- mysql -->
        <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>
  1. 修改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

  1. 添加测试代码,在启动方法中添加如下代码,用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

本文发布于:2023-04-05 03:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/7b10e697e551ee26b9bcd12022f78ed6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:spring   boot   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 44437文章数
  • 14阅读数
  • 0评论数