zoukankan      html  css  js  c++  java
  • SpringBoot的jdbc进行数据库的l连接

    这里的数据库连接的是虚拟机里面docker环境下的mysql数据库(docker环境的装配在前面也有讲到https://www.cnblogs.com/zhaochunhui/p/11359544.html

    使用的是mysql的数据库,使用jdbc的链接驱动,在创建项目的时候,就要选择好我们的依赖;(jdbc、mysql、web)
    <?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jdbc</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
     
    <properties>
    <java.version>1.8</java.version>
    </properties>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.18</version>
    </dependency>
     
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
     
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>
     
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>
     
    </project>
     
    在啊application.xml或者是application.properties进行配置:
    spring:
    datasource:
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    # initialization-mode: always
    # schema:
    # - classpath:department.sql
    (这里面是有层级关系的),设置好数据库的用户名、密码、地址(虚拟机ip/本机的localhost)
    驱动的注入;以及schema的设定
    在controller中编写一个请求:
    package com.jdbc.demo.controller;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import java.util.List;
    import java.util.Map;
    @Controller
    public class Query {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @ResponseBody
    @GetMapping("/query")
    public Map<String,Object> map(){
    List<Map<String, Object>> list = jdbcTemplate.queryForList("select * FROM department");
    return list.get(0);
    }
    }
    然后在我们发送query请求的时候就可以得到我们数据库中的第一条数据信息
  • 相关阅读:
    Merge sorted ranges
    call_once/once_flag的使用
    对‘boost::serialization::singleton_module::get_lock()’未定义的引用
    C++多线程lock_guard
    长度为0的数组—— Arrays of Length Zero
    Utunbu VLC 播放器播放本机rtp码流
    Utunbu VLC 播放器播放本机h264码流
    Declaration of non-local variable in 'for' loop
    ZFEC--Demo--C语言接口
    malloc-demo
  • 原文地址:https://www.cnblogs.com/zhaochunhui/p/11371409.html
Copyright © 2011-2022 走看看