[Java] Use IntelliJ IDEA to build the SSM (MyBatis-Plus) framework and connect to the MySQL database

0 preparation

  1. Download and install IntelliJ IDEA
  2. Download and install MySQL database
  3. Download and installPostman testing tool
  4. Create a MySQL database using Navicat

1 Create a Maven project

  1. Open IntelliJ IDEA and select "File" → "New" → "Project".
  2. Select "Maven" as the project type, and set the project name and project location.
  3. Set the Group Id and Artifact Id, and click "Create" to create the project.
    Insert image description here

2 Configure Maven dependencies

Add dependencies for SpringBoot, MyBatis-Plus, etc. in the pom.xml file:

<?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>
  <!-- 定义父项目,使用Spring Boot 的版本管理 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.17</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <!-- 项目的基本信息 -->
  <groupId>com.z</groupId>
  <artifactId>MySSM</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MySSM</name>
  <description>MySSM</description>
  <!-- 定义Java版本 -->
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <!-- Spring Boot Web Starter,包含了Spring MVC等 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- MyBatis Spring Boot Starter -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.3.1</version>
    </dependency>
    <!-- MySQL Connector Java -->
    <dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- Lombok,简化Java代码 -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <!-- Spring Boot Starter Test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- MyBatis-Plus Starter -->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.4.3</version>
    </dependency>
    <!-- Swagger Annotations -->
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-annotations</artifactId>
      <version>1.5.22</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <image>
            <builder>paketobuildpacks/builder-jammy-base:latest</builder>
          </image>
          <excludes>
            <exclude>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
            </exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

Use the Maven tool or IDEA's automatic build function to download dependencies.

If the following error occurs:
Insert image description here
Then click Maven Settings and select the Maven main path as the local Maven download path:
Insert image description here
Insert image description here

3 Configure data source

Configure database connection and other information in the application.yml file:

server:
  # 端口
  port: 8080

spring:
  # 数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/your_database_name?characterEncoding=utf-8
    username: your_username
    password: your_password
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss


mybatis-plus:
  # mapper文件映射路径
  mapper-locations: classpath*:mapper/*.xml
  configuration:
    # 打印SQL语句
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Replace your_database_name, your_username, your_password in the above example with the information and data in the actual database.

4 Project structure

The project structure is shown below:
Insert image description here

5 Create entity class

Create an entity class (entity), such as Student.java:

package com.z.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@TableName("student")
public class Student implements Serializable {
    
    
    private static final long serialVersionUID = 1L;

    /**id*/
    @TableId(type = IdType.AUTO)
    @ApiModelProperty(value = "id")
    private Integer id;

    @ApiModelProperty(value = "姓名")
    private String name;

    @ApiModelProperty(value = "性别")
    private String sex;

    @ApiModelProperty(value = "年龄")
    private Integer age;

    @ApiModelProperty(value = "专业")
    private String major;
}

6 Create the data access layer

Create a data access layer (mapper), such as StudentMapper.java:

package com.z.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.z.entity.Student;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    
    
}

Create the corresponding XML file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.z.mapper.StudentMapper">
</mapper>

7 Create service layer

Create the service layer (service) and its implementation, such as StudentService.java:

Service layer:

package com.z.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.z.entity.Student;

public interface StudentService extends IService<Student> {
    
    
}

Service implementation layer:

package com.z.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.z.entity.Student;
import com.z.mapper.StudentMapper;
import com.z.service.StudentService;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    
    

}

8 Create Controller layer

Create a Controller layer to process business logic, such as StudentController.java (take returning a data list as an example):

package com.z.controller;

import com.z.entity.Student;
import com.z.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/test")
public class StudentController {
    
    
    @Autowired
    private StudentService studentService;

    @GetMapping("/list")
    public List<Student> listStudent() {
    
    
        return studentService.list();
    }
}

9 Start the project

Write Main.java to run the project and start the project through IDEA's start button:

package com.z;

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

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

10 Use Postman to test the interface

  1. Create a new data table student in the MySQL database, which stores several pieces of test data:
    Insert image description here

  2. Open Postman, create a new Get request, and enter the request URL in the corresponding Controller for testing. The test results are as follows:
    Insert image description here
    The front-end interface can display the data in the data table through this interface.

Guess you like

Origin blog.csdn.net/qq_41084756/article/details/134637485