zoukankan      html  css  js  c++  java
  • SpringBoot整合MySql

    SpringBoot整合MySql

    系统要求

    Java 8+

    springBoot2.5 +

    创建springBoot项目工程

    导入依赖
    <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>
    
    编写application.yml
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3307/webapp1
        username: webapp1
        password: webapp1
    
    进行测试
    package com.xiang.springbootdatabase;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import java.util.List;
    import java.util.Map;
    
    @SpringBootTest
    class SpringBootDatabaseApplicationTests {
        /*该对象是SpringBoot创建的可以对数据进行操作*/
        @Autowired
        JdbcTemplate jdbcTemplate;
    
        @Test
        /**
         * 查询所有
         */
        void contextLoads() {
            List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user;");
            for (Map<String, Object> map : list) {
                System.out.println(map);
            }
        }
    
    }
    
    
    运行结果

  • 相关阅读:
    面向对象设计大作业第二阶段:图书馆系统
    OO之接口-DAO模式代码阅读及应用
    OO设计-有理数类的设计
    DS博客作业05--查找
    DS博客作业04--图
    DS博客作业03--树
    DS博客作业02--栈和队列
    DS01——线性表
    c博客06-结构体&文件
    C语言博客作业05——指针
  • 原文地址:https://www.cnblogs.com/d534/p/15417647.html
Copyright © 2011-2022 走看看