【自用 不全面】
这一期用到的技术栈有ssm,mybatis-plus,junit4,maven视图层 thymeleaf
记录一点可以复用的配置吧,这些都是新建项目时必须关注的:
一、之后可复用的配置- maven一开始要引入的包–6个初始化的时候勾选+mybatis-plus+junit
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.0 com.kkex community 0.0.1-SNAPSHOT community community 17 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2 org.springframework.boot spring-boot-devtools runtime true mysql mysql-connector-java runtime org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test junit junit 4.13 test com.baomidou mybatis-plus-boot-starter 3.5.1 org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
- 数据库连接 配置
application.yml
spring:
application:
name: community
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/community?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root
password: 123456
server:
port: 8080
mvc的三层已经不需要note了。。
比较不熟的有thymeleaf视图层。
-
相对路径的处理
对于相对路径都需要这样处理
-
帖子列表从静态变成动态(循环)
原代码:
-

备战春招,面试刷题跟他复习,一个月全搞定! 置顶 精华
寒江雪 发布于 2019-04-15 15:32:18- 赞 11
- |
- 回帖 7
改后:
引入模版引擎
相对路径的修改(举例):
说明: 对于链接(比如href标签),使用链接表达式: @{…}
帖子列表:
定义变量名为map
th:each="map:${discussPosts}
-
备战春招,面试刷题跟他复习,一个月全搞定! 置顶 精华
寒江雪 发布于 2019-04-15 15:32:18- 赞 11
- |
- 回帖 7
- Model中封装Page类
package com.example.community.model;
public class Page {
//当前页码
private int current=1;
//显示上限
private int limit=10;
//数据总数(用于计算总页数)
private int rows;
//查询路径(用于复用分页链接)
private String path;
private int totalPage=this.getTotalPage();
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
if(current>=1)
this.current = current;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
if(limit>=1&&limit<=100)
this.limit = limit;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
if(rows>=0)
this.rows = rows;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int getOffset(){
return (current-1)*limit;
}
public int getTotalPage(){
if(rows%limit==0){
return rows/limit;
}
else{
return rows/limit+1;
}
}
public int getFrom(){
int from=current-2;
return Math.max(from, 1);
}
public int getTo(){
int to=current+2;
return Math.min(to, getTotalPage());
}
}
- Controller类
Page类会被Spring MVC自动实例化,并将Page注入到Model中。(所有实体类都会做这样的处理)
@Controller
public class HomeController {
@Resource
private DiscussPostService discussPostService;
@Resource
private UserService userService;
//使用userService,这样查到的数据就全面了
@GetMapping("/index") //响应的是网页,所以不需要用ResponseBody注解
public String getIndexPage(Model model, Page page){//String是视图的名字
// 方法调用前,SpringMVC会自动实例化Model和Page,并将Page注入Model (MVC之妙)
//所以,在thymeleaf中可以直接访问Page对象中数据
//服务器也要给page设置一点值
page.setRows(discussPostService.findDiscussPostRows(0));
page.setPath("/index");
List list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());
List
- 模版修改
消息表达式: #{…},国际化时使用,也可以使用内置的对象,比如date格式化数据。 这里用了内置对象numbers.sequence(),一个数字集合,集合命名为i,用于下方遍历
- 一些问题
current没看到有设置(并没有调用setCurrent)?
根据评论区: cuurent由前端给的路径直接封装到page里了,这是SpringMVC自动提供的封装。
thymeleaf的each标签用于遍历集合,用法th:each="objName: ${list}"


