最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

0 準備工作

1 創(chuàng)建Maven項目

  • 打開 IntelliJ IDEA,選擇 "File"→ “New” → “Project”。
  • 選擇"Maven"作為項目類型,并設置項目名稱、項目位置。
  • 設置Group Id和Artifact Id,點擊"Create"創(chuàng)建項目。

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的自動構建功能,下載依賴。

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

那么點擊Maven設置,選擇Maven主路徑為本地的Maven下載路徑:

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

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

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語句
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

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

4 項目結構

項目結構如下圖所示:

5 創(chuàng)建實體類

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

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

創(chuàng)建數(shù)據(jù)訪問層(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)建對應的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)建服務層

創(chuàng)建服務層(service)及其實現(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實現(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è)務邏輯,例如StudentController.java(以返回數(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 啟動項目

編寫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數(shù)據(jù)庫中新建一個數(shù)據(jù)表student,其中存放幾條測試數(shù)據(jù):

打開Postman,新建一個Get請求,并輸入對應Controller中的請求URL進行測試,測試結果如下:

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

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

相關文章

  • Java實現(xiàn)文件上傳的方法

    Java實現(xiàn)文件上傳的方法

    這篇文章主要為大家詳細介紹了Java實現(xiàn)文件上傳的方法,供大家參考,感興趣的朋友可以參考一下
    2016-05-05
  • Java之@TableField注解的用法解析

    Java之@TableField注解的用法解析

    MyBatis-Plus的@TableField注解用于控制實體類字段與數(shù)據(jù)庫表字段的映射關系,支持字段映射、忽略、插入和更新控制、自定義填充策略和類型轉換等
    2025-01-01
  • Java實現(xiàn)浪漫流星表白的示例代碼

    Java實現(xiàn)浪漫流星表白的示例代碼

    本文將利用Java語言實現(xiàn)浪漫流星表白,可以實現(xiàn)這些功能:播放音樂、自定義流星數(shù)量、飛行速度、光暈大小、流星大小,自定義表白話語,感興趣的可以學習一下
    2022-05-05
  • Java中的數(shù)組基礎知識學習教程

    Java中的數(shù)組基礎知識學習教程

    這篇文章主要介紹了Java中的數(shù)組基礎知識學習教程,文中同時也整理了Java對數(shù)字類型的支持狀況及Number類中的方法,需要的朋友可以參考下
    2016-02-02
  • 利用Java自寫一個生成ID的工具類

    利用Java自寫一個生成ID的工具類

    平時項目中只要涉及表,那么一定能接觸到眾多各式各樣的ID編號。本文將通過Java語言實現(xiàn)手寫一個ID生成工具類,需要的小伙伴可以參考一下
    2022-11-11
  • Java面試基礎之TCP連接以及其優(yōu)化

    Java面試基礎之TCP連接以及其優(yōu)化

    這篇文章主要給大家介紹了關于Java面試基礎之TCP連接以及其優(yōu)化的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09
  • springboot2.0和springcloud Finchley版項目搭建(包含eureka,gateWay,F(xiàn)reign,Hystrix)

    springboot2.0和springcloud Finchley版項目搭建(包含eureka,gateWay,F(xiàn)re

    這篇文章主要介紹了springboot2.0和springcloud Finchley版項目搭建(包含eureka,gateWay,F(xiàn)reign,Hystrix),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 關于Java實現(xiàn)HttpServer模擬前端接口調(diào)用

    關于Java實現(xiàn)HttpServer模擬前端接口調(diào)用

    這篇文章主要介紹了關于Java實現(xiàn)Http?Server模擬前端接口調(diào)用,Http?協(xié)議是建立在?TCP?協(xié)議之上的協(xié)議,所以能用?TCP?來自己模擬一個簡單的?Http?Server?當然是可以的,需要的朋友可以參考下
    2023-04-04
  • Java線程基本使用之如何實現(xiàn)Runnable接口

    Java線程基本使用之如何實現(xiàn)Runnable接口

    這篇文章主要介紹了Java線程基本使用之如何實現(xiàn)Runnable接口問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java代碼實現(xiàn)自動檢測并刪除Excel中的空白行與空白列

    Java代碼實現(xiàn)自動檢測并刪除Excel中的空白行與空白列

    無效數(shù)據(jù)不僅影響報表的美觀,更會嚴重干擾后續(xù)的數(shù)據(jù)分析,本文將介紹如何通過 Java 代碼自動檢測并刪除 Excel 中的空白行和空白列,有需要的小伙伴可以了解下
    2026-03-03

最新評論

新干县| 沙田区| 津市市| 丹东市| 云安县| 如皋市| 孟津县| 子长县| 迁安市| 凤翔县| 吴忠市| 青河县| 台北市| 巍山| 娱乐| 石河子市| 综艺| 瓮安县| 衡东县| 和顺县| 平山县| 林周县| 通化市| 瓮安县| 喀喇沁旗| 洛川县| 东阳市| 岐山县| 富阳市| 白河县| 龙口市| 昌江| 滁州市| 察雅县| 五常市| 岳阳市| 南投市| 栾城县| 平阳县| 中山市| 万宁市|