一:使用Maven创建springboot项目
1.创建Maven项目,不使用模板。
2.创建好之后再pom.xml中加入springboot父项目的依赖
<!-- 引入springboot的父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
</parent>
3.引入springboot-web项目的起步依赖
<dependencies>
<!-- 引入springboot-web起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
4.在java目录下创建包,在包下创建一个java类,并且编写main函数。在创建的类名上加入注解
@SpringBootApplication,在main函数中加入代码
SpringApplication.run(SpringBoot02.class,args);
“SpringBoot02.class”:是当前java类
加入注解之后,当前java类就是springboot项目的启动类。
在启动类的同包下创建controller包,在controller包下创建HelloController类,并加上注解@RestController注解声明这个类。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLxEEVPpXRU5UMFpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL3kDN5ATN0QTM4EjNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
启动主函数,在浏览器地址栏访问 localhost:8080/hello。
二:springboot项目中的配置文件类型
springboot项目中有两种配置文件,配置文件必须是application开头的,否则均不是springboot配置文件。
1.application.properties:key=value形式,无层级形式。
2.application.yml:层级形式,类似目录。并且在值和冒号中间有空格,如果不加会出错,无法识别
正确格式: 错误格式:
注:一个springboot项目只有一个主配置当application.properties和application.yml同时存在的时候,只有 application.properties 生效。
三:springboot项目读取配置文件中的数据
springboot项目从配置文件中读取数据的方式有两种。
[email protected]注解:使用@Value注解的时候,括号内要写入配置文件中声明的变量名。并且使用@Value注解的时候,Map类型的数据以及自定义类型数据无法进行赋值,所以@Value注解最好只是用在基本数据类型上
[email protected]注解:@ConfigurationProperties注解需要在括号内声明前缀。
例如在配置文件中声明 【student.name studen.age studen.sex】 的时候,使用@ConfigurationProperties(prefix = "student")。
当配置文件中声明的是 【a.name b.age c.sex】时则无法使用当前注解,因为前缀均不相同。
四:springboot注册web三大组件
1.注册servlet组件
首先创建一个servlet。再创建一个配置文件类,在配置文件类上使用注解 @Configuration 声明该类是一个配置类
浏览器地址栏输入 localhost:8080/my。会进入到创建的MyServlet类的get方法中。
2.注册filter组件
五:整合Mybatis
使用springboot整合mybatis,需要加入mybatis整合springboot依赖,mysql依赖,数据库连接池阿里云druid依赖。
1.加入依赖
<!-- 阿里云druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- springboot整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2.配置springboot配置类
# 配置数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/aaa
spring.datasource.username=root
spring.datasource.password=803929.0
# 配置mapper.xml文件位置
mybatis.mapper-locations=classpath:mapper/*.xml
3.创建entity包,并在包下创建User对象
package com.hyn.entity;
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private String password;
}
4.创建dao包,并且创建UserDao接口。在resource下创建UserMapper.xml文件。
dao接口的@Mapper注解是吧当前dao接口交给spring容器进行管理。
@Mapper注解与主启动类上的@MapperScan注解作用相同。
package com.hyn.dao;
import com.hyn.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserDao {
List<User> doQueryAll();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace写当前xml对应的dao类的全限定名 -->
<mapper namespace="com.hyn.dao.UserDao">
<select id="doQueryAll" resultType="com.hyn.entity.User">
select * from t_user
</select>
</mapper>
5.创建service包,创建service接口以及对应的接口实现类
package com.hyn.service;
import com.hyn.entity.User;
import java.util.List;
public interface UserService {
List<User> doQueryAll();
}
package com.hyn.service;
import com.hyn.dao.UserDao;
import com.hyn.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> doQueryAll() {
return userDao.doQueryAll();
}
}
6.创建UserController类。
package com.hyn.controller;
import com.hyn.entity.User;
import com.hyn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getAll")
private List<User> doQueryAll() {
return userService.doQueryAll();
}
}
7.运行主启动类的main函数。浏览器地址栏访问 http://localhost:8080/springboot-maven/getAll