目录
构建项目(IDEA2023.1.2,JDK21,SpringBoot3.3.3)
这里我们利用MybatisX插件生成我们所需要的实体类、数据访问层以及服务层
以及实现类(利用MyBatis-Plus省去我们编写sql语句)
前期准备
构建项目(IDEA2023.1.2,JDK21,SpringBoot3.3.3)
添加启动器
<!--MyBatis-Plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.6</version>
</dependency>
<!-- Druid依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
Model准备
这里我们利用MybatisX插件生成我们所需要的实体类、数据访问层以及服务层
注意选择MyBatis-Plus3以及Lombok
然后再在service接口中定义我们所需要的方法
package com.ktjiaoyu.crm.service;
import com.ktjiaoyu.crm.entity.User;
import java.util.List;
/**
* @author cuishujian
* @date 2024/9/2
*/
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();
}
以及实现类(利用MyBatis-Plus省去我们编写sql语句)
package com.ktjiaoyu.crm.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.mapper.UserMapper;
import com.ktjiaoyu.crm.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author cuishujian
* @date 2024/9/2
*/
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public User login(String usrName, String usrPassword) {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("usr_name",usrName);
wrapper.eq("usr_password",usrPassword);
List<User> list = userMapper.selectList(wrapper);
return list.get(0);
}
@Override
public int addUser(User user) {
return userMapper.insert(user);
}
@Override
public int deleteUser(Long usrId) {
return userMapper.deleteById(usrId);
}
@Override
public int updateUser(User user) {
return userMapper.updateById(user);
}
@Override
public User getUser(Long usrId) {
return userMapper.selectById(usrId);
}
@Override
public List<User> findAllUsers() {
return userMapper.selectList(null);
}
}
控制器开发
package com.ktjiaoyu.crm.controller;
import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author cuishujian
* @date 2024/9/3
*/
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping("/user")
public List<User> findAllUsers(){
List<User> list = userService.findAllUsers();
return list;
}
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Long usrId){
User user = userService.getUser(usrId);
return user;
}
@PostMapping("/user")
public void addUser(User user){
userService.addUser(user);
}
@PutMapping("/user")
public void updateUser(User user){
userService.updateUser(user);
}
@DeleteMapping("/user/{id}")
public void deleteUser(@PathVariable("id") Long usrId){
userService.deleteUser(usrId);
}
}
到这里我们就基本完成了Web项目的后端代码的开发,下面就进入我们的测试环节
测试
这里我们使用SpringBoot中对Web测试的一种方案:MockMvc。MockMvc实现了对Http请求的模拟,能够直接使用网络的形式,转换到Controller的调用,这样可以使得测试速度更快、不依赖网络环境,而且提供了一套验证的工具,可以使得请求的验证统一而且更方便。
下面是MockMvc测试类的基本结构
package com.ktjiaoyu.crm;
import jakarta.annotation.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;
/**
* @author cuishujian
* @date 2024/9/3
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserControllerTester {
@Resource
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup(){
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testGetUser() throws Exception{
String mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/2")).andReturn().getResponse().getContentAsString();
System.out.println("Result===" + mvcResult);
}
@Test
public void testAddUser() throws Exception{
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("usrName","RESTful");
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());
}
}
testGetUser测试结果
testAddUser测试结果
最后再告诉大家一个Web开发中非常实用的东西,热部署,而热部署的实现再IDEA2023.2.1中的实现也非常简单只需要添加maven依赖以及设置一下就可以实现了
热部署
maven依赖
<!-- 热加载 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
修改设置
标签:Web,SpringBoot,springframework,Plus,user,org,import,public,User From: https://blog.csdn.net/cuishujian_2003/article/details/142234090