【spring-boot】集成mysql数据库

本文介绍了如何在Spring Boot项目中集成MySQL数据库,包括创建表数据bean对象、数据库bean仓库、接口请求控制器、启动类配置、数据库配置properties文件以及项目的目录结构和POM.XML依赖管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.构造表数据bean对象:

package com.bean;

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

@Entity
public class Interfaces {

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

    private String name;

    private String path;

    private String author;

    private String description;

    private String request_type;

    private String accept;

    private String post_type;

    private String state;

    private String status;

    private Integer interface_type;

    private Integer environment_id;

    private Integer service_id;

    private String controllerName;

    private Integer isvalid;

    private String remark;

    private Long update_time;

    private Long create_time;

    private String creator;

    private String statusTag;

    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 == null ? null : name.trim();
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path == null ? null : path.trim();
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author == null ? null : author.trim();
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description == null ? null : description.trim();
    }

    public String getRequest_type() {
        return request_type;
    }

    public void setRequest_type(String request_type) {
        this.request_type = request_type == null ? null : request_type.trim();
    }

    public String getAccept() {
        return accept;
    }

    public void setAccept(String accept) {
        this.accept = accept == null ? null : accept.trim();
    }

    public String getPost_type() {
        return post_type;
    }

    public void setPost_type(String post_type) {
        this.post_type = post_type == null ? null : post_type.trim();
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state == null ? null : state.trim();
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status == null ? null : status.trim();
    }

    public Integer getInterface_type() {
        return interface_type;
    }

    public void setInterface_type(Integer interface_type) {
        this.interface_type = interface_type;
    }

    public Integer getEnvironment_id() {
        return environment_id;
    }

    public void setEnvironment_id(Integer environment_id) {
        this.environment_id = environment_id;
    }

    public Integer getService_id() {
        return service_id;
    }

    public void setService_id(Integer service_id) {
        this.service_id = service_id;
    }

    public String getControllerName() {
        return controllerName;
    }

    public void setControllerName(String controllerName) {
        this.controllerName = controllerName == null ? null : controllerName.trim();
    }

    public Integer getIsvalid() {
        return isvalid;
    }

    public void setIsvalid(Integer isvalid) {
        this.isvalid = isvalid;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark == null ? null : remark.trim();
    }

    public Long getUpdate_time() {
        return update_time;
    }

    public void setUpdate_time(Long update_time) {
        this.update_time = update_time;
    }

    public Long getCreate_time() {
        return create_time;
    }

    public void setCreate_time(Long create_time) {
        this.create_time = create_time;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator == null ? null : creator.trim();
    }

    public String getStatusTag() {
        return statusTag;
    }

    public void setStatusTag(String statusTag) {
        this.statusTag = statusTag == null ? null : statusTag.trim();
    }
    public static Interfaces interfacesObj(String apiName, String path,  String request_type,String accept, String post_type, int interface_type,int environment_id,int service_id){
        long update_time = System.currentTimeMillis();
        long create_time = System.currentTimeMillis();
        String creator = "auto";
        String author = "auto";
        String description = apiName+"接口基本参数";
        Interfaces interfaces = new Interfaces();
        interfaces.setName(apiName);
        interfaces.setPath(path);
        interfaces.setAuthor(author);
        interfaces.setDescription(description);
        interfaces.setRequest_type(request_type);
        interfaces.setAccept(accept);
        interfaces.setPost_type(post_type);
        interfaces.setState("1");
        interfaces.setStatus("activated");
        interfaces.setInterface_type(interface_type);
        interfaces.setEnvironment_id(environment_id);
        interfaces.setService_id(service_id);
        interfaces.setControllerName("");
        interfaces.setIsvalid(0);
        interfaces.setRemark("");
        interfaces.setUpdate_time(update_time);
        interfaces.setCreate_time(create_time);
        interfaces.setStatusTag("");
        interfaces.setCreator(creator);
        return interfaces;
    }
}

2.构造表数据库bean仓库:

package com.bean;

import org.springframework.data.repository.CrudRepository;

public interface InterfacesRepository extends CrudRepository<Interfaces, Integer> {
}

3.构造接口请求调用控制器:

package com.controller;

import com.bean.Interfaces;
import com.bean.InterfacesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(path="/interfaces")
public class InterfacesController {

    @Autowired
    private InterfacesRepository interfacesRepository;

    @PostMapping(path="/add")
    public @ResponseBody String addNewUser (@RequestParam String apiName,
                                            @RequestParam String path,
                                            @RequestParam String request_type,
                                            @RequestParam String accept,
                                            @RequestParam String post_type,
                                            @RequestParam int environment_id,
                                            @RequestParam int service_id) {
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request

        Interfaces interfaces = new Interfaces();
        long update_time = System.currentTimeMillis();
        long create_time = System.currentTimeMillis();
        String creator = "auto";
        String author = "auto";
        String description = apiName+"接口基本参数";
        interfaces.setName(apiName);
        interfaces.setPath(path);
        interfaces.setAuthor(author);
        interfaces.setDescription(description);
        interfaces.setRequest_type(request_type);
        interfaces.setAccept(accept);
        interfaces.setPost_type(post_type);
        interfaces.setState("1");
        interfaces.setStatus("activated");
        interfaces.setInterface_type(0);
        interfaces.setEnvironment_id(environment_id);
        interfaces.setService_id(service_id);
        interfaces.setControllerName("");
        interfaces.setIsvalid(0);
        interfaces.setRemark("");
        interfaces.setUpdate_time(update_time);
        interfaces.setCreate_time(create_time);
        interfaces.setStatusTag("");
        interfaces.setCreator(creator);
        interfacesRepository.save(interfaces);
        return "Saved";
    }

    @GetMapping(path="/query")
    public @ResponseBody Iterable<Interfaces> getAllUsers() {
        return interfacesRepository.findAll();
    }
}

4.spring-boot启动类:

package com;

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

@SpringBootApplication(scanBasePackages = {"com"})
public class AccessingDataMysqlApplication {

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

5.数据库配置properties文件:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://rm-uf6xuw7c9aqu09c26to.mysql.rds.aliyuncs.com:3306/saas_auto_test
#spring.datasource.url=jdbc:mysql://${MYSQL_HOST:rm-uf6xuw7c9aqu09c26to.mysql.rds.aliyuncs.com}:3306/saas_auto_test
spring.datasource.username=apitest
spring.datasource.password=H9wejbowHwei1j0
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
#spring.jpa.show-sql: true

6.目录层级:

7. POM.XML:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>accessing-data-mysql-complete</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>accessing-data-mysql-complete</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值