说完了前后端,来看看我们怎么写自己该写的吧,我主要说后端
原来的写法:
一套基本的crud (增加(Create)、读取(Read)、更新(Update)和删除(Delete))
service包下:
UserService:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.ls.service;
import com.ls.dao.UserDAO; import com.ls.domain.User; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import java.util.List;
@Service @Transactional public class UserService {
@Resource private UserDAO userDAO;
public User findubyid(String id){ return userDAO.findubyid(id); }
public List<User> findu(){ return userDAO.findu(); }
public void insert01(User user){ userDAO.insert01(user); }
public void put01(User user){ userDAO.updateByPrimaryKeySelective(user); }
public void delete01(Integer id){ System.out.println("serser的id"+id); userDAO.deleteByPrimaryKey(id); } }
|
utils包下:
Result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package com.ls.utils;
import lombok.Data;
import java.util.HashMap; import java.util.Map;
@Data public class Result {
private boolean success;
private Integer code;
private String msg;
private Map<String, Object> data = new HashMap<>();
public static Result ok() { Result result = new Result(); result.setCode(ResultCod.SUCCESS); result.setMsg("成功"); return result; }
public static Result error() { Result result = new Result(); result.setCode(ResultCod.FALUE); result.setMsg("失败"); return result; }
public Result data(String key, Object value) { this.data.put(key, value);
return this; }
public Result message(String msg) { this.setMsg(msg);
return this; } }
|
ResultCod:
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.ls.utils;
public class ResultCod {
public static final Integer SUCCESS = 20000; public static final Integer FALUE = 20001;
}
|
dao包下:
UserDAO:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.ls.dao;
import com.ls.domain.User; import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper public interface UserDAO extends tk.mybatis.mapper.common.Mapper<User> {
@Select("select * from tab_user where uid = #{id}") public User findubyid(@Param(value = "id")String id);
@Select("select * from tab_user ") public List<User> findu();
@Insert("insert into tab_user (uid, username, password, sex, birthday) values (#{uid},#{username},#{password},#{sex},#{birthday})") public void insert01(User user);
@Delete("delete from tab_user where uid=#{id}") public void delete01(@Param("id") Integer id); }
|
domain包下:
User:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.ls.domain;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import java.util.Date;
@Data @AllArgsConstructor @NoArgsConstructor @Table(name = "tab_user") public class User {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer uid; private String username; private String password; private String sex; private Date birthday;
}
|
controller:
UserController:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package com.ls.controller;
import com.ls.domain.User; import com.ls.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RequestMapping(path = "/user") public class UserController {
private final UserService userService;
@Autowired public UserController(UserService userService) { this.userService = userService; }
@GetMapping(value = "/{id}") public ResponseEntity<User> findubyid(@PathVariable(value = "id") String id) { System.out.println(id); User findubyid = userService.findubyid(id);
if (findubyid != null) { return ResponseEntity.ok(findubyid); } else { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } }
@GetMapping(value = "/all") public ResponseEntity<List<User>> searchAll() { List<User> findu = userService.findu();
return new ResponseEntity<>(findu, HttpStatus.OK); }
@PostMapping(value = "add01") public ResponseEntity<Void> add01(@RequestBody User user) { System.out.println(user);
userService.insert01(user);
return new ResponseEntity<>(HttpStatus.OK); }
@PutMapping(value = "put") public ResponseEntity<Void> put01(@RequestBody User user) { System.out.println(user); userService.put01(user); return new ResponseEntity<>(HttpStatus.OK); }
@DeleteMapping(value = "/{id}") public ResponseEntity<Void> delete01(@PathVariable(value = "id") Integer id) { System.out.println(id); userService.delete01(id);
return new ResponseEntity<>(HttpStatus.OK); } }
|
企业中写法:
新增类:
controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package com.ls.controller;
import com.ls.domain.User; import com.ls.service.UserService; import com.ls.utils.Result; 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.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RequestMapping(path = "/user2") public class User2Controller {
private final UserService userService;
@Autowired public User2Controller(UserService userService) { this.userService = userService; }
@GetMapping(value = "/{id}") public Result findubyid(@PathVariable(value = "id") String id) { System.out.println(id); User findubyid = userService.findubyid(id);
if (findubyid != null) { return Result.ok().data("data", findubyid); } else { return Result.error().message("未找到"); } }
@GetMapping(value = "/all") public Result findAll(){ List<User> findu = userService.findu(); return Result.ok().data("user",findu).message("成功了"); } }
|
utils:
Result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package com.ls.utils;
import lombok.Data;
import java.util.HashMap; import java.util.Map;
@Data public class Result {
private boolean success;
private Integer code;
private String msg;
private Map<String, Object> data = new HashMap<>();
public static Result ok() { Result result = new Result(); result.setCode(ResultCod.SUCCESS); result.setMsg("成功"); return result; }
public static Result error() { Result result = new Result(); result.setCode(ResultCod.FALUE); result.setMsg("失败"); return result; }
public Result data(String key, Object value) { this.data.put(key, value);
return this; }
public Result message(String msg) { this.setMsg(msg);
return this; } }
|
ResultCod:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.ls.utils;
public class ResultCod {
public static final Integer SUCCESS = 20000; public static final Integer FALUE = 20001;
}
|
相对于原来的写法,只是多了一个步骤,用包装类将返回值封装起来,实际效果是,让前端永远只能看到返回为200的响应值,但是真正的问题,我们会用实体类封装好,最后返回给前端调用我们接口的人,但是这样仍有弊端:
弊端:
工具类中的方法,如果直接定义了另一个类写一些常量作为状态码,其实并不妥善,因为你可以放一个常量 20000,那就可以放一个别的数字,多少存在了隐患,所以最好用枚举
改进:
新增包:
enumcl:
ResultCode
1 2 3 4 5 6 7 8 9 10 11 12
| package com.ls.enumcl;
public enum ResultCode { SUCCESS, FALUE
}
|
这就是枚举类,我也是第一次真正用
ResultEnum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| package com.ls.enumcl;
import com.ls.utils.ResultCod; import lombok.Data;
import java.util.HashMap; import java.util.Map;
@Data public class ResultEnum {
private String msg; private ResultCode code;
private Map<String, Object> data = new HashMap<>();
public static ResultEnum ok() { ResultEnum result = new ResultEnum(); result.setCode(ResultCode.SUCCESS); result.setMsg("成功"); return result; }
public static ResultEnum error() { ResultEnum result = new ResultEnum(); result.setCode(ResultCode.FALUE); result.setMsg("失败"); return result; }
public ResultEnum data(String key, Object value) { this.data.put(key, value);
return this; } public ResultEnum message(String msg) { this.setMsg(msg);
return this; } }
|
可以看到的效果是,给枚举类型的变量赋值的时候,返回值也只能是枚举类中的变量;
至此结束,简单总结,代码还是分为四个包controller、service、dao、domain,但是新增了工具包,对返回的状态码与返回数据进行了重新封装、这样往往更加安全,但是封装之后的参数用枚举进行限定会更加安全,所以用上了枚举。