news 2026/11/3 2:47:52

【idea搭建简易版springboot项目】

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【idea搭建简易版springboot项目】

idea搭建简易版springboot项目

    • 新建项目,如图所示
    • 添加接口测试一下
    • Swagger接口文档 测试Springboot接口
    • 添加数据库配置并连接数据库,以mysql为例

新建项目,如图所示



选择 Maven,然后点击 Next。

编辑项目名称,请注意:Location 路径中不能包含中文,否则运行时可能找不到主类。

新建完项目,在java文件夹下创建好文件夹目录,根据需要进行创建
编辑pom文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--【必加1】父工程,统一版本管理--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.15</version><relativePath/><!--lookup parent from repository--></parent><groupId>org.example</groupId><artifactId>aitest</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!--【必加2】web启动器(内置tomcat、springmvc)--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--【推荐加】测试依赖,单元测试,scope=test只在测试时生效--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><!--【必加3】打包插件!没有它打出来的jar不能直接java-jar运行--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

在controller文件夹外层创建主类AitestApplication

packagecom.ting.test;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;/** * @description:主类 * @author: 夏之小星星 * @create: 2026/10/30 14:39 */@SpringBootApplicationpublicclassAitestApplication{publicstaticvoidmain(String[]args){SpringApplication.run(AitestApplication.class,args);}}


刷新Maven,等待系统下载完依赖,没有报红

右上角选中Edit Configurations…

点击“+”号,选中Application

编辑项目名称,选择主类com.ting.test.AitestApplication,debug模式运行

项目运行成功,简易框架搭建完成

添加接口测试一下

在controller文件夹里新建一个测试类MyClass

packagecom.ting.test.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;/** * @description:测试 * @author: 夏之小星星 * @create: 2026/10/30 14:50 */@RestController@RequestMapping("/test")publicclassMyClass{@GetMapping(value="/helloworld")publicStringtestMethod(){System.out.println("Welcome to Springboot");return"This is helloworld";}}

浏览器地址栏输入接口访问地址 http://localhost:8080/test/helloworld
看到了返回的字符串,同时控制台有输出

接口测试完毕

Swagger接口文档 测试Springboot接口

1、添加依赖,刷新Maven

<dependency><groupId>io.springfox</groupId><artifactId>springfox-spring-web</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>

2、Controller类 添加注解
Controller类上添加注解:@Api(value =“类的说明”),需要在接口文档中显示的接口上面添加注解:@ApiOperation(value = “接口说明”),在io.swagger.annotations包下。

packagecom.ting.test.controller;importcom.ting.test.service.UserService;importio.swagger.annotations.Api;importio.swagger.annotations.ApiOperation;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjavax.annotation.Resource;/** * @description:测试 * @author: 夏之小星星 * @create: 2026/10/30 14:50 */@RestController@RequestMapping("/test")@Api(value="测试类")publicclassMyClass{@ResourceUserServiceuserService;@ApiOperation(value="测试接口")@GetMapping(value="/helloworld")publicStringtestMethod(){System.out.println("Welcome to Springboot");return"This is helloworld";}}

在resources文件夹下新建配置文件application.yml,在配置文件中添加配置

spring:mvc:pathmatch:matching-strategy:ant_path_matcher swagger:default-include-pattern:/.* title:com.ting.test version:1.0

com.ting.test是启动类所在目录,在启动类上添加注解@EnableSwagger2,此时Swagger已经配置完成可以访问了,项目默认端口号为8080,则Swagger接口文档的访问地址是:
http://localhost:8080/swagger-ui.html#
重启项目,访问接口文档

添加数据库配置并连接数据库,以mysql为例

1、pom文件中添加配置

<!--Mysql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.5</version></dependency><dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>${tk.mybatis.version}</version></dependency>

标签里添加<tk.mybatis.version>2.0.3</tk.mybatis.version>刷新Maven

<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><tk.mybatis.version>2.0.3</tk.mybatis.version></properties>

在application.yml中添加数据库配置

spring:datasource:url:jdbc:mysql://localhost:3306/数据库名?allowMultiQueries=true&userSSL=false&serverTimezone=UTC&characterEncoding=UTF-8username:用户名 password:密码 driver-class-name:com.mysql.cj.jdbc.Driver

2、在数据库中新建t_user表

DROPTABLEIFEXISTS`t_user`;CREATETABLE`t_user`(`id`int(0)NOTNULLAUTO_INCREMENTCOMMENT'用户主键',`username`varchar(20)CHARACTERSETutf8mb3COLLATEutf8mb3_general_ciNOTNULLCOMMENT'用户名',`age`int(0)NULLDEFAULTNULLCOMMENT'年龄',PRIMARYKEY(`id`)USINGBTREE)ENGINE=InnoDBAUTO_INCREMENT=4CHARACTERSET=utf8mb3COLLATE=utf8mb3_general_ci ROW_FORMAT=Dynamic;

创建UserDto实体类,在pom中添加依赖,

<!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>

在application.yml中添加配置

mybatis:# 指定别名设置的包为所有entity type-aliases-package:com.ting.test.entity configuration:map-underscore-to-camel-case:truemapper-locations:classpath*:mapper/*Mapper.xml

3、分别创建UserMapper,UserDao,UserService,UserServiceImpl,UserController

UserMapper

<?xml version="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.ting.test.dao.UserDao"><select id="selectByName"parameterType="java.lang.String"resultType="com.ting.test.entity.UserDto">select id,username,age from t_user where username=#{username}</select></mapper>

UserDao

packagecom.ting.test.dao;importcom.ting.test.entity.UserDto;importorg.apache.ibatis.annotations.Mapper;importorg.apache.ibatis.annotations.Param;importorg.apache.ibatis.annotations.Select;/** * @description: TODO * @author: 夏之小星星 * @create: 2026/10/30 16:29 */@MapperpublicinterfaceUserDaoextendstk.mybatis.mapper.common.Mapper<UserDto>{publicUserDtoselectByName(@Param("username")Stringusername);@Select("select * from t_user where id = #{id}")UserDtogetById(Integerid);}

UserService

packagecom.ting.test.service;importcom.ting.test.entity.UserDto;/** * @description: TODO * @author: 夏之小星星 * @create: 2026/10/30 16:32 */publicinterfaceUserService{publicUserDtogetById(Integerid);publicUserDtoselectByName(Stringusername);}

UserServiceImpl

packagecom.ting.test.service.impl;importcom.ting.test.dao.UserDao;importcom.ting.test.entity.UserDto;importcom.ting.test.service.UserService;importorg.springframework.stereotype.Service;importjavax.annotation.Resource;/** * @description: TODO * @author: 夏之小星星 * @create: 2026/10/30 16:32 */@ServicepublicclassUserServiceImplimplementsUserService{@ResourceUserDaouserDao;@OverridepublicUserDtogetById(Integerid){UserDtouser=userDao.getById(id);returnuser;}@OverridepublicUserDtoselectByName(Stringusername){returnuserDao.selectByName(username);}}

UserController

packagecom.ting.test.controller;importcom.ting.test.entity.UserDto;importcom.ting.test.service.UserService;importio.swagger.annotations.Api;importio.swagger.annotations.ApiOperation;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importjavax.annotation.Resource;/** * @description:测试用户 * @author: 夏之小星星 * @create: 2026/10/30 16:35 */@RestController@RequestMapping("/user")@Api(value="用户操作")publicclassUserController{@ResourceUserServiceuserService;@ApiOperation(value="根据id查找用户")@GetMapping(value="/searchById")publicStringsearchById(@RequestParam("id")Integerid){Stringresult="没有查到用户";UserDtouser=userService.getById(id);if(user!=null){result="姓名:"+user.getUsername()+",年龄:"+user.getAge();}returnresult;}@ApiOperation(value="根据姓名查找用户")@GetMapping(value="/searchByName")publicStringsearchByName(@RequestParam("name")Stringname){Stringresult="没有查到用户";UserDtouser=userService.selectByName(name);if(user!=null){result="姓名:"+user.getUsername()+",年龄:"+user.getAge();}returnresult;}}

4、Swagger中测试一下接口吧


先测试接口根据id查找用户

测试接口根据姓名查找用户

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/11/3 2:46:37

工业设备预测性维护声学方案推荐,3天上线

一、预测性维护为什么成了工厂的刚需 在工业 4.0 的语境里&#xff0c;"设备不能停、停机就要钱"是制造业最朴素的痛点。一台关键机组非计划停机一小时&#xff0c;损失往往以万元计&#xff1b;而一次严重的轴承失效&#xff0c;可能引发整条产线停摆。传统"坏…

作者头像 李华
网站建设 2026/11/3 2:45:59

小白程序员如何快速成长为高薪AI Engineer?

本文深入解析AI Engineer这一新兴岗位的内涵与市场需求&#xff0c;从岗位演变、技能要求到转型路径进行详细阐述。文章指出&#xff0c;AI Engineer的核心价值在于将现成的大模型转化为可靠的产品&#xff0c;而非模型训练。文章强调评测与集成的重要性&#xff0c;并提供了后…

作者头像 李华
网站建设 2026/11/3 2:44:21

OPRF应用之OPAQUE增强型口令认证密钥交换协议【4】

摘要本文是对RFC 9807所描述的增强型口令认证密钥交换协议OPAQUE的学习与梳理&#xff0c;包括&#xff1a;1&#xff09;协议整体执行流程、2&#xff09;注册方案和在线认证方案、3&#xff09;关键模块OPRF与3DH。文章保留了原标准的章节对应关系&#xff0c;方便对照RFC原文…

作者头像 李华
网站建设 2026/11/3 2:43:15

QML 字体度量与文本度量:FontMetrics 与 TextMetrics

目录一、文本排版中的基线概念二、FontMetrics&#xff1a;测量字体固有的尺寸属性三、TextMetrics&#xff1a;测量特定文本排版后的实际尺寸宽度与边界矩形属性的区别四、组件职责划分与适用场景对照五、关键开发注意事项与潜在缺陷拦截在进行界面排版和自定义控件设计时&…

作者头像 李华