说完了前后端,来看看我们怎么写自己该写的吧,我主要说后端

原来的写法:

一套基本的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;

/**
* @author qidai
*/
@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;

/**
* @author qidai
* <p>
* 用来统一返回结果,因为前端的程序传递参数进来的时候,我们后端返回的日志不全面,有可能让后端出现大无语
* 这里统一规定格式:
* <p>
* <p>
* 修改成功:
* {
* "code":"20000"
* "msg":"修改成功"
* "data":{
* <p>
* }
* }
*/
@Data
public class Result {

private boolean success;// true 成功 false 失败

private Integer code;//状态码 20000 成功 20001失败


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;
}

/**
* 所谓的data方法就是向 hashmap中存放数据
*
* @param key 键值对中的key
* @param value 键值对中的value
* @return 这个返回值很有考究,返回this可以使我们的代码进入链式编程
*/
public Result data(String key, Object value) {
this.data.put(key, value);

//this代表当前的对象
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;

/**
* @author qidai
* <p>
* 枚举静态常量
*/
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;


/**
* @author qidai
*/
@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;


/**
* @author qidai
*/
@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;

/**
* @author qidai
*/
@RestController
@RequestMapping(path = "/user")
public class UserController {

private final UserService userService;

@Autowired
public UserController(UserService userService) {
this.userService = userService;
}

//这个接收方法是rest风格的 / 接收
@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);
}
}

增删改查真的太基础了,我不贴了,我主要贴区别,想要基础的,可以看我的

spring基础篇

企业中写法:

新增类:

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;


/**
* 返回的状态码永远是200,让前端舒服,(具体返回多少我们说的算),我们封装好一些信息,告诉前端具体的日志信息,否则光返回
* 状态码,前端不懂后端代码逻辑还是无济于事
*/
@RestController
@RequestMapping(path = "/user2")
public class User2Controller {


private final UserService userService;

@Autowired
public User2Controller(UserService userService) {
this.userService = userService;
}

//这个接收方法是rest风格的 / 接收
@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;

/**
* @author qidai
* <p>
* 用来统一返回结果,因为前端的程序传递参数进来的时候,我们后端返回的日志不全面,有可能让后端出现大无语
* 这里统一规定格式:
* <p>
* <p>
* 修改成功:
* {
* "code":"20000"
* "msg":"修改成功"
* "data":{
* <p>
* }
* }
*/
@Data
public class Result {

private boolean success;// true 成功 false 失败

private Integer code;//状态码 20000 成功 20001失败


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;
}

/**
* 所谓的data方法就是向 hashmap中存放数据
*
* @param key 键值对中的key
* @param value 键值对中的value
* @return 这个返回值很有考究,返回this可以使我们的代码进入链式编程
*/
public Result data(String key, Object value) {
this.data.put(key, value);

//this代表当前的对象
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;

/**
* @author qidai
* <p>
* 枚举静态常量
*/
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;

/**
* @author qidai
* <p>
* 枚举静态常量
*/
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;

/**
* @author qidai
* <p>
* 用来统一返回结果,因为前端的程序传递参数进来的时候,我们后端返回的日志不全面,有可能让后端出现大无语
* 这里统一规定格式:
* <p>
* <p>
* 修改成功:
* {
* "code":"20000"
* "msg":"修改成功"
* "data":{
* <p>
* }
* }
*/
@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;
}

/**
* 所谓的data方法就是向 hashmap中存放数据
*
* @param key 键值对中的key
* @param value 键值对中的value
* @return 这个返回值很有考究,返回this可以使我们的代码进入链式编程
*/
public ResultEnum data(String key, Object value) {
this.data.put(key, value);

//this代表当前的对象
return this;
}
public ResultEnum message(String msg) {
this.setMsg(msg);

return this;
}
}

可以看到的效果是,给枚举类型的变量赋值的时候,返回值也只能是枚举类中的变量;

至此结束,简单总结,代码还是分为四个包controller、service、dao、domain,但是新增了工具包,对返回的状态码与返回数据进行了重新封装、这样往往更加安全,但是封装之后的参数用枚举进行限定会更加安全,所以用上了枚举。