5分钟快速搭建SpringBoot3 + MyBatis-Plus工程/项目的实现示例
作者:玖年JN
环境
idea 2023.3.5jdk 17mysql 8
创建SpringBoot工程
创建SpringBoot工程,这里有两种方式可选,一种是使用idea
提供的Spring Initializr
自动创建,一种是通过Maven Archetype
手动创建
自动创建SpringBoot工程
使用Spring Initializr
创建,这里选择Maven
类型,JDK
和Java
选择17
,选择后点击Next
上方选择自己想要的spring boot
版本,下方在Web
栏勾选Spring Web
,选择后点击Create
在pom.xml
文件的右上角点击maven
图标刷新maven
依赖
刷新后,在工程名 + Application的文件中可以启动这个springBoot
项目
这里我们创建一个/hello/word
路径来做测试,在com.jiunian.springboot_mybatisplus_auto
下创建controller
包,并创建HelloController
类
@RestController @RequestMapping("/hello") public class HelloController { @RequestMapping("/world") public String helloWorld() { return "Hello World!"; } }
启动SpringbootMybatisplusAutoApplication
在下方的终端输出可以看出,项目启动在8080
端口的/
目录下
尝试访问,访问成功
手动创建SpringBoot工程
选择Maven Archetype
方式创建项目,在Archetype
处选择quickstart
选项,选择后点击Create
等待项目创建完成,修改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
类重构,改名为SpringBootMyBatisPlusManualApplication
,并将其内容修改为下方方式
package com.jiunian; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootMyBatisPlusManualApplication { public static void main( String[] args ) { // 第一个参数是当前类的.class SpringApplication.run(SpringBootMyBatisPlusManualApplication.class, args); } }
创建该项目的spring配置文件,在main
下新创建一个文件夹,resources
在resource目录下创建一个application.yaml
文件或application.properties
,没有修改配置需求时可以不写东西
最后,和自动创建一样,创建一个/hello/word
路径来做测试,在com.jiunian
下创建controller
包,并创建HelloController
类
@RestController @RequestMapping("/hello") public class HelloController { @RequestMapping("/world") public String helloWorld() { return "Hello World!"; } }
启动SpringbootMybatisplusManualApplication
在下方的终端输出可以看出,项目启动在8080
端口的/
目录下
尝试访问,访问成功
整合MyBatis-Plus
我这里使用手动创建的SpringBoot
工程继续整合MyBatis-Plus
,修改项目pom.xml
,导入mybatis-plus
,lombok
,mysql-connector-java
,其中lombok
是用于简化类开发,修改后,记得更新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
文件,配置数据库连接
spring: datasource: # 连接的数据库地址 url: jdbc:mysql:///mybatis?useSSL=false # 数据库用户名称 username: root # 该用户的密码 password: 123456
为了测试是否配置成功,我们创建数据库mybatis
create table mybatis
创建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, '女', '天津');
创建对应的pojo
类Dept
,在com.jiunian
下创建一个pojo
包并在其下创建Dept
类
package com.jiunian.pojo; import lombok.Data; // 这里使用了lombok的注解 // 能够自动生成所有属性对应的getters/setters、equals、hashCode和toString方法 // 如果不使用 @TableName 注解,MyBatis-Plus 默认会使用实体类的类名作为表名(默认是首字母小写,驼峰转下划线形式) @Data public class Dept { private int id; private String name; private int age; private String sex; private String address; }
如下图结构创建该类的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>接口,可以直接调用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
创建一个控制类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工程/项目的实现示例的文章就介绍到这了,更多相关SpringBoot3 MyBatis-Plus搭建内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- 解决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实现mysql乐观锁
- 浅谈Spring Boot、MyBatis、MyBatis-Plus 依赖版本对应关系
- Spring Boot Mybatis++ 2025详解