Skip to content

Commit f420b17

Browse files
committedApr 15, 2021
comment finish
1 parent 671e9ef commit f420b17

34 files changed

+3654
-346
lines changed
 

‎README.md

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
## 解决问题
1717
[ajax不执行callback函数问题](https://blog.csdn.net/rhythm923/article/details/66476425)
1818

19+
--
20+
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
21+
--

‎src/main/java/online/xuanwei/bbs/BbsApplication.java

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package online.xuanwei.bbs;
22

33
import org.mybatis.spring.annotation.MapperScan;
4+
import org.mybatis.spring.annotation.MapperScans;
45
import org.springframework.boot.SpringApplication;
56
import org.springframework.boot.autoconfigure.SpringBootApplication;
67

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package online.xuanwei.bbs.advice;
2+
3+
import online.xuanwei.bbs.exception.CustomizeException;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.ControllerAdvice;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.servlet.ModelAndView;
9+
10+
import javax.servlet.http.HttpServletRequest;
11+
12+
@ControllerAdvice
13+
public class CustomizeExceptionHandler {
14+
15+
@ExceptionHandler(Exception.class)
16+
ModelAndView Model(HttpServletRequest httpServletRequest, Throwable throwable, Model model){
17+
HttpStatus httpStatus = getStatus(httpServletRequest);
18+
if (throwable instanceof CustomizeException) {
19+
model.addAttribute("message", throwable.getMessage());
20+
}
21+
return new ModelAndView("error");
22+
}
23+
24+
private HttpStatus getStatus(HttpServletRequest httpServletRequest){
25+
Integer statusCode = (Integer) httpServletRequest.getAttribute("javax.servlet.error.status_code");
26+
if(statusCode == null){
27+
return HttpStatus.INTERNAL_SERVER_ERROR;
28+
}
29+
return HttpStatus.valueOf(statusCode);
30+
}
31+
}

‎src/main/java/online/xuanwei/bbs/controller/AuthorizeController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public String callback(@RequestParam(name = "code") String code,
5757
if(githubUser.getId() !=0){
5858
UserExample userExample = new UserExample();
5959
userExample.createCriteria()
60-
.andAccountIdEqualTo("38238021");
60+
.andAccountIdEqualTo(Long.toString(githubUser.getId()));
6161
List<User> users = userMapper.selectByExample(userExample);
6262
if(users.size() == 0 ) {
6363
User user = new User();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package online.xuanwei.bbs.controller;
2+
3+
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
4+
import org.springframework.boot.web.servlet.error.ErrorController;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.servlet.ModelAndView;
11+
12+
import javax.jws.WebParam;
13+
import javax.servlet.RequestDispatcher;
14+
import javax.servlet.http.HttpServletRequest;
15+
import javax.servlet.http.HttpServletResponse;
16+
import java.util.Collections;
17+
import java.util.Map;
18+
19+
20+
@Controller
21+
@RequestMapping("${server.error.path:${error.path:/error}}")
22+
public class CustomizeErrorController implements ErrorController {
23+
24+
@Override
25+
public String getErrorPath() {
26+
return "error";
27+
}
28+
29+
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
30+
public ModelAndView errorHtml(HttpServletRequest request, Model model) {
31+
HttpStatus status = getStatus(request);
32+
if(status.is4xxClientError()){
33+
model.addAttribute("message","你请求错了");
34+
}
35+
36+
return new ModelAndView("error");
37+
}
38+
39+
private HttpStatus getStatus(HttpServletRequest request) {
40+
Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
41+
if (statusCode == null) {
42+
return HttpStatus.INTERNAL_SERVER_ERROR;
43+
}
44+
try {
45+
return HttpStatus.valueOf(statusCode);
46+
}
47+
catch (Exception ex) {
48+
return HttpStatus.INTERNAL_SERVER_ERROR;
49+
}
50+
}
51+
}

‎src/main/java/online/xuanwei/bbs/controller/IndexController.java

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import online.xuanwei.bbs.dto.PaginationDTO;
44
import online.xuanwei.bbs.dto.QuestionDTO;
5+
import online.xuanwei.bbs.exception.CustomizeErrorCode;
6+
import online.xuanwei.bbs.exception.CustomizeException;
57
import online.xuanwei.bbs.mapper.UserMapper;
68
import online.xuanwei.bbs.service.QuestionService;
79
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,6 +35,10 @@ public String index(HttpServletRequest request, Model model,
3335
paginationDTO.setPage(page);
3436
paginationDTO.setSize(size);
3537
paginationDTO.setPages(size, questionService.getCount());
38+
//System.out.println("测试页码"+page+" "+paginationDTO.getPages());
39+
if (page>paginationDTO.getPages()){
40+
throw new CustomizeException(CustomizeErrorCode.PAGE_NOT_FOUND);
41+
}
3642
if(model.getAttribute("questions")==null) {
3743
model.addAttribute("questions", paginationDTO);
3844
}else{

‎src/main/java/online/xuanwei/bbs/controller/PublishController.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package online.xuanwei.bbs.controller;
22

3-
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.QEncoderStream;
3+
import online.xuanwei.bbs.exception.CustomizeErrorCode;
4+
import online.xuanwei.bbs.exception.CustomizeException;
5+
import online.xuanwei.bbs.exception.ICustomizeErrorCode;
46
import online.xuanwei.bbs.mapper.QuestionMapper;
7+
import online.xuanwei.bbs.mapper.QuestionMapperExt;
58
import online.xuanwei.bbs.model.Question;
9+
import online.xuanwei.bbs.model.QuestionExample;
610
import online.xuanwei.bbs.model.User;
711
import online.xuanwei.bbs.util.Result;
812
import online.xuanwei.bbs.util.ResultGenerator;
@@ -19,10 +23,13 @@ public class PublishController {
1923
@Autowired
2024
QuestionMapper questionMapper;
2125

26+
@Autowired
27+
QuestionMapperExt questionMapperExt;
28+
2229

2330
@GetMapping("/publish/edit")
2431
public String edit(@RequestParam(name="id") Integer id, Model model){
25-
Question question = questionMapper.getById(id);
32+
Question question = questionMapper.selectByPrimaryKey(id);
2633
model.addAttribute("edit",question);
2734
return "publish";
2835
}
@@ -38,9 +45,12 @@ public String update(@RequestParam("id") Integer id,
3845
question.setDescription(description);
3946
question.setTag(tag);
4047
question.setGmtModified(System.currentTimeMillis());
41-
questionMapper.update(question);
42-
System.out.println(questionMapper.getById(question.getId()).toString());
43-
return "redirect:index";
48+
int updated = questionMapperExt.updateContentByPrimaryKey(question);
49+
if (updated !=1){
50+
throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FOUND);
51+
}
52+
return "redirect:question?id=" + id;
53+
4454
}
4555

4656
@GetMapping("/publish")
@@ -67,7 +77,7 @@ public Result doPublish(@RequestParam("title") String title,
6777
question.setCreator(user.getId());
6878
question.setGmtCreate(System.currentTimeMillis());
6979
question.setGmtModified(question.getGmtCreate());
70-
questionMapper.create(question);
80+
questionMapper.insert(question);
7181
return ResultGenerator.genSuccessResult("发布成功");
7282
}
7383

‎src/main/java/online/xuanwei/bbs/controller/QuestionController.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package online.xuanwei.bbs.controller;
22

33
import online.xuanwei.bbs.dto.QuestionDTO;
4-
import online.xuanwei.bbs.mapper.QuestionMapper;
54
import online.xuanwei.bbs.service.QuestionService;
65
import org.springframework.beans.factory.annotation.Autowired;
76
import org.springframework.stereotype.Controller;
87
import org.springframework.ui.Model;
98
import org.springframework.web.bind.annotation.GetMapping;
10-
import org.springframework.web.bind.annotation.PathVariable;
119
import org.springframework.web.bind.annotation.RequestParam;
1210

1311
@Controller
1412
public class QuestionController {
1513

14+
15+
1616
@Autowired
1717
QuestionService questionService;
1818

1919
@GetMapping("/question")
2020
public String questions(@RequestParam(name = "id") Integer id, Model model){
2121
QuestionDTO questionDTO = questionService.getById(id);
22+
questionService.incView(id);
2223
model.addAttribute("question",questionDTO);
2324
return "question";
2425

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package online.xuanwei.bbs.exception;
2+
3+
/**
4+
* lxw
5+
*/
6+
7+
public enum CustomizeErrorCode implements ICustomizeErrorCode{
8+
9+
QUESTION_NOT_FOUND("找不到文章!"),
10+
PAGE_NOT_FOUND("找不到该页面,请重新输入");
11+
12+
private String message;
13+
14+
@Override
15+
public String getMessage() {
16+
return message;
17+
}
18+
19+
CustomizeErrorCode(String message) {
20+
this.message = message;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package online.xuanwei.bbs.exception;
2+
3+
public class CustomizeException extends RuntimeException{
4+
private String message;
5+
6+
public CustomizeException(ICustomizeErrorCode errorCode) {
7+
this.message = errorCode.getMessage();
8+
}
9+
10+
@Override
11+
public String getMessage() {
12+
return message;
13+
}
14+
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package online.xuanwei.bbs.exception;
2+
3+
public interface ICustomizeErrorCode {
4+
String getMessage();
5+
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package online.xuanwei.bbs.mapper;
2+
3+
import java.util.List;
4+
import online.xuanwei.bbs.model.Comment;
5+
import online.xuanwei.bbs.model.CommentExample;
6+
import org.apache.ibatis.annotations.Param;
7+
import org.apache.ibatis.session.RowBounds;
8+
9+
public interface CommentMapper {
10+
/**
11+
* This method was generated by MyBatis Generator.
12+
* This method corresponds to the database table comment
13+
*
14+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
15+
*/
16+
long countByExample(CommentExample example);
17+
18+
/**
19+
* This method was generated by MyBatis Generator.
20+
* This method corresponds to the database table comment
21+
*
22+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
23+
*/
24+
int deleteByExample(CommentExample example);
25+
26+
/**
27+
* This method was generated by MyBatis Generator.
28+
* This method corresponds to the database table comment
29+
*
30+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
31+
*/
32+
int deleteByPrimaryKey(Long id);
33+
34+
/**
35+
* This method was generated by MyBatis Generator.
36+
* This method corresponds to the database table comment
37+
*
38+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
39+
*/
40+
int insert(Comment record);
41+
42+
/**
43+
* This method was generated by MyBatis Generator.
44+
* This method corresponds to the database table comment
45+
*
46+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
47+
*/
48+
int insertSelective(Comment record);
49+
50+
/**
51+
* This method was generated by MyBatis Generator.
52+
* This method corresponds to the database table comment
53+
*
54+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
55+
*/
56+
List<Comment> selectByExampleWithRowbounds(CommentExample example, RowBounds rowBounds);
57+
58+
/**
59+
* This method was generated by MyBatis Generator.
60+
* This method corresponds to the database table comment
61+
*
62+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
63+
*/
64+
List<Comment> selectByExample(CommentExample example);
65+
66+
/**
67+
* This method was generated by MyBatis Generator.
68+
* This method corresponds to the database table comment
69+
*
70+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
71+
*/
72+
Comment selectByPrimaryKey(Long id);
73+
74+
/**
75+
* This method was generated by MyBatis Generator.
76+
* This method corresponds to the database table comment
77+
*
78+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
79+
*/
80+
int updateByExampleSelective(@Param("record") Comment record, @Param("example") CommentExample example);
81+
82+
/**
83+
* This method was generated by MyBatis Generator.
84+
* This method corresponds to the database table comment
85+
*
86+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
87+
*/
88+
int updateByExample(@Param("record") Comment record, @Param("example") CommentExample example);
89+
90+
/**
91+
* This method was generated by MyBatis Generator.
92+
* This method corresponds to the database table comment
93+
*
94+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
95+
*/
96+
int updateByPrimaryKeySelective(Comment record);
97+
98+
/**
99+
* This method was generated by MyBatis Generator.
100+
* This method corresponds to the database table comment
101+
*
102+
* @mbg.generated Thu Apr 15 22:07:05 CST 2021
103+
*/
104+
int updateByPrimaryKey(Comment record);
105+
}

0 commit comments

Comments
 (0)
Please sign in to comment.