Springboot 项目搭建【保姆级教程】
目录一、创建项目二、配置 pom.xml 依赖文件三、设置 maven下载依赖四、配置 application.yml五、主启动类中添加注解1.MapperScan(com.example.*.mapper)2.Configration六、编写代码1. entity2. mapper3. service4. controller七、选择application类 点击运行八、搭建项目遇到的坑一、创建项目(阿里云地址 https://start.aliyun.com/ )注意springboot3版本需要Java17及以上springboot2版本一般用Java1.8。二、配置 pom.xml 依赖文件?xml version1.0 encodingUTF-8? 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 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.6.13/version relativePath/ !-- lookup parent from repository -- /parent groupIdcom.edward/groupId artifactIddemo-takeout/artifactId version0.0.1-SNAPSHOT/version namedemo-takeout/name descriptiondemo-takeout/description properties java.version1.8/java.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-boot-starter/artifactId version3.5.3/version /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.32/version /dependency dependency groupIdorg.freemarker/groupId artifactIdfreemarker/artifactId version2.3.32/version /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency /dependencies build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId /plugin plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-surefire-plugin/artifactId version2.5/version configuration skiptrue/skip /configuration /plugin /plugins resources resource directorysrc/main/resources/directory includes include**/*.yml/include include**/*.xml/include /includes filteringtrue/filtering /resource resource directorysrc/main/java/directory includes include**/*.yml/include include**/*.xml/include /includes filteringtrue/filtering /resource /resources /build /project三、设置 maven下载依赖配置好后点击 install 安装依赖四、配置 application.ymlserver: port: 8888 spring: datasource: username: root password: 8888888 url: jdbc:mysql://localhost:3306/sky_take_out?serverTimezoneGMT%2B8characterEncodingutf-8useSSLfalse driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: type-aliases-package: com.example.demotakeout.entity mapper-locations: classpath:mybatis/*.xml五、主启动类中添加注解1.MapperScan(com.example.*.mapper)2.Configration注意启动类的层级六、编写代码1. entity2. mapper如果启动类中没有添加MapperScan注解则Mapper中需要添加 RepositoryDishMapper.xml:!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd !--namespace 绑定一个对应的Mapper接口-- mapper namespacecom.example.demotakeout.mapper.DishMapper select idlist resultTypeDish select * from dish /select insert idinsertOne parameterTypeDish insert into dish(name, category_id, price, image, description, status, create_time, create_user, update_time, update_user) values (#{name},#{categoryId},#{price},#{image},#{description},#{status},#{createTime},#{createUser},#{updateTime},#{updateUser}) /insert /mapper3. service注意(service注解写在实现类中而不是interface中)4. controller[代码]package com.edward.system.controller; import com.edward.system.entity.User; import com.edward.system.service.impl.UserServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RestController; import java.util.List; RestController RequestMapping(/system/user) public class UserController { Autowired private UserServiceImpl userServiceImpl; GetMapping(/hello) public String hello() { return 你好世界; } GetMapping(/all) public ListUser getAllUser() { ListUser userList userServiceImpl.list(); return userList; } }七、选择application类 点击运行控制台上出现下方log时表示在 localhost:9999 上启动成功访问页面八、搭建项目遇到的坑1. springboot3以上要求jdk17及以上2. 建好项目后点开setting—maven手动更改maven为阿里云并选择本地maven中相应的配置文件和仓库3. 点击install进行依赖项下载4. 下载完毕后显示build success但是启动类和test类里都没有启动按钮原因注解未生效5. 注解不生效原因idea调为了省电模式6. 关掉省电模式Power Save Mode7. 彻底清除缓存并重启file - Invalidate Caches/Restart8. 点击启动按钮后服务未正常启动原因约定大于配置启动类里没有configuration注解要yml配置文件等目录要放在规定好的层级里9. 没有查出数据原因service注解在实现类而不是interface里10. 当idea右侧没有Database选项时可以通过如下步骤添加然后就有了