Skip to content

Commit a3586c2

Browse files
committed
Update >> 修复登录唯一性 执行update操作 增加问题更新功能
1 parent dabfc06 commit a3586c2

File tree

14 files changed

+110
-80
lines changed

14 files changed

+110
-80
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@
4848
> 9. 使用 `developer tools` 添加配置 `spring.devtools.restart.exclude=static/**,public/**` 完成自动部署(热更新)
4949
> 10. 添加分页功能
5050
> 11. 添加拦截器 `WebConfig`
51-
> 12. 完善问题详情页
51+
> 12. 完善问题详情页 & 修复登录持久化每次插入不更新用户信息 增加问题更新功能
52+
53+
54+
> > 排查问题先检查`debug` 信息中的 `Caused by`
5255
5356
```markdown
5457
*Flyway Migration*

src/main/java/life/majiang/community/controller/AuthorizeController.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66

77
import life.majiang.community.dto.AccessTokenDTO;
88
import life.majiang.community.dto.GithubUser;
9-
import life.majiang.community.mapper.UserMapper;
109
import life.majiang.community.model.User;
1110
import life.majiang.community.provider.GithubProvider;
11+
import life.majiang.community.service.UserServices;
1212
import org.springframework.beans.factory.annotation.Autowired;
1313
import org.springframework.beans.factory.annotation.Value;
1414
import org.springframework.stereotype.Controller;
1515
import org.springframework.web.bind.annotation.GetMapping;
1616
import org.springframework.web.bind.annotation.RequestParam;
1717

1818
import javax.servlet.http.Cookie;
19+
import javax.servlet.http.HttpServletRequest;
1920
import javax.servlet.http.HttpServletResponse;
2021
import java.util.UUID;
2122

@@ -34,8 +35,8 @@ public class AuthorizeController {
3435
@Value("${github.client.uri}")
3536
private String clientUri;
3637

37-
@Autowired // 把 Usermapper 对象放入容器内
38-
private UserMapper userMapper;
38+
@Autowired // 替换userMapper
39+
private UserServices userServices;
3940

4041
@GetMapping("/callback") // 登录成功后返回到登录页
4142
// 接收返回后的参数 code, state
@@ -63,12 +64,9 @@ public String callback(@RequestParam(name = "code") String code,
6364
user.setToken(token); // 生成唯一的标识码
6465
user.setName(githubUser.getName()); // set 用户名
6566
user.setAccountID(String.valueOf(githubUser.getId())); // set 用户第三方id
66-
user.setGmtCreate(System.currentTimeMillis()); // set 创建时间
67-
user.setGmtModified(user.getGmtCreate()); // set 更新时间
6867
user.setAvatarUrl(githubUser.getAvatar_url()); // set 用户头像
69-
System.out.println("--->>> usermapper >>>--- " + userMapper);
7068
System.out.println("--->>> insertuser >>>--- " + user);
71-
userMapper.insert(user); // 执行SQL
69+
userServices.createOrUpdate(user); // 执行SQL 插入或更新user
7270
/** Cookie 写入 */
7371
// 自动写入 服务器生成 Token 放入 Cookie
7472
response.addCookie(new Cookie("token", token));
@@ -80,5 +78,16 @@ public String callback(@RequestParam(name = "code") String code,
8078
return "redirect:/";
8179
}
8280
}
81+
82+
@GetMapping("logout")
83+
public String logout(HttpServletRequest request,
84+
HttpServletResponse response) {
85+
request.getSession().removeAttribute("user"); // 移除session
86+
Cookie cookie = new Cookie("token", null); // 移除token 重新赋值为null
87+
cookie.setMaxAge(0);
88+
response.addCookie(cookie);
89+
return "redirect:/";
90+
}
91+
8392
}
8493

src/main/java/life/majiang/community/controller/IndexController.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,8 @@ public String index(HttpServletRequest request,
3737
@RequestParam(name = "page", defaultValue = "1") Integer page, // 默认第1页
3838
@RequestParam(name = "size", defaultValue = "5") Integer size // 默认5页 每页条数
3939
) {
40-
// 用户 cookies 由拦截器SessionInterceptor执行
41-
// Cookie[] cookies = request.getCookies(); // 获取用户 cookie
42-
// if (cookies != null && cookies.length != 0) {// 判断是用户 Cookie 是否为空 长度不为0
43-
// for (Cookie cookie : cookies) {
44-
// if (cookie.getName().equals("token")) { // 检查 cookies_key是否为 token
45-
// String token = cookie.getValue();
46-
// User user = userMapper.findByToken(token);
47-
// // 如果user != null 写入session
48-
// if (user != null) {
49-
// System.out.println(user.toString());
50-
// request.getSession().setAttribute("user", user);
51-
// }
52-
// break; // 命中后结束循环
53-
// }
54-
// }
55-
// }
40+
// 用户 cookies 由拦截器SessionInterceptor执行 >> 移动到 SessionController
41+
5642
// 获取 question_list
5743
// List<Question> questionList = questionMapper.list(); // question表不能返回用户头像
5844
PaginationDTO pagination = questionService.list(page, size);

src/main/java/life/majiang/community/controller/ProfileController.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,8 @@ public String profile(HttpServletRequest request,
2727
@RequestParam(name = "page", defaultValue = "1") Integer page, // 默认第1页
2828
@RequestParam(name = "size", defaultValue = "5") Integer size, // 默认5页 每页条数
2929
Model model) {
30-
// User user = null;
31-
// Cookie[] cookies = request.getCookies(); // 获取用户 cookie
32-
// if (cookies != null && cookies.length != 0) {// 判断是用户 Cookie 是否为空 长度不为0
33-
// for (Cookie cookie : cookies) {
34-
// if (cookie.getName().equals("token")) { // 检查 cookies_key是否为 token
35-
// String token = cookie.getValue();
36-
// user = userMapper.findByToken(token);
37-
// // 如果user != null 写入session
38-
// if (user != null) {
39-
// System.out.println(user.toString());
40-
// request.getSession().setAttribute("user", user);
41-
// }
42-
// break; // 命中后结束循环
43-
// }
44-
// }
45-
// }
30+
// 检测登录状态 移动到 SessionController
31+
4632
// 获取User
4733
User user = (User) request.getSession().getAttribute("user");
4834
if (user == null) { // 如果未登录跳转到 首页

src/main/java/life/majiang/community/controller/PublishController.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package life.majiang.community.controller;
22

3-
import life.majiang.community.mapper.QuestionMapper;
3+
import life.majiang.community.dto.QuestionDTO;
44
import life.majiang.community.model.Question;
55
import life.majiang.community.model.User;
6+
import life.majiang.community.service.QuestionService;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Controller;
89
import org.springframework.ui.Model;
910
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
1012
import org.springframework.web.bind.annotation.PostMapping;
1113
import org.springframework.web.bind.annotation.RequestParam;
1214

@@ -15,11 +17,20 @@
1517
@Controller
1618
public class PublishController {
1719

18-
@Autowired // 注入 questionMapper
19-
private QuestionMapper questionMapper;
20+
@Autowired
21+
private QuestionService questionService;
2022

21-
// @Autowired // 注入 userMapper
22-
// private UserMapper userMapper;
23+
@GetMapping("publish/{id}") // 编辑问题
24+
public String edit(@PathVariable(name = "id") Integer id,
25+
Model model) {
26+
QuestionDTO question = questionService.getById(id);
27+
// 回显到页面
28+
model.addAttribute("title", question.getTitle());
29+
model.addAttribute("description", question.getDescription());
30+
model.addAttribute("tag", question.getTag());
31+
model.addAttribute("id", question.getId());
32+
return "publish";
33+
}
2334

2435
// get 去渲染页面
2536
@GetMapping("/publish")
@@ -34,28 +45,15 @@ public String doQuestion(
3445
@RequestParam("title") String title,
3546
@RequestParam("description") String description,
3647
@RequestParam("tag") String tag,
48+
@RequestParam(value = "id", required = false) Integer id,
3749
HttpServletRequest request,
3850
Model model
3951
) {
4052

4153
/**
4254
* publish_page 获取填写者信息 判断是否登录
55+
* 移动到 SessionController
4356
* */
44-
// User user = null;
45-
// Cookie[] cookies = request.getCookies();
46-
// if (cookies != null && cookies.length != 0) {// 判断是用户 Cookie 是否为空 长度不为0 去除空指针
47-
// for (Cookie cookie : cookies) {
48-
// if (cookie.getName().equals("token")) { // 检查 cookies_key是否为 token
49-
// String token = cookie.getValue();
50-
// user = userMapper.findByToken(token);
51-
// // 如果user != null 写入session
52-
// if (user != null) {
53-
// request.getSession().setAttribute("user", user);
54-
// }
55-
// break; // 命中后结束循环
56-
// }
57-
// }
58-
// }
5957

6058
// 获取User
6159
User user = (User) request.getSession().getAttribute("user");
@@ -94,9 +92,8 @@ public String doQuestion(
9492
question.setDescription(description);// set 内容
9593
question.setTag(tag);// set 标签
9694
question.setCreator(user.getId());// set 提问者id
97-
question.setGmtCreate(System.currentTimeMillis()); // set 创建时间
98-
question.setGmtModified(System.currentTimeMillis()); // set 更新时间
99-
questionMapper.create(question); // 执行SQL
95+
question.setId(id); // set 问题id
96+
questionService.createOrUpdate(question); // 执行SQL 插入或更新question
10097
return "redirect:/"; // 成功返回 首页
10198
}
10299
}

src/main/java/life/majiang/community/controller/QuestionController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public String question(@PathVariable(name = "id") Integer id,
2020
QuestionDTO questionDTO = questionService.getById(id); // 获取用户id
2121
model.addAttribute("question", questionDTO); // 传递到页面
2222

23-
return "question";
23+
return "question"; // question中的user
2424
}
2525
}

src/main/java/life/majiang/community/interceptor/SessionInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
4040
}
4141
}
4242
}
43-
return true; // 返回 true 继续执行
43+
return true; // 返回 true 继续执行 & session中的user
4444
}
4545

4646
@Override

src/main/java/life/majiang/community/mapper/QuestionMapper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package life.majiang.community.mapper;
22

33
import life.majiang.community.model.Question;
4-
import org.apache.ibatis.annotations.Insert;
5-
import org.apache.ibatis.annotations.Mapper;
6-
import org.apache.ibatis.annotations.Param;
7-
import org.apache.ibatis.annotations.Select;
4+
import org.apache.ibatis.annotations.*;
85

96
import java.util.List;
107

@@ -30,4 +27,6 @@ public interface QuestionMapper {
3027
@Select("select * from question where id = #{id}")
3128
Question getById(@Param("id") Integer id);
3229

30+
@Update("update question set title = #{title}, description = #{description}, gmt_modified = #{gmtModified}, tag = #{id} where id = #{id}")
31+
void update(Question question); // 更新问题
3332
}

src/main/java/life/majiang/community/mapper/UserMapper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package life.majiang.community.mapper;
22

33
import life.majiang.community.model.User;
4-
import org.apache.ibatis.annotations.Insert;
5-
import org.apache.ibatis.annotations.Mapper;
6-
import org.apache.ibatis.annotations.Param;
7-
import org.apache.ibatis.annotations.Select;
4+
import org.apache.ibatis.annotations.*;
85

96
/*
107
* 引入数据库
@@ -21,4 +18,10 @@ public interface UserMapper {
2118
@Select("select * from USER where id = #{id}")
2219
User findByID(@Param("id") Integer id);
2320

21+
@Select("select * from USER where account_id = #{accountID}")
22+
User findByAccountId(@Param("accountID") String accountID); // 根据accountID查询用户
23+
24+
@Update("update user set name = #{name}, token = ${token} gmt_modified = #{gmt_Modified}, avatar_url = #{avatarUrl}) where id = #{id v}")
25+
void update(User user); // 更新用户信息
26+
2427
}

src/main/java/life/majiang/community/service/QuestionService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,18 @@ public QuestionDTO getById(Integer id) {
127127
questionDTO.setUser(user); // 添加返回 user属性
128128
return questionDTO;
129129
}
130+
131+
public void createOrUpdate(Question question) {
132+
if (question.getId() == null) {
133+
// 插入操作 未找到
134+
question.setGmtCreate(System.currentTimeMillis()); // set 创建时间
135+
question.setGmtModified(System.currentTimeMillis()); // set 更新时间
136+
questionMapper.create(question);
137+
} else {
138+
// 更新操作 找到
139+
question.setGmtModified(System.currentTimeMillis()); // set 更新时间
140+
questionMapper.update(question);
141+
142+
}
143+
}
130144
}

0 commit comments

Comments
 (0)