2026/2/12 2:09:09
网站建设
项目流程
网页设计制作与网站建设课程,广告网页制作模板,东莞桂城网站建设,新媒体运营岗位职责环境
idea 2023.3.5
jdk 17
mysql 8
创建SpringBoot工程
创建SpringBoot工程#xff0c;这里有两种方式可选#xff0c;一种是使用idea提供的Spring Initializr自动创建#xff0c;一种是通过Maven Archetype手动创建
自动创建SpringBoot工程
使用Spring Initializr创建这里有两种方式可选一种是使用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 groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.4.1/version /parent dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.4.1/version /parent groupIdcom.jiunian/groupId artifactIdspringboot_mybatisplus_manual/artifactId version1.0-SNAPSHOT/version packagingjar/packaging namespringboot_mybatisplus_manual/name urlhttp://maven.apache.org/url properties project.build.sourceEncodingUTF-8/project.build.sourceEncoding /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/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-pluslombokmysql-connector-java其中lombok是用于简化类开发修改后记得更新maven依赖project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.4.1/version /parent groupIdcom.jiunian/groupId artifactIdspringboot_mybatisplus_manual/artifactId version1.0-SNAPSHOT/version packagingjar/packaging namespringboot_mybatisplus_manual/name urlhttp://maven.apache.org/url properties project.build.sourceEncodingUTF-8/project.build.sourceEncoding /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.27/version /dependency dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-spring-boot3-starter/artifactId version3.5.8/version /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency /dependencies /project修改application.yaml文件配置数据库连接spring: datasource: # 连接的数据库地址 url: jdbc:mysql:///mybatis?useSSLfalse # 数据库用户名称 username: root # 该用户的密码 password: 123456为了测试是否配置成功我们创建数据库mybatiscreate 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、ServiceImplDeptMapperpackage com.jiunian.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.jiunian.pojo.Dept; import org.apache.ibatis.annotations.Mapper; Mapper // 继承BaseMapperT接口可以直接调用Mybatis-Plus提供的CRUD方法 public interface DeptMapper extends BaseMapperDept { }DeptServicepackage com.jiunian.service; import com.baomidou.mybatisplus.extension.service.IService; import com.jiunian.pojo.Dept; public interface DeptService extends IServiceDept { }DeptServiceImplpackage 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 ServiceImplDeptMapper, Dept implements DeptService { }在controller下为Dept创建一个控制类DeptControllerpackage 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来检查是否连接成功如下图所示连接成功