上一篇文章已经说过如何定义一个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进行测试。
- 先清空表中数据。
- 插入数据
- 按id查询单个用户
- 查询表中所有用户
5.按id更新用户