如何使用IntelliJ IDEA搭建MyBatis-Plus框架并连接MySQL数据库

 更新时间:2023年11月28日 10:40:32   作者:STARBLOCKSHADOW  
这篇文章主要介绍了如何使用IntelliJ IDEA搭建MyBatis-Plus框架并连接MySQL数据库,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

0 准备工作

1 创建Maven项目

  • 打开 IntelliJ IDEA,选择 "File"→ “New” → “Project”。
  • 选择"Maven"作为项目类型,并设置项目名称、项目位置。
  • 设置Group Id和Artifact Id,点击"Create"创建项目。

2 配置Maven依赖

在pom.xml文件中添加SpringBoot和MyBatis-Plus等的依赖:

<?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>

使用Maven工具或IDEA的自动构建功能,下载依赖。

若出现如下错误:

那么点击Maven设置,选择Maven主路径为本地的Maven下载路径:

3 配置数据源

在application.yml文件中配置数据库连接等信息:

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

替换上面的示例中的your_database_nameyour_usernameyour_password为实际数据库中的信息和数据。

4 项目结构

项目结构如下图所示:

5 创建实体类

创建实体类(entity),例如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 创建数据访问层

创建数据访问层(mapper),例如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> {
}

创建对应的XML文件:

<?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 创建服务层

创建服务层(service)及其实现,例如StudentService.java:

Service层:

package com.z.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.z.entity.Student;
public interface StudentService extends IService<Student> {
}

Service实现层:

package com.z.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.z.entity.Student;
public interface StudentService extends IService<Student> {
}

8 创建Controller层

创建Controller层,处理业务逻辑,例如StudentController.java(以返回数据列表为例):

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 启动项目

编写Main.java运行项目,并通过IDEA的启动按钮启动项目:

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 使用Postman测试接口

在MySQL数据库中新建一个数据表student,其中存放几条测试数据:

打开Postman,新建一个Get请求,并输入对应Controller中的请求URL进行测试,测试结果如下:

前端界面可通过该接口展示数据表中的数据。

到此这篇关于使用IntelliJ IDEA搭建SSM(MyBatis-Plus)框架并连接MySQL数据库的文章就介绍到这了,更多相关idea搭建SSM框架内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Struts2学习笔记(1)-入门教程

    Struts2学习笔记(1)-入门教程

    本文是一个Struts2的简单入门教程,比较简单,希望能给大家做一个参考。
    2016-06-06
  • SpringBoot实现接口统一前缀

    SpringBoot实现接口统一前缀

    本文主要介绍了SpringBoot实现接口统一前缀,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • java中的三种取整函数总结

    java中的三种取整函数总结

    下面小编就为大家带来一篇java中的三种取整函数总结。希望对大家有所帮助。一起跟随小编过来看看吧,祝大家游戏愉快哦
    2016-11-11
  • Java和Ceylon对象的构造和验证

    Java和Ceylon对象的构造和验证

    这篇文章主要为大家详细介绍了Java和Ceylon对象的构造和验证,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • 一文带你深入了解Guava的缓存机制

    一文带你深入了解Guava的缓存机制

    缓存在现代编程中的作用非常大,它能提高应用性能,减少数据库压力,简直就是性能优化的利器,本文主要来和大家聊聊Google Guava的缓存机制,感兴趣的小伙伴可以了解下
    2023-12-12
  • RabbitMQ进阶之消息可靠性详解

    RabbitMQ进阶之消息可靠性详解

    这篇文章主要介绍了RabbitMQ进阶之消息可靠性详解,abbitmq消息的投递过程中,怎么确保消息能不丢失,这是一个很重要的问题,哪怕我们做了Rabbitmq持久化,也不能保证我们的业务消息不会被丢失,需要的朋友可以参考下
    2023-08-08
  • Spring boot 基本部署方式

    Spring boot 基本部署方式

    SpringBoot部署也是非常简单,需要把打包输出的包由jar改为war。具体部署方式大家参考下本文
    2017-08-08
  • 详解Java程序启动时-D指定参数是什么

    详解Java程序启动时-D指定参数是什么

    java服务启动的时候,都会指定一些参数,下面这篇文章主要给大家介绍了关于Java程序启动时-D指定参数是什么的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • SpringBoot自动配置的原理详解

    SpringBoot自动配置的原理详解

    这篇文章主要介绍了SpringBoot自动配置的原理详解,本节更详细地介绍了如何使用 Spring Boot,它涵盖了诸如构建系统、自动配置以及如何运行应用程序等主题,我们还介绍了一些 Spring Boot 最佳实践,需要的朋友可以参考下
    2023-09-09
  • Java利用AlphaComposite类合并图像

    Java利用AlphaComposite类合并图像

    这篇文章主要介绍了Java利用AlphaComposite类合并图像,帮助大家更好的利用Java处理图像,感兴趣的朋友可以了解下
    2020-10-10

最新评论