1. 构建项目:
2.添加依赖
<!-- SpringBoot热部署支持(开发阶段使用)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--添加jst1标签库依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--添加jsp引擎依赖,Spring Boot内置tomcat没有这个依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!--Web开发依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
3.Model 准备
public interface UserService {
User login(String usrName, String usrPassword);
int addUser(User user);
int deleteUser(Long usrId);
int updateUser(User user);
User getUser(Long usrId);
List<User> findAllUsers();
}
4.创建控制器
@RequestMapping(value = "/getUser",method = RequestMethod.GET)
public User getUser(Long usrId){
User user = userService.getUser(usrId);
return user;
}
5.测试方法
@Resource
private WebApplicationContext wac; // 注入 web 环境
private MockMvc mockMvc; // 注入 MockMvc
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); // 初始化 MockMvc
}
//根据Id查询
@Test
public void testGetUser() throws Exception {
String mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/2")) // 测试地址
.andReturn().getResponse().getContentAsString(); // 返回响应主体内容
System.out.println("Result==" + mvcResult);
}
//新增(post请求)
@Test
public void testAddUser() throws Exception {
//构造请求参数
final MultiValueMap<String , String> params = new LinkedMultiValueMap<>();
params.add("usrName","SB");
params.add("usrPassword","123456");
params.add("usrRoleId","2");
params.add("usrFlag","1");
mockMvc.perform(MockMvcRequestBuilders.post("/user").params(params))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print());//打印响应结果
}
热部署配置:在修改代码后,无需手动重启整个应用程序即可看到修改后的效果。例如,在 Java Web 开发中,对一个 Servlet 类进行了方法的修改,如果开启了热部署,无需重新启动 Tomcat 服务器,节省了从停止到重新启动服务器的等待时间,让开发者可以更快速地进行代码调试和验证。
添加热部署的依赖
<!-- 配置热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
在 application.xml或application.properties文件配置热部署配置
总结:
大量减少了传统 Spring 项目中繁琐的 XML 配置文件。例如,在 application.properties 或 application.yml 文件中,仅需简单几行配置就能完成如数据库连接、端口号修改等常见设置,如server.port = 8081可轻松更改应用的启动端口。
通过 Spring Initializr 可以轻松创建基于 Spring Boot 的 Web 项目,可根据需求选择 Web、Thymeleaf、JPA 等相关依赖,极大地简化了项目搭建的初始步骤。