栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【community1】社区首页

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【自用 不全面】
这一期用到的技术栈有ssm,mybatis-plus,junit4,maven视图层 thymeleaf

记录一点可以复用的配置吧,这些都是新建项目时必须关注的:

一、之后可复用的配置
  1. 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
                        
                    
                
            
        
    



  1. 数据库连接 配置

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视图层。

二、thymeleaf视图层修改要点
  1. 相对路径的处理

    对于相对路径都需要这样处理

  2. 帖子列表从静态变成动态(循环)

原代码:


				

改后:

引入模版引擎


相对路径的修改(举例):

	

	
	

说明: 对于链接(比如href标签),使用链接表达式: @{…}

帖子列表:
定义变量名为map
th:each="map:${discussPosts}


				
三、分页要点
  1. 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());
    }
}

  1. 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> discussPosts=new ArrayList<>();
        for(DiscussPost discussPost:list){
            Map map=new HashMap<>();
            map.put("post",discussPost);
            String userId = discussPost.getUserId();
            Integer userIdNumber = Integer.valueOf(userId);
            User userById = userService.findUserById(userIdNumber);
            map.put("user",userById);
            discussPosts.add(map);
        }
        model.addAttribute("discussPosts",discussPosts);
        return "index";
    }





}

  1. 模版修改

消息表达式: #{…},国际化时使用,也可以使用内置的对象,比如date格式化数据。 这里用了内置对象numbers.sequence(),一个数字集合,集合命名为i,用于下方遍历


				
  1. 一些问题
    current没看到有设置(并没有调用setCurrent)?
    根据评论区: cuurent由前端给的路径直接封装到page里了,这是SpringMVC自动提供的封装。

thymeleaf的each标签用于遍历集合,用法th:each="objName: ${list}"

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/927133.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号