5分鐘快速搭建SpringBoot3?+?MyBatis-Plus工程/項目的實現(xiàn)示例
環(huán)境
idea 2023.3.5jdk 17mysql 8
創(chuàng)建SpringBoot工程
創(chuàng)建SpringBoot工程,這里有兩種方式可選,一種是使用idea
提供的Spring Initializr
自動創(chuàng)建,一種是通過Maven Archetype
手動創(chuàng)建
自動創(chuàng)建SpringBoot工程
使用Spring Initializr
創(chuàng)建,這里選擇Maven
類型,JDK
和Java
選擇17
,選擇后點擊Next
上方選擇自己想要的spring boot
版本,下方在Web
欄勾選Spring Web
,選擇后點擊Create
在pom.xml
文件的右上角點擊maven
圖標刷新maven
依賴
刷新后,在工程名 + Application的文件中可以啟動這個springBoot
項目
這里我們創(chuàng)建一個/hello/word
路徑來做測試,在com.jiunian.springboot_mybatisplus_auto
下創(chuàng)建controller
包,并創(chuàng)建HelloController
類
@RestController @RequestMapping("/hello") public class HelloController { @RequestMapping("/world") public String helloWorld() { return "Hello World!"; } }
啟動SpringbootMybatisplusAutoApplication
在下方的終端輸出可以看出,項目啟動在8080
端口的/
目錄下
嘗試訪問,訪問成功
手動創(chuàng)建SpringBoot工程
選擇Maven Archetype
方式創(chuàng)建項目,在Archetype
處選擇quickstart
選項,選擇后點擊Create
等待項目創(chuàng)建完成,修改pom.xml
文件添加springboot父類
并添加spring-boot-web
依賴,修改后需要點擊右上角maven
圖標刷新依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.1</version> </parent>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.1</version> </parent> <groupId>com.jiunian</groupId> <artifactId>springboot_mybatisplus_manual</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_mybatisplus_manual</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
將初始提供給我們的App
類重構(gòu),改名為SpringBootMyBatisPlusManualApplication
,并將其內(nèi)容修改為下方方式
package com.jiunian; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootMyBatisPlusManualApplication { public static void main( String[] args ) { // 第一個參數(shù)是當前類的.class SpringApplication.run(SpringBootMyBatisPlusManualApplication.class, args); } }
創(chuàng)建該項目的spring配置文件,在main
下新創(chuàng)建一個文件夾,resources
在resource目錄下創(chuàng)建一個application.yaml
文件或application.properties
,沒有修改配置需求時可以不寫東西
最后,和自動創(chuàng)建一樣,創(chuàng)建一個/hello/word
路徑來做測試,在com.jiunian
下創(chuàng)建controller
包,并創(chuàng)建HelloController
類
@RestController @RequestMapping("/hello") public class HelloController { @RequestMapping("/world") public String helloWorld() { return "Hello World!"; } }
啟動SpringbootMybatisplusManualApplication
在下方的終端輸出可以看出,項目啟動在8080
端口的/
目錄下
嘗試訪問,訪問成功
整合MyBatis-Plus
我這里使用手動創(chuàng)建的SpringBoot
工程繼續(xù)整合MyBatis-Plus
,修改項目pom.xml
,導入mybatis-plus
,lombok
,mysql-connector-java
,其中lombok
是用于簡化類開發(fā),修改后,記得更新maven
依賴
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.1</version> </parent> <groupId>com.jiunian</groupId> <artifactId>springboot_mybatisplus_manual</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_mybatisplus_manual</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.8</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
修改application.yaml
文件,配置數(shù)據(jù)庫連接
spring: datasource: # 連接的數(shù)據(jù)庫地址 url: jdbc:mysql:///mybatis?useSSL=false # 數(shù)據(jù)庫用戶名稱 username: root # 該用戶的密碼 password: 123456
為了測試是否配置成功,我們創(chuàng)建數(shù)據(jù)庫mybatis
create table mybatis
創(chuàng)建dept表
CREATE TABLE dept ( id INT NOT NULL PRIMARY KEY, name VARCHAR(255) NOT NULL, address VARCHAR(255) NOT NULL, age INT NOT NULL, sex VARCHAR(255) NOT NULL );
插入語句
INSERT INTO dept VALUES (1, '張三', 25, '男', '北京'); INSERT INTO dept VALUES (2, '李四', 26, '男', '上海'); INSERT INTO dept VALUES (3, '王五', 30, '女', '天津');
創(chuàng)建對應的pojo
類Dept
,在com.jiunian
下創(chuàng)建一個pojo
包并在其下創(chuàng)建Dept
類
package com.jiunian.pojo; import lombok.Data; // 這里使用了lombok的注解 // 能夠自動生成所有屬性對應的getters/setters、equals、hashCode和toString方法 // 如果不使用 @TableName 注解,MyBatis-Plus 默認會使用實體類的類名作為表名(默認是首字母小寫,駝峰轉(zhuǎn)下劃線形式) @Data public class Dept { private int id; private String name; private int age; private String sex; private String address; }
如下圖結(jié)構(gòu)創(chuàng)建該類的Mapper
、Service
、ServiceImpl
DeptMapper
package com.jiunian.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.jiunian.pojo.Dept; import org.apache.ibatis.annotations.Mapper; @Mapper // 繼承BaseMapper<T>接口,可以直接調(diào)用Mybatis-Plus提供的CRUD方法 public interface DeptMapper extends BaseMapper<Dept> { }
DeptService
package com.jiunian.service; import com.baomidou.mybatisplus.extension.service.IService; import com.jiunian.pojo.Dept; public interface DeptService extends IService<Dept> { }
DeptServiceImpl
package com.jiunian.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jiunian.mapper.DeptMapper; import com.jiunian.pojo.Dept; import com.jiunian.service.DeptService; import org.springframework.stereotype.Service; @Service public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService { }
在controller
下為Dept
創(chuàng)建一個控制類DeptController
package com.jiunian.controller; import com.jiunian.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/dept") public class DeptController { @Autowired private DeptService deptService; @RequestMapping("/getAll") public String getAll() { return deptService.list().toString(); } }
接下來訪問localhost:8080/dept/getAll
來檢查是否連接成功,如下圖所示,連接成功
到此這篇關于5分鐘快速搭建SpringBoot3 + MyBatis-Plus工程/項目的實現(xiàn)示例的文章就介紹到這了,更多相關SpringBoot3 MyBatis-Plus搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 解決mybatis-plus-boot-starter與mybatis-spring-boot-starter的錯誤問題
- Spring Boot 中整合 MyBatis-Plus詳細步驟(最新推薦)
- Spring?Boot?集成?MyBatis?全面講解(最新推薦)
- Spring Boot 中使用 Mybatis Plus的操作方法
- SpringBoot同時集成Mybatis和Mybatis-plus框架
- Springboot使用MybatisPlus實現(xiàn)mysql樂觀鎖
- 淺談Spring Boot、MyBatis、MyBatis-Plus 依賴版本對應關系
- Spring Boot Mybatis++ 2025詳解
相關文章
java 裝飾模式(Decorator Pattern)詳解及實例代碼
裝飾器模式(Decorator Pattern)允許向一個現(xiàn)有的對象添加新的功能,同時又不改變其結(jié)構(gòu)。這種類型的設計模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有的類的一個包裝2016-10-10Java靜態(tài)方法不能調(diào)用非靜態(tài)成員的原因分析
在Java中,靜態(tài)方法是屬于類的方法,而不是屬于對象的方法,它可以通過類名直接調(diào)用,無需創(chuàng)建對象實例,非靜態(tài)成員指的是類的實例變量和實例方法,它們需要通過對象實例才能訪問和調(diào)用,本文小編將和大家一起探討Java靜態(tài)方法為什么不能調(diào)用非靜態(tài)成員2023-10-10Java數(shù)據(jù)庫連接池連接Oracle過程詳解
這篇文章主要介紹了Java數(shù)據(jù)庫連接池連接Oracle過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09