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

牛客网项目1:开发社区首页

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

牛客网项目1:开发社区首页

总结在先:

①首先根据每张表创建相对应的实体类,该实体类中的属性与表中的字段名相同;

②由于每张表都需要一些增删改查的方法,因此需要创建Mapper接口(每张表各一个),接口中放有对应表的增删改查方法。

③由于是接口,只提供了方法名,本身没有具体方法的实现。因此创建对应的XxxMapper.xml文件,在该xml文件中编写相应Mapper接口方法的SQL语句。

④直接去调用Mapper接口中的方法,不方便进行管理。因此为每个Mapper接口再创建对应的Service类,通过Service类去调用Mapper接口中相应的增删改查方法,Mapper接口又会去调用相应mapper.xml文件中真正的SQL语句。

这便是整个流程。

1. 创建项目

① new Project - Spring Initailizr

②填写 Group: com.nowcoder.mycommunity

③勾选:

 - 选择Web - 勾选Spring Web

 - 选择SQL - 勾选MyBatis Framework

 - 选择NoSQL - 勾选Spring Data Redis

 - Next - Finish

2. 添加依赖:pom.xml

由于刚才勾选了一些组件,因此会自动引入对应的依赖。

我们再添加一些其他的依赖,分别为:

        thymeleaf、test、mysql、devtools(方便我们在线build项目)

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            mysql
            mysql-connector-java
            
        

        
            org.springframework.boot
            spring-boot-devtools
            true
        

3. 核心配置文件 application.properties

配置数据库连接、mybatis的一些设置。

spring.datasource.url=jdbc:mysql://localhost:3306/community
spring.datasource.username=root
spring.datasource.password=1472
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# MyBatisProperties
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.nowcoder.mycommunity.entity
mybatis.configuration.use-generated-keys=true
mybatis.configuration.map-underscore-to-camel-case=true

4. 创建实体类 entity

右键com.nowcoder.mycommunity新建三个实体类,起名为:entity.XXX:

①DiscussPost

该实体类对应数据库中的discuss_post表。

package com.nowcoder.mycommunity.entity;
import java.util.Date;

//对应数据库中的discuss_post表
public class DiscussPost {
    private int id;
    private int userId;
    private String title;
    private String content;
    private int type;          //帖子类型
    private int status;        //帖子状态:正常、拉黑
    private Date createTime;
    private int commentCount;  //评论数量
    private double score;      //帖子得分

    //get()、set()
    //toString()
}

②User

该实体类对应于User表。

package com.nowcoder.mycommunity.entity;

import java.util.Date;

//对应数据库中的user表
public class User {
    private int id;
    private String username;
    private String password;
    private String salt;
    private String email;
    private int type;
    private int statue;
    private String activationCode;
    private String headerUrl;
    private Date createTime;

    //get()、set()
    //toString()
}

③Page

该实体类对应于后面的分类功能。

由于分类功能需要进行数据的处理,直接将许多方法、参数封装到此类中。

package com.nowcoder.mycommunity.entity;

// 封装分页相关的信息
public class Page {
    //当前页码
    private int current = 1; //默认为第一页
    //显示上限
    private int limit = 5;

    //数据总数(用于计算总页数)
    private int rows;
    //查询路径(用于复用分页链接)
    private String path;

    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(){
        //计算公式为: current * limit - limit
        return (current - 1) * limit;
    }

    //获取总页数
    public int getTotal(){
        if(rows % limit == 0){  //可以整除
            return rows/limit;
        }else {
            return rows/limit + 1;
        }
    }

    //获取展示的起始页码
    public int getFrom(){
        int from = current - 2;  //展示前后两页
        //如果前面没有两页,则展示的起始页码为1,有两页及以上,则展示的起始页码为2
        return from < 1 ? 1 :from;
    }

    //获取展示的终止页码
    public int getTo(){
        int to = current + 2;
        int total = getTotal();
        //展示的终止页大于总页数,则展示到总页数,否则展示到to
        return to > total ? total : to;
    }
}

注:该类中有一些get()、set()方法需要合理设置,里面添加了一些条件。

5. 创建Mapper

右键com.nowcoder.mycommunity新建两个接口,起名为:dao.XxxMapper:

Mapper主要是对应于SQL语句的一些增删改查的方法。

刚才每个实体类都对应了一张表,我们自然需要对每张表都编写一些增删改查的方法,因此与表对应的每个实体类都需要创建对应的Mapper方法。

①DiscussPostMapper

import org.apache.ibatis.annotations.Param;
import java.util.List;

@Mapper
public interface DiscussPostMapper {
    //查询用户个人的所有帖子:用户ID、分页的起始行号、每页展示的数目
    List selectDiscussPosts(int userId, int offset, int limit);

    //查询行数:@Param 给参数起别名
    //当SQL语句中需要动态的拼接条件,且方法内只有一个参数时,该参数必须起别名
    int selectDiscussPostRows(@Param("userId") int userId);

}

②UserMapper

package com.nowcoder.mycommunity.dao;
import com.nowcoder.mycommunity.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {
    User selectById(int id);

    User selectByName(String username);

    User selectByEmail(String email);

    int insertUser(User user);

    int updateStatus(int id, int status);

    int updateHeader(int id, String headerUrl);

    int updatePassword(int id, String password);
}

6. 创建Mapper对应的xml文件

刚才创建了两个Mapper接口,里面只有方法,方法中的具体内容(即SQL语句)是放在Mapper对应的xml文件里的。

右键resources - new Directory - 起名:mapper

① discusspost-mapper.xml




    
    
        id, user_id, title, content, type, status, create_time, comment_count, score
    

    
        select count(id)
        from discuss_post
        where status != 2
        
            and user_id = #{userId}
        
    

②user-mapper.xml




    
        id, username, password, salt, email, type, status, activation_code, header_url, create_time
    

    
    
        username, password, salt, email, type, status, activation_code, header_url, create_time
    

    
        select 
        from user
        where username = #{username}
    

    
                        
                    
                
            
        
    

    
    
        
            
                
                
  • 最新
  • 最热
新帖发布
提示 发布完毕!
  • 备战春招,面试刷题跟他复习,一个月全搞定! 置顶 精华 寒江雪 发布于 2019-04-15 15:32:18
    • 赞 11
    • |
    • 回帖 7
  • 关于我们
  • 加入我们
  • 意见反馈
  • 企业服务
  • 联系我们
  • 免责声明
  • 友情链接
  • 公司地址:北京市朝阳区大屯路东金泉时代3-2708北京牛客科技有限公司
  • 联系方式:010-60728802(电话)    admin@nowcoder.com
  • 牛客科技©2018 All rights reserved
  • 京ICP备14055008号-4      京公网安备 11010502036488号

10. 难点说明

①首先Page类与控制器方法的一些交互

Page类有4个属性:当前页、显示页数的上限、总页数、查询路径。

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

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

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