Spring boot 项目访问 Mysql 数据库

简介

本文介绍了 Spring boot 项目如何访问 Mysql 数据库,插入和查询 Mysql 数据。

环境准备

  • JDK 1.8 +
  • Gradle 4 + or Maven 3.2+
  • IDEA
  • Postman
  • Mysql server 8.0.28

创建 Spring boot 项目

需要这些依赖

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</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>

社区版的 IDEA 可以参考 《社区版 IntelliJ IDEA 如何创建Spring boot项目》

旗舰版的 IDEA 安装 spring initializr 后创建 Spring boot

配置文件

src/main/resources/application.properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://172.23.176.3:3306/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name =com.mysql.jdbc.Driver

开发过程

1. 创建 Entity 模型

src/main/java/com/example/demo/accessingdatamysql/User.java

package com.example.demo.accessingdatamysql;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;

    private String email;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

2. 创建Repository

src/main/java/com/example/demo/accessingdatamysql/UserRepository.java

package com.example.demo.accessingdatamysql;

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Integer> {

}

3. 创建Controller

src/main/java/com/example/demo/accessingdatamysql/MainController.java

package com.example.demo.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(path="/demo")
public class MainController {

    @Autowired
    private UserRepository userRepository;

    @PostMapping(path="/add")
    public @ResponseBody String addNewUser(@RequestParam String name, @RequestParam String email){

        User user = new User();
        user.setName(name);
        user.setEmail(email);
        userRepository.save(user);
        return "Saved";

    }

    @GetMapping(path = "/all")
    public @ResponseBody Iterable<User> getAllUsers() {
        return userRepository.findAll();
    }

}

4. 创建 Spring boot 启动类

src/main/java/com/example/demo/accessingdatamysql/AccessingDataMysqlApplication.java

package com.example.demo.accessingdatamysql;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AccessingDataMysqlApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccessingDataMysqlApplication.class,args);
    }
}

启动 Spring boot 项目并postman 测试

1. IDEA 启动 Spring boot 项目

Spring boot 项目访问 Mysql 数据库

2. Postman 测试插入数据

Spring boot 项目访问 Mysql 数据库

3. Postman 测试查询数据

Spring boot 项目访问 Mysql 数据库

上一篇:【记录】使用maven创建springboot项目过程


下一篇:001SpringBoot的第一个程序