问答题:(提交形式:文字解答+语音)
1、什么是SpringBoot?与Spring的区别是什么?
- springboot就是一个快速开发spring项目的脚手架
- spring配置复杂,繁琐,springboot简单快速,能大大提高开发效率、
- springboot只是spring本身的拓展、是开发部署和测试更加的方便
2、SpringBoot的4大功能是什么?
- 自动依赖 :简化pom文件的依赖
- 自动配置:简化第三方集成框架的初始化工作
- 快速启动:内置tomcat,可一键启动
- 统一监控:统一监控应用程序的状态
3、SpringBoot有那些核心属性配置文件?
- 注解配置: JavaConfig
- maven依赖:pom配置
- 属性配置:application.yml
- web配置:web.xml- springboot默认使用的是servlet3.0无需配置
4、SpringBoot属性配置文件的加载顺序是?(至少说3条)
- 父项目下的config文件下的配置文件优先于父项目根路径下的配置文件
- 父项目下的配置文件优先于子项目下的配置文件
- application.properties
- bootstrap.yml
- application.yml
- application.yaml
5、SpringBoot读取属性有几种方式?
//1.在属性加上@value("${要读取的属性值}")
@Value("${test.name}")
private String name;
//2.使用Environment的api获取属性
@Autowired
private Environment environment;
//3.在实体类上在注解
@ConfigurationProperties(prefix = "person"):读取批量的属性
6、如何搭建SpringBoot项目?
-
创建一个普通的maven工程
-
导入坐标
<!--springboot工程需要继承的父工程--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> </parent> <dependencies> <!--web开发的起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
-
springboot项目的入口类
/** * 引导类。 SpringBoot项目的入口 */ @SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class,args); } }
7、SpringBoot集成SpringMVC的步骤是?
- 导入mybatis-spring-boot-starter的依赖和相关web依赖。
- 创建启动类,用注解@SpringBootApplication:声明为一个SpringBoot启动类
8、SpringBoot集成Mybatis的步骤是?
- 创建普通maven工程
- 导入pom.xml文件
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--<scope>runtime</scope>-->
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
-
写接口mapper接口
@Mapper @Repository public interface UserMapper { /** * 查询所有用户 * @return 用户 */ @Select("select * from t_user ") List<User> findAll(); }
-
写对应的pojo
package com.itheima.pojo; import java.io.Serializable; /** * @author 陈朋 * @date 2021/1/18 0018 21:06 */ public class User implements Serializable { private Integer id; private String username; private String password; public User() { } public User(Integer id, String username, String password) { this.id = id; this.username = username; this.password = password; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
-
测试
package com.itheima.mapper; import com.itheima.pojo.User; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = UserMapper.class) class MybatisApplicationTests { @Autowired private UserMapper userMapper; @Test public void testFindAll() { List<User> list = userMapper.findAll(); System.out.println(list); } }
-
9、SpringBoot如何查找集成的三方框架有那些配置属性?
要集成的三方框架的对象名. 就能看到配置属性
10、SpringBoot集成Junit的步骤是?
package com.itheima.mapper;
import com.itheima.pojo.User;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserMapper.class)
class MybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testFindAll() {
List<User> list = userMapper.findAll();
System.out.println(list);
}
}
11、SpringBoot实现起步依赖的原理是?
借助maven的子父工程传递依赖实现
- dependencyManagement:被子工程选用的依赖
- dependencies:被子工程自动依赖
- pluginManagement:被子工程选用的依赖
12、SpringBoot实现快速启动的原理是?
- springboot内置tomcat
13、SpringBoot项目涉及的4大配置文件分别是?
- pom配置
- web配置(web.xml)
- 属性配置(properties/yml/yaml)
- Bean配置(JavaConfig)
14、如何使用SpringBoot提供的多环境配置?
一共分为三种环境
- dev:开发环境
- test:测试环境
- prod:生产环境
- 在配置文件中使用spring.profiles.active=环境名称
- 使用虚拟机命令激活,-Dspring.profiles.active=环境名称
- 使用命令行参数激活,–spring.profiles.active=环境名称
15、在SpringBoot中,如何扫描启动类包之外的类?
@ComponentScan(basePackages=“com.xx”)
@import
@Enable
启动类上方的注解
编程题:(提交形式:文字解答+录屏演示)
1、实现传智健康用户表(t_user)信息查询接口(直接使用主键id查询)
要求:1、使用SpringBoot技术实现以上需求;
2、使用Dockerfile制作包括jdk、该项目jar的镜像,并设置容器启动自动运行项目
3、Dao层使用myabtis的注解开发
-
导入pom.xml文件
<?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> <groupId>com.itheima</groupId> <artifactId>springoot-mvc</artifactId> <version>1.0.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jdbc的启动器,默认使用HikariCP连接池--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--不要忘记数据库驱动,因为springboot不知道我们使用的什么数据库,这里选择mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- 通用mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> </dependencies> </project>
UserMapper
package com.itheima.mapper; import com.itheima.pojo.User; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; /** * @author 陈朋 * @date 2021/1/19 0019 21:57 */ @Mapper @Repository public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> { /** * 查询所有用户 * * @return list集合 */ @Select("select * from t_user") List<User> findAll(); /** * 删除用户 * * @param id 用户id */ @Delete("delete * from t_user where id=#{id}") void deleteById(Integer id); /** * 根据id查询用户 * * @param id 用户的id * @return user对象 */ @Select("select * from t_user where id=#{id}") User findById(Integer id); }
User
package com.itheima.pojo; import java.io.Serializable; import java.util.Date; import java.util.HashSet; import java.util.Objects; import java.util.Set; /** * 用户 * @author 陈朋 */ public class User implements Serializable{ private Integer id; // 主键 private Date birthday; // 生日 private String gender; // 性别 private String username; // 用户名,唯一 private String password; // 密码 private String remark; // 备注 private String station; // 状态 private String telephone; // 联系电话 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getStation() { return station; } public void setStation(String station) { this.station = station; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } User user = (User) o; return Objects.equals(id, user.id) && Objects.equals(birthday, user.birthday) && Objects.equals(gender, user.gender) && Objects.equals(username, user.username) && Objects.equals(password, user.password) && Objects.equals(remark, user.remark) && Objects.equals(station, user.station) && Objects.equals(telephone, user.telephone) ; } @Override public int hashCode() { return Objects.hash(id, birthday, gender, username, password, remark, station, telephone); } }
UserService
package com.itheima.service; import com.itheima.mapper.UserMapper; import com.itheima.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * @author 陈朋 * @date 2021/1/19 0019 22:01 */ @Service @SuppressWarnings("all") public class UserService { @Autowired private UserMapper userMapper; public List<User> findAll() { return userMapper.findAll(); } public User findUserById(Integer id) { return userMapper.findById(id); } @Transactional(rollbackFor = Exception.class) public void delete() { userMapper.deleteById(1); } }
UserApplication
package com.itheima; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author 陈朋 * @date 2021/1/19 0019 21:31 */ @SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }
application.properties
# 映射端口 server.port=80 # 设置org.springframework包的日志级别为debug logging.level.org.springframework=debug # 连接四大参数 spring.datasource.url=jdbc:mysql://159.75.102.92:3306/springboot spring.datasource.username=root spring.datasource.password=root # 可省略,SpringBoot自动推断 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.hikari.idle-timeout=60000 spring.datasource.hikari.maximum-pool-size=30 spring.datasource.hikari.minimum-idle=10
打包jar上传到linux 进入docker-file编辑springbootmvc-dockerfile3
FROM java:8 MAINTAINER chenpeng<itheima@itcast> ADD springoot-mvc-1.0.0-SNAPSHOT.jar mvc.jar CMD java -jar mvc.jar # 构建镜像 docker build -f ./springbootmvc-dockerfile3 -t mvc:1.0 docker run -id -p 9004:8080 mvc .
疑惑:
完成今日作业过程中遇见的问题,可以记录到如下(描述完整错误信息、分析思路、解决方案)