欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

SpringBoot集成MySQL

时间:2023-07-06
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;@RestControllerpublic 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成功,可以正常访问数据库中的表

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。