栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot集成MySQL

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot集成MySQL

SpringBoot集成MySQL:

注:集成mysql之前请先搭建好springboot(springboot搭建)
1
配置pom.xml文件引入mysql依赖



    mysql
    mysql-connector-java
    8.0.26


    org.springframework.boot
    spring-boot-starter-jdbc
    2.6.1

2
配置yml文件application.yml

spring:
  application:
    name: springboot_union
  datasource:
    #url切换数据库之后如果对应数据库名称和路径有变动,需要修改url
    url: jdbc:mysql://localhost:3306/springboot_union?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

3
设计表

打开数据库,新建表,表名为:mysql_test
填充数据
建表语句:

CREATE TABLE `mysql_test` ( `id` INT UNSIGNED auto_increment, `username` VARCHAR ( 100 ), `user_id` VARCHAR ( 100 ), `create_time` VARCHAR ( 100 ), PRIMARY KEY ( `id` ) ) ENGINE = INNODB;

INSERT INTO `mysql_test` (`user_name`,`user_id`,`create_time`) VALUES ('jwh','01','2021-10-20');
INSERT INTO `mysql_test` (`user_name`,`user_id`,`create_time`) VALUES ('伽罗','175','2021-10-22');
INSERT INTO `mysql_test` (`user_name`,`user_id`,`create_time`) VALUES ('孙行者','185','2021-10-23');

4
包结构

5
实体类StudentMysql.java

package com.jwh.springboot.integration_test.mysql_test.entity;


import java.util.Objects;



public class StudentMysql {
    private int id;
    private String userName;
    private String userId;
    private String createTime;


    
    public StudentMysql() {
    }


    
    public StudentMysql(int id, String userName, String userId, String createTime) {
        this.id = id;
        this.userName = userName;
        this.userId = userId;
        this.createTime = createTime;
    }


    
    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getUserName() {
        return userName;
    }


    public void setUserName(String userName) {
        this.userName = userName;
    }


    public String getUserId() {
        return userId;
    }


    public void setUserId(String userId) {
        this.userId = userId;
    }


    public String getCreateTime() {
        return createTime;
    }


    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {return true;}
        if (o == null || getClass() != o.getClass()) {return false;}
        StudentMysql that = (StudentMysql) o;
        return id == that.id && Objects.equals(userName, that.userName) && Objects.equals(userId, that.userId) && Objects.equals(createTime, that.createTime);
    }


    @Override
    public int hashCode() {
        return Objects.hash(id, userName, userId, createTime);
    }


    @Override
    public String toString() {
        return "StudentMysql{" +
                "id=" + id +
                ", userName='" + userName + ''' +
                ", userId='" + userId + ''' +
                ", createTime='" + createTime + ''' +
                '}';
    }
}

6
接口测试类

package com.jwh.springboot.integration_test.mysql_test.controller;


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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping(value="/hello", method= RequestMethod.GET)//如果使用postman进行测试,使用post方式进行访问,则需将此处的GET改为POST
    public String index() {

        String sql = "SELECt user_name FROM mysql_test WHERe id = ?";

        // 通过jdbcTemplate查询数据库
        String userName = (String) jdbcTemplate.queryForObject(
                sql, new Object[]{1}, String.class);

        return "Good job!" + userName;
    }
}

7
启动项目

8
浏览器中访问地址 http://localhost:8080/hello

测试成功
表示springboot集成mysql成功,可以正常访问数据库中的表

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732108.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号