SpringBoot简单入门

创建Spring Boot项目

自学笔记,如有理解不正确之处欢迎指出

安装插件

先去下载插件:Spring Boot Extension Pack

创建Spring Boot项目

使用快捷键:Ctrl+Shift+P调出命令窗口

  1. 创建Java 项目
  1. Spring Boot
  1. Maven Project
  1. 选择Spring Boot版,这里选择3.1.2
  1. 选择语言 Java
  1. Group Id、项目名称等
  1. 选择打包类型、Java版本等
  1. 选择需要引入的包,引入如下几个包即可满足web开发:
  • DevTools(代码修改热更新,无需重启)
  • Web(集成tomcat、SpringMVC)
  • Lombok(智能生成setter、getter、toString等接口,无需手动生成,代码更简介)
  • Thymeleaf (模板引擎)
  • MySQL (链接数据库)

点击Selected 6 dependencies进入下一步

  1. 选择目录路径
  1. Add to Workspace
  1. 重启vscode
  1. 删除我当前用不到的文件

==>

添加pom.xml依赖

  • mybatis-plus:MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变
  • Commons Lang:扩展Java的能力
  • druid:分布式的、支持实时多维OLAP 分析、列式存储的数据处理系统
  • fastjson:Java 对象与 JSON 格式之间相互转换
XML 复制代码
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3</version>
</dependency>
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.39</version>
</dependency>

设置配置文件

springboot_mybatisplus_quickstart\src\main\resources\application.yml

yaml 复制代码
spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/jdbc
        username: root
        password: 5508769123

注意:

启动引导文件SpringbootReggieApplication.java默认是放在springboot_reggie\src\main\java\com\mercurows\springboot_reggie\文件下的。而SpringBoot项目的Bean装配默认规则是根据DemoApplication类所在的包位置从上往下扫描。

当执行测试文件:springboot_reggie\src\test\java\com\mercurows\springboot_reggie\SpringbootReggieApplicationTests.java时:

java 复制代码
package com.mercurows.springboot_reggie;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.mercurows.dao.StudentDao;
import com.mercurows.domain.Student;

@SpringBootTest()
class SpringbootReggieApplicationTests {
	@Autowired
	private StudentDao studentDao;

	@Test
	void testSave() {
		Student student = new Student();
		student.setNameCh("小米米");
		student.setNameEn("xiaomimi");
		student.setSex("男");
		student.setAge(16);
		studentDao.insert(student);
		System.out.println("testSave");
	}
}

会弹出创建Bean错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.mercurows.springboot_reggie.SpringbootReggieApplicationTests': Unsatisfied dependency expressed through field 'studentDao': No qualifying bean of type 'com.mercurows.dao.StudentDao' available: expected at least 1 bean which qualifies as autowire candidate.

这时需要我们将文件SpringbootReggieApplication.java移动到:springboot_reggie\src\main\java\com\mercurows\下即可

相关推荐
张紫娃6 分钟前
Spring @Scope, @Lazy, @DependsOn, @Required, @Lookup
java·后端·spring
简诚7 分钟前
java实现RabbitMQ消息发送和接收功能(包含测试)
java·rabbitmq·java-rabbitmq
是梦终空10 分钟前
JAVA毕业设计227—基于SpringBoot+hadoop+spark+Vue的大数据房屋维修系统(源代码+数据库)
hadoop·spring boot·spark·vue·毕业设计·源代码·大数据房屋维修系统
結城1 小时前
Spring Security如何拿到登录用户的信息
java·spring·mybatis
GalaxyPokemon2 小时前
LeetCode - 2. 两数相加
java·前端·javascript·算法·leetcode·职场和发展
AKAMAI2 小时前
微服务架构的核心优势解析
后端·云原生·云计算
dualven_in_csdn5 小时前
搞了两天的win7批处理脚本问题
java·linux·前端
你的人类朋友5 小时前
✍️【Node.js程序员】的数据库【索引优化】指南
前端·javascript·后端
C++chaofan7 小时前
74. 搜索二维矩阵
java·算法·leetcode·矩阵
诺浅8 小时前
AWS S3 SDK FOR JAVA 基本使用及如何兼容七牛云
java·spring boot·aws