返回

用 Eclipse 创建一个 Spring Boot 项目(连接 MySQL,简单使用 MyBatis)

后端

当然可以,以下是用 Eclipse 创建一个 Spring Boot 项目(连接 MySQL,简单使用 MyBatis)的步骤:

  1. 创建一个 Spring Boot 项目

    • 打开 Eclipse,选择 File > New > Spring Starter Project。
    • 在弹出的对话框中,选择 Spring Boot 版本,并输入项目名称。
    • 在 Dependencies 选项卡中,搜索并添加 Spring Boot JDBC、Spring Boot Data JPA 和 MySQL 驱动依赖。
  2. 配置 pom.xml 依赖

    • 在项目根目录下的 pom.xml 文件中,添加以下依赖:

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
      </dependency>
      
  3. 配置 application.properties

    • 在项目根目录下的 application.properties 文件中,添加以下配置:

      spring.datasource.url=jdbc:mysql://localhost:3306/database_name
      spring.datasource.username=username
      spring.datasource.password=password
      
    • 其中,database_name 是你的 MySQL 数据库名称,username 是你的 MySQL 用户名,password 是你的 MySQL 密码。

  4. 创建实体类

    • 在项目根目录下创建一个实体类包,例如 com.example.demo.entities

    • 在实体类包中,创建一个实体类,例如 User.java

      package com.example.demo.entities;
      
      import javax.persistence.*;
      
      @Entity
      public class User {
      
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Long id;
      
          private String name;
      
          private String email;
      
          // ... getters and setters
      }
      
  5. 创建实体类对应的服务接口

    • 在项目根目录下创建一个服务接口包,例如 com.example.demo.services

    • 在服务接口包中,创建一个实体类对应的服务接口,例如 UserService.java

      package com.example.demo.services;
      
      import com.example.demo.entities.User;
      
      public interface UserService {
      
          List<User> findAll();
      
          User findById(Long id);
      
          User save(User user);
      
          void delete(Long id);
      
          // ... other methods
      }
      
  6. 创建实体类对应的 MyBatis Mapper 接口

    • 在项目根目录下创建一个 MyBatis Mapper 接口包,例如 com.example.demo.mapper

    • 在 MyBatis Mapper 接口包中,创建一个实体类对应的 MyBatis Mapper 接口,例如 UserMapper.java

      package com.example.demo.mapper;
      
      import com.example.demo.entities.User;
      
      public interface UserMapper {
      
          List<User> findAll();
      
          User findById(Long id);
      
          int save(User user);
      
          int delete(Long id);
      
          // ... other methods
      }
      
  7. 将 MyBatis Mapper 接口注入到服务类中

    • 在服务类的构造函数中,将 MyBatis Mapper 接口注入到服务类中。

      public class UserService {
      
          private UserMapper userMapper;
      
          public UserService(UserMapper userMapper) {
              this.userMapper = userMapper;
          }
      
          // ... other methods
      }
      
  8. 在 Spring Boot 主类中启用 MyBatis

    • 在 Spring Boot 主类中,使用 @MapperScan 注解启用 MyBatis。

      @SpringBootApplication
      @MapperScan("com.example.demo.mapper")
      public class DemoApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(DemoApplication.class, args);
          }
      }
      
  9. 运行项目

    • 在 Eclipse 中,右键单击项目,然后选择 Run As > Java Application。
  10. 测试项目

    • 使用浏览器访问 http://localhost:8080/users,应该可以返回所有用户列表。
    • 使用浏览器访问 http://localhost:8080/users/1,应该可以返回 ID 为 1 的用户。

SEO优化

标题