欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何使用IntelliJ IDEA搭建MyBatis-Plus框架并連接MySQL數(shù)據(jù)庫(kù)

 更新時(shí)間:2023年11月28日 10:40:32   作者:STARBLOCKSHADOW  
這篇文章主要介紹了如何使用IntelliJ IDEA搭建MyBatis-Plus框架并連接MySQL數(shù)據(jù)庫(kù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

0 準(zhǔn)備工作

1 創(chuàng)建Maven項(xiàng)目

  • 打開(kāi) IntelliJ IDEA,選擇 "File"→ “New” → “Project”。
  • 選擇"Maven"作為項(xiàng)目類(lèi)型,并設(shè)置項(xiàng)目名稱(chēng)、項(xiàng)目位置。
  • 設(shè)置Group Id和Artifact Id,點(diǎn)擊"Create"創(chuàng)建項(xiàng)目。

2 配置Maven依賴(lài)

在pom.xml文件中添加SpringBoot和MyBatis-Plus等的依賴(lài):

<?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>
  <!-- 定義父項(xiàng)目,使用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>
  <!-- 項(xiàng)目的基本信息 -->
  <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,簡(jiǎn)化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的自動(dòng)構(gòu)建功能,下載依賴(lài)。

若出現(xiàn)如下錯(cuò)誤:

那么點(diǎn)擊Maven設(shè)置,選擇Maven主路徑為本地的Maven下載路徑:

3 配置數(shù)據(jù)源

在application.yml文件中配置數(shù)據(jù)庫(kù)連接等信息:

server:
  # 端口
  port: 8080
spring:
  # 數(shù)據(jù)源配置
  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語(yǔ)句
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

替換上面的示例中的your_database_nameyour_username、your_password為實(shí)際數(shù)據(jù)庫(kù)中的信息和數(shù)據(jù)。

4 項(xiàng)目結(jié)構(gòu)

項(xiàng)目結(jié)構(gòu)如下圖所示:

5 創(chuàng)建實(shí)體類(lèi)

創(chuàng)建實(shí)體類(lèi)(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 = "專(zhuān)業(yè)")
    private String major;
}

6 創(chuàng)建數(shù)據(jù)訪問(wèn)層

創(chuàng)建數(shù)據(jù)訪問(wèn)層(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> {
}

創(chuàng)建對(duì)應(yīng)的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 創(chuàng)建服務(wù)層

創(chuàng)建服務(wù)層(service)及其實(shí)現(xiàn),例如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實(shí)現(xiàn)層:

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

8 創(chuàng)建Controller層

創(chuàng)建Controller層,處理業(yè)務(wù)邏輯,例如StudentController.java(以返回?cái)?shù)據(jù)列表為例):

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 啟動(dòng)項(xiàng)目

編寫(xiě)Main.java運(yùn)行項(xiàng)目,并通過(guò)IDEA的啟動(dòng)按鈕啟動(dòng)項(xiàng)目:

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測(cè)試接口

在MySQL數(shù)據(jù)庫(kù)中新建一個(gè)數(shù)據(jù)表student,其中存放幾條測(cè)試數(shù)據(jù):

打開(kāi)Postman,新建一個(gè)Get請(qǐng)求,并輸入對(duì)應(yīng)Controller中的請(qǐng)求URL進(jìn)行測(cè)試,測(cè)試結(jié)果如下:

前端界面可通過(guò)該接口展示數(shù)據(jù)表中的數(shù)據(jù)。

到此這篇關(guān)于使用IntelliJ IDEA搭建SSM(MyBatis-Plus)框架并連接MySQL數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)idea搭建SSM框架內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Struts2學(xué)習(xí)筆記(1)-入門(mén)教程

    Struts2學(xué)習(xí)筆記(1)-入門(mén)教程

    本文是一個(gè)Struts2的簡(jiǎn)單入門(mén)教程,比較簡(jiǎn)單,希望能給大家做一個(gè)參考。
    2016-06-06
  • SpringBoot實(shí)現(xiàn)接口統(tǒng)一前綴

    SpringBoot實(shí)現(xiàn)接口統(tǒng)一前綴

    本文主要介紹了SpringBoot實(shí)現(xiàn)接口統(tǒng)一前綴,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • java中的三種取整函數(shù)總結(jié)

    java中的三種取整函數(shù)總結(jié)

    下面小編就為大家?guī)?lái)一篇java中的三種取整函數(shù)總結(jié)。希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧,祝大家游戲愉快哦
    2016-11-11
  • Java和Ceylon對(duì)象的構(gòu)造和驗(yàn)證

    Java和Ceylon對(duì)象的構(gòu)造和驗(yàn)證

    這篇文章主要為大家詳細(xì)介紹了Java和Ceylon對(duì)象的構(gòu)造和驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 一文帶你深入了解Guava的緩存機(jī)制

    一文帶你深入了解Guava的緩存機(jī)制

    緩存在現(xiàn)代編程中的作用非常大,它能提高應(yīng)用性能,減少數(shù)據(jù)庫(kù)壓力,簡(jiǎn)直就是性能優(yōu)化的利器,本文主要來(lái)和大家聊聊Google?Guava的緩存機(jī)制,感興趣的小伙伴可以了解下
    2023-12-12
  • RabbitMQ進(jìn)階之消息可靠性詳解

    RabbitMQ進(jìn)階之消息可靠性詳解

    這篇文章主要介紹了RabbitMQ進(jìn)階之消息可靠性詳解,abbitmq消息的投遞過(guò)程中,怎么確保消息能不丟失,這是一個(gè)很重要的問(wèn)題,哪怕我們做了Rabbitmq持久化,也不能保證我們的業(yè)務(wù)消息不會(huì)被丟失,需要的朋友可以參考下
    2023-08-08
  • Spring boot 基本部署方式

    Spring boot 基本部署方式

    SpringBoot部署也是非常簡(jiǎn)單,需要把打包輸出的包由jar改為war。具體部署方式大家參考下本文
    2017-08-08
  • 詳解Java程序啟動(dòng)時(shí)-D指定參數(shù)是什么

    詳解Java程序啟動(dòng)時(shí)-D指定參數(shù)是什么

    java服務(wù)啟動(dòng)的時(shí)候,都會(huì)指定一些參數(shù),下面這篇文章主要給大家介紹了關(guān)于Java程序啟動(dòng)時(shí)-D指定參數(shù)是什么的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • SpringBoot自動(dòng)配置的原理詳解

    SpringBoot自動(dòng)配置的原理詳解

    這篇文章主要介紹了SpringBoot自動(dòng)配置的原理詳解,本節(jié)更詳細(xì)地介紹了如何使用 Spring Boot,它涵蓋了諸如構(gòu)建系統(tǒng)、自動(dòng)配置以及如何運(yùn)行應(yīng)用程序等主題,我們還介紹了一些 Spring Boot 最佳實(shí)踐,需要的朋友可以參考下
    2023-09-09
  • Java利用AlphaComposite類(lèi)合并圖像

    Java利用AlphaComposite類(lèi)合并圖像

    這篇文章主要介紹了Java利用AlphaComposite類(lèi)合并圖像,幫助大家更好的利用Java處理圖像,感興趣的朋友可以了解下
    2020-10-10

最新評(píng)論