# community **Repository Path**: lemzdo/MyCommunity ## Basic Information - **Project Name**: community - **Description**: 基于springboot新建的community社区项目 - **Primary Language**: Java - **License**: MulanPSL-1.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2020-05-17 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## community 基于springboot项目搭建的community社区 ## 资料 [Spring 文档](https://spring.io/guides) [Spring Web](https://spring.io/guides/gs/serving-web-content/) [es](https://elasticsearch.cn/explore) [Github deploy key](https://developer.github.com/v3/guides/managing-deploy-keys/#deploy-keys) [Bootstrap](https://v3.bootcss.com/getting-started/) [Github OAuth](https://developer.github.com/apps/building-oauth-apps/creating-an-oauth-app/) [原生java代码实现码云第三方验证登录](https://blog.csdn.net/qq_40565265/article/details/105103701) [码云OAuth官方文档](https://gitee.com/api/v5/oauth_doc#/list-item-1) [Spring](https://docs.spring.io/spring-boot/docs/2.0.0.RC1/reference/htmlsingle/#boot-features-embedded-database-support) [菜鸟教程](https://www.runoob.com/mysql/mysql-insert-query.html) [Thymeleaf](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-attribute-values) [Spring Dev Tool](https://docs.spring.io/spring-boot/docs/2.0.0.RC1/reference/htmlsingle/#using-boot-devtools) [Spring MVC](https://docs.spring.io/spring/docs/5.0.3.RELEASE/spring-framework-reference/web.html#mvc-handlermapping-interceptor) [Markdown 插件](http://editor.md.ipandao.com/) [UFfile SDK](https://github.com/ucloud/ufile-sdk-java) [Count(*) VS Count(1)](https://mp.weixin.qq.com/s/Rwpke4BHu7Fz7KOpE2d3Lw) [MyBatis逆向工程,Example的使用](https://blog.csdn.net/biandous/article/details/65630783) ## 工具 [Git](https://git-scm.com/download) [Visual Paradigm](https://www.visual-paradigm.com) [Flyway](https://flywaydb.org/getstarted/firststeps/maven) [Lombok](https://www.projectlombok.org) [ctotree](https://www.octotree.io/) [Table of content sidebar](https://chrome.google.com/webstore/detail/table-of-contents-sidebar/ohohkfheangmbedkgechjkmbepeikkej) [One Tab](https://chrome.google.com/webstore/detail/chphlpgkkbolifaimnlloiipkdnihall) [Live Reload](https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei/related) [Postman](https://chrome.google.com/webstore/detail/coohjcphdfgbiolnekdpbcijmhambjff) [前端小图标](https://www.iconfont.cn/) [阿里云OSS上传1](https://blog.csdn.net/lianzhang861/article/details/82776495) [阿里云OSS上传2](https://blog.csdn.net/ckyblack/article/details/102853569) [阿里云OSS上传3](https://blog.csdn.net/u012832579/article/details/83575354) ##搭建流程 springboot框架搭建一个项目 类似中文社区 1.页面部分: 上面导航栏,搜索框,登录按钮 一些标签 正文:一些文章 时间倒序排列 评论数,发布时间,浏览数 右边是一些热门话题 分页功能 使用Bootstrap快速搭建页面 html中引入Bootstrap提供的css和js文件 Bootstrap官网搜索想要的组件 复制代码进行修改即可 2.git操作: 第一次git Git 全局设置: git config --global user.name "lemzdo" git config --global user.email "2897468268@qq.com" 向 git 仓库中push代码 git init git add . git commit -m "first commit" git remote add origin git@gitee.com:lemzdo/MyCommunity.git git push -u origin master 之后每次push 直接IDEA中项目右键-git-commit 成功后右键-git-repository-push 3.编码部分: 依赖: 前端页面thymeleaf OkHttp 用于发送get,post请求 fastjson 用于快速转换json格式字符串(gitee第二次返回的带access-token的信息,和第三次返回的带user信息的串) mysql 用于存取用户信息,创建时间等等 MyBatis 用于操作mysql数据库 还有逆向工程的插件用于自动生成实体类,mapper,映射文件 手动指定文件夹为resources lombok 用于简化数据库实体类代码,直接使用lombok的@Data注解即可 pageHelper mybatis分页插件,用于快速分页 commons-lang3 apache提供的工具包,内含StringUtils等工具类帮助代码简洁,可以快速判断非空等 阿里云oss依赖 用于使用阿里云进行文件(图片)的上传 主配置文件application: thymeleaf配置: 设置thymeleaf前端页面的前后缀(可选),关闭缓存来完成界面实时更改,不用重启服务 configuration中需要设置成update resources tomcat配置: 设置上下文根和端口号(可选) 自定义配置: 配置第三方登录所需要的gitee配置,避免代码写死,在代码中使用@Value("${key}")注解可以取得这些值 mysql配置: 配置数据库url,username,password,driver lombok 用于简化数据库实体类代码,直接使用lombok的@Data注解即可 pageHelper mybatis分页插件,用于快速分页 commons-lang3 apache提供的工具包,内含StringUtils等工具类帮助代码简洁,可以快速判断非空等 自定义的gitee配置: id,secret,url用于第三方跳转登录使用,通过@Value("${key}")获取 自定义的阿里云需要的配置: endpoint,accessKeyId,accessKeySecret,bucketName 4.登录功能: 所谓第三方登录,实质就是 OAuth 授权。用户想要登录 A 网站, A 网站让用户提供第三方网站的数据,证明自己的身份。 获取第三方网站的身份数据,就需要 OAuth 授权。 community主页index 点击登录按钮,发送请求到gitee 登录按钮url https://gitee.com/oauth/authorize? client_id=58c3f0dbf03cfb5c7e12b14b9c1d7647d8922cc1167b392fcf0df905e642d474 &redirect_uri=http://localhost:8888/community/login&response_type=code gitee第三方授权返回一个授权码code 通过code访问gitee,gitee返回一个json串,剪切可以得到我们需要的access-token https://gitee.com/oauth/token?grant_type=authorization_code &code={code}&client_id={client_id}&redirect_uri={redirect_uri} &client_secret={client_secret} 通过access-token访问gitee,gitee返回一个json串,里面是登录的用户的信息,可以从中获取我们需要的信息,封装成userDTO对象 "https://gitee.com/api/v5/user?access_token=" + access-token 5.发帖功能: 标题栏不变,index基础上修改 编写数据库对应帖子的表,用于存储数据(评论数,发帖人,点赞数等等) 未登录发帖报错弹框 后端验证标题,文本非空(实际开发中前后端都需要去验证) 6.主页面显示帖子功能 左边头像,右边主题和浏览数,评论数,发帖时间等等 发帖时间thymeleaf表达式:${#dates.format(post.gmtCreate,'yyyy-MM-dd HH:mm')} post.gmtCreate加上${}会报错 post数据库没有头像,需要将post通过作者id关联user,拿到头像 新定义一个DTO类,存放post所有属性和user,用于存放需要展示到前端的各个属性 需要遍历显示出所有的帖子 thymeleaf自带each循环 th:each 参数1,参数2:参数3 参数1 user 当前循环的对象变量名称(随意,见名知意) 参数2 userStat 当前循环对象状态的变量 有一些属性,索引,数数,第一个,最后一个等等 可以不写, 默认存在 默认名称对象名+Stat 参数3 ${userList} 当前循环的集合 7.帖子分页功能 mysql语法 select * from post limit 起始记录的index(从0开始),每页显示的记录条数; (页数-1)*每页记录数 = 起始index(索引) 页数显示在前端,后端查询记录靠每页记录数和index 可以直接使用mybatis的分页插件 pageHelper完成 https://pagehelper.github.io/docs/howtouse/ https://blog.csdn.net/qq_27317475/article/details/81168241 8.页面拆解 所有页面公用一个导航栏 即发帖,登录,社区 thymeleaf提供拆解功能,可以拆解并在需要的地方引入 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-attribute-values 新建模板 footer.html