Restful API应用实例

本文详细介绍了如何使用SpringBoot和MyBatis框架设计和实现Restful风格的API,包括用户数据的操作,如增删改查,并通过Postman进行了测试验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇文章已经说过如何定义一个Restful API,Restful风格最大的特点就是便于分类,提高URL的美观程度,同时很大程度上又能防止API的重复出错。

这里我们以对用户数据操作进行举例,用户表中只有id,username,password三个字段,设计API用的框架是SpringBoot,ORM框架用的是MyBatis,配置不再赘述,请转到之前的文章。

定义Mapper

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.demo.bean.User;

@Mapper
public interface UserDao {
	
	@Select("select * from tb_user")
	List<User> getUserList() throws Exception;
	
	@Select("<script>"
			+ "select * from tb_user where 1=1 "
			+ "<if test='username!=null'>"
			+ " and username like concat('%',#{username},'%') "
			+ "</if>"
			+ "<if test='password!=null'>"
			+ " and password like concat('%',#{password},'%')"
			+ "</if>"
			+ "</script>")
	List<User> getUserByFields(User user) throws Exception;
 	
	@Select("select * from tb_user where id=#{id}")
	User getUserById(int id) throws Exception;
	
	@Insert("insert into tb_user(username,password) values(#{username},#{password})")
	void insertUser(User user) throws Exception;
	
	@Insert("<script>"
			+ "insert into tb_user(username,password) values "
			+ "<foreach collection='users' item='item' index='index' separator=','>"
			+ " (#{item.username},#{item.password})"
			+ "</foreach>"
			+ "</script>")
	void insertMulUser(List<User> users) throws Exception;
	
	@Delete("delete from tb_user where id=#{id}")
	void delUserById(int id) throws Exception;
	
	@Delete("<script>"
			+ "<foreach collection='users' item='item' index='index' separator=','>"
			+ "delete from tb_user where id=#{item.id}"
			+ "</foreach>"
			+ "</script>")
	void delMulUser(List<Integer> users) throws Exception;
	
	@Update("update tb_user set username=#{user.username},password=#{user.password} where id=#{id}")
	void updateUserById(User user, int id) throws Exception;
	
}

Service层代码

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;

@Service
public class UserService {
	
	@Autowired
	private UserDao userDao;
	
	public List<User> getUserList(){
		List<User> users = null;
		try {
			users =  userDao.getUserList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}
	
	public User getUser(int id) {
		User user = null;
		try {
			user = userDao.getUserById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	
	public void insertUser(User u) {
		
		try {
			userDao.insertUser(u);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateUser(User u,int id) {
		try {
			userDao.updateUserById(u, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insertMulUser(List<User> users) {
		try {
			userDao.insertMulUser(users);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Controller层代码(Restful风格表现的核心)

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.User;
import com.example.demo.service.UserService;

@RestController
public class TestController {
	
	@Autowired
	private UserService userService;
	
	@GetMapping(value="/test")
	public String test() {
		return "测试成功";
	}
	
	@GetMapping(value="/user")
	public List<User> getUserList(){
		return userService.getUserList();
	}
	
	@GetMapping(value="/user/{id}")
	public User getUser(@PathVariable("id") int id) {
		return userService.getUser(id);
	}
	
	@PostMapping(value="/user")
	public String insertUser(User u) {
		userService.insertUser(u);
		return "成功";
	}	
	
	@PutMapping(value="/user")
	public String updateUser(User u,int id) {
		userService.updateUser(u, id);
		return "成功";
	}
	
	@PostMapping(value="/users")
	public String insertMulUser(List<User> users) {
		userService.insertMulUser(users);
		return "成功";
	}

}

测试

我们利用postman对API进行测试。

  1. 先清空表中数据。
    在这里插入图片描述
  2. 插入数据
    在这里插入图片描述
    在这里插入图片描述
  3. 按id查询单个用户
    在这里插入图片描述
  4. 查询表中所有用户
    在这里插入图片描述
    5.按id更新用户
    在这里插入图片描述
    在这里插入图片描述
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值