zoukankan      html  css  js  c++  java
  • springboot连接mysql简单例子

    第一步:    打开 https://start.spring.io/

    输入 mysql   client,web,jdbc,jpa

    然后  idea   import 进入idea

    连接mysql逻辑

    package com.segmentfault.springbootlesson61;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import sun.font.StandardGlyphVector;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Created with IntelliJ IDEA.
     * User: @别慌
     * Date: 2019-07-31
     * Time: 11:04
     * Description:
     */
    @RestController   //返回json数据类型
    @RequestMapping("/user")    //获取web页面的mapping地址
    public class jdbcController {
    
        @Autowired              //自动装配,根据类型进行装配,map<Object,String>  装配的值就是  int,string
    
        private DataSource dataSource;
    
        @GetMapping("get")
    
        public Map<Object,String> getuser(@RequestParam(value = "idd",defaultValue = "1") String idd ){
            // RequestParam   请求的参数
            Map<Object,String> data=new HashMap<Object,String>();
    
                Connection connection=null;
            try {
                connection=dataSource.getConnection();
                Statement statement=connection.createStatement();
                ResultSet resultSet=statement.executeQuery("select idd,name from song where idd ="+idd);
                while (resultSet.next()){
                    int idd_=resultSet.getInt("idd");
                    String name=resultSet.getString("name");
    
                    data.put(idd_,name);
                    System.out.println(idd_+"="+name);
    
                }
    
    
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    
            return data;
    
        }
    }
    spring.datasource.url=jdbc:mysql://localhost:3306/test    //连接的mysqlurl地址
    
    spring.datasource.username=root                           //连接的mysql账号密码
    spring.datasource.password=123456
    
    spring.datasource.driverClassName=com.mysql.jdbc.Driver   //连接的mysql数据驱动
    RUSH B
  • 相关阅读:
    0113-06-侯舒舒(作业-复盘分析)
    0113-06-侯舒舒(日报)
    总结
    0108-06-侯舒舒(日报)
    cobalt strike插件
    关于python安装后更新的一些问题
    常用dos命令
    curl命令用法
    2020/1/16
    2020/1/15
  • 原文地址:https://www.cnblogs.com/tangsonghuai/p/11277400.html
Copyright © 2011-2022 走看看