Skip to content

Commit aa64c03

Browse files
committed
Update >> 完成首页问题列表展示
Fix Bug >> 提交问题落库没有时间
1 parent b6f8c90 commit aa64c03

File tree

13 files changed

+300
-22
lines changed

13 files changed

+300
-22
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
>[Spring Mybaits](https://mybatis.org/mybatis-3/zh/index.html)
1111
>[Flyway Migration](https://flywaydb.org/getstarted/firststeps/maven)
1212
>[Project Lombok](https://projectlombok.org/)
13+
>[Thymeleaf](https://thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-srreibute-values)
1314
1415
工具
1516
>代码版本管理[GitHub](https://github.com)
1617
>流程图[Visual Paradigm](https://www.visual-paradigm.com)
1718
>数据库版本脚本管理[Flyway Migration](https://flywaydb.org/getstarted/firststeps/maven)
1819
>自动插入编辑器并构建工具[Project Lombok](https://projectlombok.org/)
20+
>
1921
2022
> # Git 使用
2123
> ```shell script
@@ -39,6 +41,7 @@
3941
> 5. 实现持久化登录 服务器下发 user_token 并在数据库查询
4042
> 6. 集成 `Flyway Migration` 统一数据库结构脚本(数据库版本控制)
4143
> 7. 添加 `Lombok` 支持 自动构建
44+
> 8. 完善首页问题列表展示
4245
4346
```markdown
4447
*Flyway Migration*

projectTree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
D:.
2+
├── ProjectTree // 项目结构介绍
3+
├── README.md
4+
├── Wiki.md
5+
├── bash.exe.stackdump
6+
├── community.iml
7+
├── mvnw
8+
├── mvnw.cmd
9+
├── pom.xml // Maveny依赖
10+
├── src
11+
│   ├── main
12+
│   │   ├── java
13+
│   │   │   └── life
14+
│   │   │   ├── CommunityApplication.java
15+
│   │   │   └── majiang
16+
│   │   │   └── community // 控制器
17+
│   │   │   ├── controller
18+
│   │   │   │   ├── AuthorizeController.java
19+
│   │   │   │   ├── HelloController.java // 测试
20+
│   │   │   │   ├── IndexController.java
21+
│   │   │   │   └── PublishController.java
22+
│   │   │   ├── dto // 传输层
23+
│   │   │   │   ├── AccessTokenDTO.java
24+
│   │   │   │   ├── GithubUser.java
25+
│   │   │   │   └── QuestionDTO.java
26+
│   │   │   ├── mapper // 映射层
27+
│   │   │   │   ├── QuestionMapper.java
28+
│   │   │   │   └── UserMapper.java
29+
│   │   │   ├── model // 数据库模型
30+
│   │   │   │   ├── Question.java
31+
│   │   │   │   └── User.java
32+
│   │   │   ├── provider // 连接层
33+
│   │   │   │   └── GithubProvider.java
34+
│   │   │   └── service // 服务层 中间层
35+
│   │   │      └── QuestionService
36+
│   │   └── resources // 资源
37+
│   │   ├── application.properties // 启动入口
38+
│   │   ├── db // 数据库
39+
│   │   │   └── migration //版本控制
40+
│   │   │   ├── V1__Create_User_table.sql
41+
│   │   │   ├── V2__Add_bio_To_User_table.sql
42+
│   │   │   ├── V3__Create_question_table.sql
43+
│   │   │   └── V4__Add_avatar_url_To_UserTable.sql
44+
│   │   ├── static
45+
│   │   │   ├── css // 样式
46+
│   │   │   │   ├── bootstrap-theme.css
47+
│   │   │   │   ├── bootstrap-theme.css.map
48+
│   │   │   │   ├── bootstrap-theme.min.css
49+
│   │   │   │   ├── bootstrap-theme.min.css.map
50+
│   │   │   │   ├── bootstrap.css
51+
│   │   │   │   ├── bootstrap.css.map
52+
│   │   │   │   ├── bootstrap.min.css
53+
│   │   │   │   ├── bootstrap.min.css.map
54+
│   │   │   │   └── community.css
55+
│   │   │   ├── fonts
56+
│   │   │   │   ├── glyphicons-halflings-regular.eot
57+
│   │   │   │   ├── glyphicons-halflings-regular.svg
58+
│   │   │   │   ├── glyphicons-halflings-regular.ttf
59+
│   │   │   │   ├── glyphicons-halflings-regular.woff
60+
│   │   │   │   └── glyphicons-halflings-regular.woff2
61+
│   │   │   └── js // js脚本
62+
│   │   │   ├── bootstrap.js
63+
│   │   │   ├── bootstrap.min.js
64+
│   │   │   └── npm.js
65+
│   │   └── templates // Web页面
66+
│   │   ├── hello.html // 测试
67+
│   │   ├── index.html
68+
│   │   └── publish.html
69+
│   └── test
70+
│   └── java
71+
│   └── life
72+
│   └── majiang
73+
│   └── community
74+
│   └── CommunityApplicationTests.java
75+
└── target
76+
├── classes
77+
│   ├── application.properties
78+
│   ├── db
79+
│   │   └── migration
80+
│   │   ├── V1__Create_User_table.sql
81+
│   │   ├── V2__Add_bio_To_User_table.sql
82+
│   │   ├── V3__Create_question_table.sql
83+
│   │   └── V4__Add_avatar_url_To_UserTable.sql
84+
│   ├── life
85+
│   │   ├── CommunityApplication.class
86+
│   │   └── majiang
87+
│   │   └── community
88+
│   │   ├── controller
89+
│   │   │   ├── AuthorizeController.class
90+
│   │   │   └── PublishController.class
91+
│   │   ├── dto
92+
│   │   │   ├── AccessTokenDTO.class
93+
│   │   │   ├── GithubUser.class
94+
│   │   │   └── QuestionDTO.class
95+
│   │   ├── mapper
96+
│   │   │   ├── QuestionMapper.class
97+
│   │   │   └── UserMapper.class
98+
│   │   ├── model
99+
│   │   │   ├── Question.class
100+
│   │   │   └── User.class
101+
│   │   └── provider
102+
│   │   └── GithubProvider.class
103+
│   ├── static
104+
│   │   ├── css
105+
│   │   │   ├── bootstrap-theme.css
106+
│   │   │   ├── bootstrap-theme.css.map
107+
│   │   │   ├── bootstrap-theme.min.css
108+
│   │   │   ├── bootstrap-theme.min.css.map
109+
│   │   │   ├── bootstrap.css
110+
│   │   │   ├── bootstrap.css.map
111+
│   │   │   ├── bootstrap.min.css
112+
│   │   │   ├── bootstrap.min.css.map
113+
│   │   │   └── community.css
114+
│   │   ├── fonts
115+
│   │   │   ├── glyphicons-halflings-regular.eot
116+
│   │   │   ├── glyphicons-halflings-regular.svg
117+
│   │   │   ├── glyphicons-halflings-regular.ttf
118+
│   │   │   ├── glyphicons-halflings-regular.woff
119+
│   │   │   └── glyphicons-halflings-regular.woff2
120+
│   │   └── js
121+
│   │   ├── bootstrap.js
122+
│   │   ├── bootstrap.min.js
123+
│   │   └── npm.js
124+
│   └── templates
125+
│   ├── hello.html
126+
│   ├── index.html
127+
│   └── publish.html
128+
├── generated-sources
129+
│   └── annotations
130+
├── generated-test-sources
131+
│   └── test-annotations
132+
└── test-classes
133+
└── life
134+
└── majiang
135+
└── community
136+
└── CommunityApplicationTests.class
137+
138+
50 directories, 84 files

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ public String callback(@RequestParam(name = "code") String code,
6161
String token = UUID.randomUUID().toString();
6262
/** token 放入 User 对象中并存储到数据库 */
6363
user.setToken(token); // 生成唯一的标识码
64-
user.setName(githubUser.getName());
65-
user.setAccountID(String.valueOf(githubUser.getId()));
66-
user.setGmtCreate(System.currentTimeMillis());
67-
user.setGmtModified(user.getGmtCreate());
68-
user.setAvatarUrl(githubUser.getAvatar_url());
64+
user.setName(githubUser.getName()); // set 用户名
65+
user.setAccountID(String.valueOf(githubUser.getId())); // set 用户第三方id
66+
user.setGmtCreate(System.currentTimeMillis()); // set 创建时间
67+
user.setGmtModified(user.getGmtCreate()); // set 更新时间
68+
user.setAvatarUrl(githubUser.getAvatar_url()); // set 用户头像
6969
System.out.println("--->>> usermapper >>>--- " + userMapper);
7070
System.out.println("--->>> insertuser >>>--- " + user);
71-
userMapper.insert(user);
71+
userMapper.insert(user); // 执行SQL
7272
/** Cookie 写入 */
7373
// 自动写入 服务器生成 Token 放入 Cookie
7474
response.addCookie(new Cookie("token", token));

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package life.majiang.community.controller;
22

33
/*
4+
* 传输层
45
* 定义自己的 index
56
*/
67

8+
import life.majiang.community.dto.QuestionDTO;
79
import life.majiang.community.mapper.UserMapper;
810
import life.majiang.community.model.User;
11+
import life.majiang.community.service.QuestionService;
912
import org.springframework.beans.factory.annotation.Autowired;
1013
import org.springframework.stereotype.Controller;
14+
import org.springframework.ui.Model;
1115
import org.springframework.web.bind.annotation.GetMapping;
1216

1317
import javax.servlet.http.Cookie;
1418
import javax.servlet.http.HttpServletRequest;
19+
import java.util.List;
1520

1621
@Controller
1722
public class IndexController {
1823

1924
@Autowired // 注入 UserMapper
2025
private UserMapper userMapper;
2126

27+
@Autowired // 注入 UserMapper
28+
private QuestionService questionService;
29+
2230
/**
2331
* 访问首页的时候 循环看所有的Cookie
2432
* 找到 Cookie 为 token 的值
@@ -27,7 +35,8 @@ public class IndexController {
2735
* 前端通过页面数据判断是否登录
2836
*/
2937
@GetMapping("/")
30-
public String index(HttpServletRequest request) {
38+
public String index(HttpServletRequest request,
39+
Model model) {
3140
Cookie[] cookies = request.getCookies(); // 获取用户 cookie
3241
if (cookies != null && cookies.length != 0) {// 判断是用户 Cookie 是否为空 长度不为0
3342
for (Cookie cookie : cookies) {
@@ -36,12 +45,17 @@ public String index(HttpServletRequest request) {
3645
User user = userMapper.findByToken(token);
3746
// 如果user != null 写入session
3847
if (user != null) {
48+
System.out.println(user.toString());
3949
request.getSession().setAttribute("user", user);
4050
}
4151
break; // 命中后结束循环
4252
}
4353
}
4454
}
55+
// 获取 question_list
56+
// List<Question> questionList = questionMapper.list(); // question表不能返回用户头像
57+
List<QuestionDTO> questionList = questionService.list();
58+
model.addAttribute("questions", questionList); // 此处返回除question信息外还有User信息
4559
return "index";
4660
}
4761
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ public String doQuestion(
8989
* publish_page 问题提内容
9090
* */
9191
Question question = new Question();
92-
question.setTitle(title);
93-
question.setDescription(description);
94-
question.setTag(tag);
95-
question.setCreator(user.getId());
96-
questionMapper.create(question);
92+
question.setTitle(title);// set 标题
93+
question.setDescription(description);// set 内容
94+
question.setTag(tag);// set 标签
95+
question.setCreator(user.getId());// set 提问者id
96+
question.setGmtCreate(System.currentTimeMillis()); // set 创建时间
97+
question.setGmtModified(System.currentTimeMillis()); // set 更新时间
98+
questionMapper.create(question); // 执行SQL
9799
return "redirect:/"; // 成功返回 首页
98100
}
99101
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package life.majiang.community.dto;
2+
3+
import life.majiang.community.model.User;
4+
import lombok.Data;
5+
6+
@Data
7+
public class QuestionDTO {
8+
private Integer id; // 问题自增id
9+
private String title; // title
10+
private String description; // 描述
11+
private String tag; // 标签
12+
private Long gmtCreate; // 创建时间
13+
private Long gmtModified; // 更新时间
14+
private Integer creator; // 创建者
15+
private Integer viewCount; // 阅读数
16+
private Integer commentCount; // 提交数
17+
private Integer likeCount; // 点赞数
18+
private User user; // 用户头像
19+
20+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
import life.majiang.community.model.Question;
44
import org.apache.ibatis.annotations.Insert;
55
import org.apache.ibatis.annotations.Mapper;
6+
import org.apache.ibatis.annotations.Select;
7+
8+
import java.util.List;
69

710
@Mapper
811
public interface QuestionMapper {
912
@Insert("insert into question (title, description, gmt_create, gmt_modified, creator, tag) " +
1013
"values (#{title}, #{description}, #{gmtCreate}, #{gmtModified}, #{creator}, #{tag})")
1114
void create(Question question);
15+
16+
@Select("select * from question")
17+
List<Question> list();
1218
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/*
1010
* 引入数据库
11-
* 类会自动放入 不是的需要引入 @Param 注解
11+
* 类会自动放入 不是的类的 需要引入 @Param 注解
1212
*/
1313
@Mapper
1414
public interface UserMapper {
@@ -17,4 +17,8 @@ public interface UserMapper {
1717

1818
@Select("select * from USER where token = #{token}")
1919
User findByToken(@Param("token") String token);
20+
21+
@Select("select * from USER where id = #{id}")
22+
User findByID(@Param("id") Integer id);
23+
2024
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package life.majiang.community.service;
2+
3+
import life.majiang.community.dto.QuestionDTO;
4+
import life.majiang.community.mapper.QuestionMapper;
5+
import life.majiang.community.mapper.UserMapper;
6+
import life.majiang.community.model.Question;
7+
import life.majiang.community.model.User;
8+
import org.springframework.beans.BeanUtils;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
/**
16+
* 中间层 组装请求
17+
*/
18+
@Service
19+
public class QuestionService {
20+
21+
// 注入依赖
22+
@Autowired
23+
private UserMapper userMapper;
24+
@Autowired
25+
private QuestionMapper questionMapper;
26+
27+
public List<QuestionDTO> list() {
28+
List<Question> questions = questionMapper.list(); // 查询所有question
29+
List<QuestionDTO> questionDTOList = new ArrayList<>(); // new list
30+
for (Question question : questions) { // 循环Question对象
31+
User user = userMapper.findByID(question.getCreator()); // 用getCreator获取当前User 返回User对象
32+
QuestionDTO questionDTO = new QuestionDTO();
33+
BeanUtils.copyProperties(question, questionDTO); // 该工具类的目的是 question的属性快速复制到 questionDTO
34+
questionDTO.setUser(user); // 返回的DTO对象 需新建list
35+
questionDTOList.add(questionDTO);
36+
37+
}
38+
39+
return questionDTOList; // 返回的 DTOlist
40+
}
41+
}

src/main/resources/application.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ github.client.uri=http://localhost:8080/callback
88
spring.datasource.url=jdbc:h2:D:\\Users\\LITSOFT\\IdeaProjects\\SqlDate\\community.db
99
spring.datasource.username=root
1010
spring.datasource.password=123456
11-
spring.datasource.driver-class-name=org.h2.Driver
11+
spring.datasource.driver-class-name=org.h2.Driver
12+
# MyBaits配置
13+
#开启驼峰标识
14+
mybatis.configuration.map-underscore-to-camel-case=true

0 commit comments

Comments
 (0)