IDEA Spring boot 项目构建全过程,MySQL和idea连接

官方文档:Spring | Quickstart

问题及解决

问题一:

[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (E:\软件工程\Base Project). Please verify you inv Maven from the correct directory. -> [Help 1]

解决:

需要在包含pom.xml的文件夹中运行:

mvn spring-boot:run

问题二:

Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.3.4:run (default-cli) on project demo: Process terminated with exit code: 1 -> [Help 1]
#找不到或无法加载主类

解决:

不用mvn spring-boot:run了,直接运行主类: locate the DemoApplication.java file in the src/main/java/com/example/demo folder

成功

问题三:

直接运行主类控制台出现:

2024-09-29T17:26:41.735+08:00 ERROR 18596 --- [demo] [           main] o.a.catalina.core.AprLifecycleListener   : An incompatible version [1.2.21] of the Apache Tomcat Native library is installed, while Tomcat requires version [1.2.34]

,但是打开http://localhost:8080/hello正确出现”hello world!“

项目构建

典型的 Spring Boot 项目结构,通常会涉及到控制层(Controller)、服务层(Service)、以及持久层(Repository)。下面结合 Spring Boot 的相关概念,解释项目结构。

项目结构解释:

com
 └── example
     └── myapplication
         ├── MyApplication.java
         ├── customer
         │   ├── Customer.java
         │   ├── CustomerController.java
         │   ├── CustomerService.java
         │   └── CustomerRepository.java
         └── order
             ├── Order.java
             ├── OrderController.java
             ├── OrderService.java
             └── OrderRepository.java
1. MyApplication.java

这是整个 Spring Boot 应用的启动类,通常包含 @SpringBootApplication 注解,并且包括 main 方法来启动整个应用。Spring Boot 通过这个类来启动嵌入式的应用服务器(如 Tomcat)并运行整个项目。

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
2. customer

这个包处理与“客户”相关的逻辑,包含了 MVC 架构的各个层次。

  • Customer.java: 这是客户的实体类(Entity),通常会使用 JPA 的注解来映射数据库表中的字段。例如:

    @Entity
    public class Customer {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String name;
        private String email;
        // getters, setters, constructors
    }
  • CustomerController.java: 这是控制器类,用于处理与客户相关的 HTTP 请求。典型的注解是 @RestController,并且会通过 @RequestMapping@GetMapping 等注解来映射 URL 路径。

    @RestController
    @RequestMapping("/customers")
    public class CustomerController {
        private final CustomerService customerService;
    ​
        public CustomerController(CustomerService customerService) {
            this.customerService = customerService;
        }
    ​
        @GetMapping
        public List<Customer> getAllCustomers() {
            return customerService.getAllCustomers();
        }
    ​
        // Other endpoints like creating, updating customers
    }
  • CustomerService.java: 服务类,包含业务逻辑。通常会使用 @Service 注解。它会与持久层(Repository)进行交互,进行数据库操作。

    @Service
    public class CustomerService {
        private final CustomerRepository customerRepository;
    
        public CustomerService(CustomerRepository customerRepository) {
            this.customerRepository = customerRepository;
        }
    
        public List<Customer> getAllCustomers() {
            return customerRepository.findAll();
        }
    
        // Other business logic methods
    }
  • CustomerRepository.java: 持久层接口,通常继承自 JPAJpaRepositoryCrudRepository,负责与数据库交互,执行增删改查操作。

    public interface CustomerRepository extends JpaRepository<Customer, Long> {
        // Custom database query methods if needed
    }
3. order

customer 包类似,order 包处理与“订单”相关的逻辑,包含了对应的实体类、控制器、服务类和持久层接口。

  • Order.java: 订单实体类。

    @Entity
    public class Order {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        private String orderNumber;
        private Double amount;
        // getters, setters, constructors
    }
  • OrderController.java: 处理订单相关的 HTTP 请求的控制器类。

    @RestController
    @RequestMapping("/orders")
    public class OrderController {
        private final OrderService orderService;
    
        public OrderController(OrderService orderService) {
            this.orderService = orderService;
        }
    
        @GetMapping
        public List<Order> getAllOrders() {
            return orderService.getAllOrders();
        }
    
        // Other endpoints
    }
  • OrderService.java: 订单相关的业务逻辑类。

    @Service
    public class OrderService {
        private final OrderRepository orderRepository;
    
        public OrderService(OrderRepository orderRepository) {
            this.orderRepository = orderRepository;
        }
    
        public List<Order> getAllOrders() {
            return orderRepository.findAll();
        }
    
        // Other business logic
    }
  • OrderRepository.java: 订单持久层接口,通常继承自 JpaRepository

    public interface OrderRepository extends JpaRepository<Order, Long> {
        // Additional query methods if necessary
    }

总结

这个结构非常典型地遵循了 Spring Boot 应用的三层架构:

  1. 控制层Controller 处理用户请求。

  2. 服务层Service 处理业务逻辑。

  3. 持久层Repository 负责与数据库交互。

每个实体(如 CustomerOrder)都有对应的控制器、服务和持久层,确保了代码的模块化和清晰的职责分离。

mysql和idea连接

好的,下面是使用 MySQL 数据库的配置步骤。

1. MySQL 安装与设置

首先,确保你的系统上已经安装并运行了 MySQL。你可以使用以下命令来检查 MySQL 是否运行:

mysql -u root -p

如果 MySQL 还没有安装,可以根据你的操作系统选择安装方式,例如:

  • Windows/MacOS:可以使用 MySQL Installer 或 Homebrew 安装。

  • Linux:可以使用 apt-getyum 等包管理工具。

2. 创建数据库

登录 MySQL 后,创建一个数据库供 Spring Boot 项目使用。例如,创建一个名为 myappdb 的数据库:

CREATE DATABASE myappdb;

还可以为这个数据库创建一个用户(假设用户名为 user,密码为 password):

CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON myappdb.* TO 'user'@'localhost';
FLUSH PRIVILEGES;

3. 添加 MySQL 依赖

在 Spring Boot 项目的 pom.xml 中,添加 MySQL 依赖:

<dependencies>
    <!-- MySQL 驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- Spring Data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- 其他依赖 -->
    <!-- 例如Spring Web  -->
</dependencies>

然后,刷新 Maven 项目以下载新的依赖。

4. 配置 MySQL 数据库连接

打开 src/main/resources/application.properties 文件,添加以下 MySQL 连接配置:

# MySQL 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/myappdb?useSSL=false&serverTimezone=UTC
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# JPA配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

说明

  • spring.datasource.url 中的 localhost 可以替换为你的 MySQL 服务器的 IP 地址,

    mysql> SHOW VARIABLES WHERE Variable_name = 'port';
    #用于查看端口号

    myappdb 为前面创建的数据库名称。

  • spring.datasource.usernamespring.datasource.password 为你设置的 MySQL 用户名和密码。

  • spring.jpa.hibernate.ddl-auto=update 会根据模型自动更新数据库表结构。

5. 创建实体类和 Repository

创建一个简单的实体类来代表数据库中的表。例如,创建 User 实体:

package com.example.myapp.entity;

import javax.persistence.*;

@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;

    // getters and setters
}

接着,创建 UserRepository 接口来访问数据库:

package com.example.myapp.repository;

import com.example.myapp.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

6. 创建服务层与控制器

你可以创建一个服务层和控制器来使用这个实体和数据库交互。例如:

Service 类:
package com.example.myapp.service;

import com.example.myapp.entity.User;
import com.example.myapp.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    public User saveUser(User user) {
        return userRepository.save(user);
    }
}
Controller 类:
package com.example.myapp.controller;

import com.example.myapp.entity.User;
import com.example.myapp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> getUsers() {
        return userService.getAllUsers();
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.saveUser(user);
    }
}

7. 运行项目并测试

运行项目后,可以通过 POST 请求向 http://localhost:8080/users 发送一个 JSON 数据来创建用户,例如:

{
  "name": "John",
  "email": "john@example.com"
}

然后使用 GET 请求访问 http://localhost:8080/users 来获取所有用户。

我的项目:基线需求

buyers:
idnamephonetrade_timetrade_addressstauts
int auto_incrementvarchar20varchar15datetimevarchar30varchar10
good:
idnamepricestatus
int auto_incrementvarchar20intvarchar10
sellers:
accountpassword
varchar20varchar20
  1. 先连接mysql:File/new/data source/mysql

  2. 再自动生成entity:jpa 之自动生成entity与repository相关数据库实体类_jpa entity生成-CSDN博客

  3. 再手动生成repository

推送项目到他人的github仓库

  1. 他人把我拉进colaborators

  2. 本地新建目录,git clone 他人的仓库.git

  3. 将项目文件全拉进新建的目录

  4. 在IDEA中,Git\new branch,Git\Push

  5. 成功

问题:

  1. 运行main方法,出现报错:

    Unable to resolve name [org.hibernate.dialect.MySQL5Dialect ] as strategy [org.hibernate.dialect.Dialect]

    解决:

    是application.properties中jpa的问题,将下面这行注释掉就成功了

    spring.jpa.properties.hibernate.dialect="org.hibernate.dialect.MySQL5Dialect"

问题:JDK版本不同导致我运行他人项目报错

在pom.xml中修改完jdk之后还是报错:

需要发行版21

解决:

rebuild然后invalidate cashes就好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值