zoukankan      html  css  js  c++  java
  • springboot连接mysql数据库,JdbcTemplate方式

    SQL部分

    CREATE TABLE test(
      id int(10) primary key,
      name varchar(50) not null,
      age int(10),
      address varchar(50)
    );
    
    insert into test values(1,'zs',20,'bj');
    insert into test values(2,'ls',20,'sh');
    insert into test values(3,'ww',20,'bj');

    1.application.properties

    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    server.port=8080
    server.tomcat.uri-encoding=UTF-8

    2.pom.xml

            <!--mysql依赖-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
           <version>8.0.11</version>
            </dependency>
            <!--jdbc依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>

    pom.xml中有关artifactId找不到
    1).file -> settings -> 搜索maven -> always update snapshots 打钩 -> OK
    2).右下角选择 import...

    3.JdbcDbController

    package com.example.demo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    import java.util.Map;
    
    @RestController
    public class JdbcDbController {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @RequestMapping("/test")
        public List test(){
            String sql = "select * from test";
            List list = jdbcTemplate.queryForList(sql);
    return list; } }

    访问地址

    http://localhost:8080/test

  • 相关阅读:
    Oracle2MySQL数据校验
    MySQL物理删除大表文件的方式
    MySQL恢复误删除的ibd文件
    如何下载MySQL企业版数据库
    C++篇之成员函数注册为回调函数
    Java入门篇
    日常收藏之Linux篇
    实现简单网页rtmp直播:nginx+ckplayer+linux
    网络通信之accept函数简述
    网络通信之bind、listen函数简述
  • 原文地址:https://www.cnblogs.com/songfei90/p/10646906.html
Copyright © 2011-2022 走看看