项目演示地址:http://www.mawen.co/

快速搭建sprintboot项目

运行第一个springboot项目

leaf

package hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; @Controller
public class GreetingController { @GetMapping("/greeting")
//其中第一个name为key,第二个String name是用来接收值的
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";//返回template目录查找页面
} }

一个错误是:

model.addAttribute("name", name);//两个name的位置写反了导致运行的时候页面识别不到出现null

使用github托管项目

{% asset_img 2019-08-10_08-49-47.png %}

设计使用idea中terminal终端时,由于之前把git重新安装在D盘,而idea识别的是C盘里面卸载不干净的git目录,因此出现

'git' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

解决方法:Idea中Terminal命令不能执行git命令,因为是默认是cmd,按照下图改为bash就可以了

{% asset_img 20170611035122285.jpg %}

git add .

添加当前所有内容到暂存区里面区

$ git commit -m "add README"

添加这条记录并描述

git commit --amend --no-edit

--amend 表示追加

--no-edit 表示不编辑

git push

使用这个命令后github上才会出现相应的文件

明确需求

参考网站

初识Bootstrap

bootstrap中文网

通过快速的前端框架搭好页面

介绍十二等份栅格系统实现响应式布局

Bootstrap编写导航栏样式

引入三个包:样式文件,css文件,js文件

<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

index.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>码匠社区</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<script src="js/bootstrap.min.js" type="application/javascript"></script>
</head>
<body>
</body>
</html>

注册Github app

building-oauth-apps

图解Github登录流程

编写时序图:表示对象和对象通过时间消息的路线

{% asset_img 2019-08-10_10-42-05.png %}

Github登录之调用authorize

GET https://github.com/login/oauth/authorize

<li><a href="https://github.com/login/oauth/authorize?client_id=251734b4a67768f93428&redirect_uri=http://localhost:8887/callback&scope=user&state=1">登录</a></li>

演示结果:

http://localhost:8887/callback?code=2bbe5affd7594f4d8a8c&state=1

Github登录之获取code

@Controller
public class AuthorizeController { @GetMapping("/callback")
public String callback(@RequestParam(name = "code") String code,
@RequestParam(name = "state") String state){
return "index";
}
}

OKHTTP

Github登录之获取用户信息

**Post to a Server¶**

public static final MediaType JSON
= MediaType.get("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}

@Controller 把当前的类作为路由API的承载者

@Component 仅仅把当前类初始化到spring的上下文(可以理解为不需要实例化该类的对象)

参数

名称 类型 描述
client_id string 需要。您从GitHub收到的GitHub应用程序的客户端ID。
client_secret string 需要。您从GitHub收到的GitHub应用程序的客户机密。
code string 需要。您收到的代码作为对第1步的回复。
redirect_uri string 应用程序中的URL,用于在授权后发送用户。
state string 您在步骤1中提供的不可思议的随机字符串。

需要的okhttp的依赖

        <dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.1</version>
</dependency>

maven仓库 查找fastjson

配置Application.properties

    @Value("${github.client.id}")
private String clientId; @Value("${github.client.secret}")
private String clientSecret; @Value("${github.redirect.uri}")
private String redirectUri;
github.redirect.uri=http://localhost:8887/callback
github.client.secret=395672ecc7481d4aa5a6cee4945d11a09bbe0a4d
github.client.id=251734b4a67768f93428

细说session和cookies的原理及实现

cookie类似银行卡,而session类似银行

每次通过浏览器的cookie去访问服务器的session,即拿卡向银行取钱

图解MySQL

小匠老师阐述了对于程序员来说画图的重要性,这让我想起来我的uml老师发哥,摸了一学期的鱼,应付完考试什么关系全都忘了...

初识H2数据库

此数据库可以在嵌入模式或服务器模式下使用。要在嵌入模式下使用它,您需要:

  • 添加h2*.jar到类路径(H2没有任何依赖项)
  • 使用JDBC驱动程序类: org.h2.Driver
  • 数据库URL 在用户主目录中jdbc:h2:~/test打开数据库test
  • 将自动创建一个新数据库

Maven依赖

<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
<scope>test</scope>
</dependency>

集成MyBatis并实现插入操作

2019-08-12 16:37:32.452  INFO 2064 --- [nio-8887-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-08-12 16:37:32.452 INFO 2064 --- [nio-8887-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'

第一次 出现上面这种错误,表现为控制台出现这两串代码后网页显示跳转找不到页面或者页面错误,最后发现是修改了application.properities文件的端口号,我修改成了8888,然而原本的controller里面代码的端口号还是8887,这就导致后面找不到页面然后出错。

配置H2数据库

CREATE USER IF NOT EXISTS sa PASSWORD '123';
ALTER USER sa admin true ;
create table user
(
id int auto_increment,
account_id varchar(100),
name varchar(50),
token char(36),
gmt_create bigint,
gmt_modified bigint,
constraint user_pk
primary key (id)
);

由于H2 数据库每次只能准许一个用户进行操作,否则就会出错,这一点很恼人。

ERROR:500,服务器异常

H2数据库的账户名和密码错误:需要在第一次初始化H2数据库的时候就配置好数据库的用户名还有密码,按照教程配置

username:sa
password:123<hidden>
@Mapper
public interface UserMapper {
@Insert("insert into user (name,account_id,gmt_create,gmt_modified) values (#{name},#{accountId},#{gmtCreate},#{gmtModified})")
void insert(User user);

上面这个错误会报找不到account_id,实际上是后面的accountId我写成了和前面一样的account_id,mapper这里用的是user对象的属性和数据库的字段不一样的时候要好好填写。

还有一个低级错误是插入成功后数据库显示某个字段为null,结果是mapper里面的sql语句没有写进去。

后面总算是搞定数据库这一个大坑了。

实现登录状态持久化获取

访问主页的时候,获取token然后查询数据库

如果数据库有,那么直接登录

小tips:编写mapper的时候,如果参数是个类,则spring会自动绑定变量到sql,如果是某个变量或者参数,则需要一个注解@Param("name")

集成 Flyway Migration

flyway的maven使用

            <plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>5.2.4</version>
<configuration>
<url>jdbc:h2:file:./target/foobar</url>
<user>sa</user>
</configuration>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
</dependencies>
</plugin>

注意:需要大写V递增开头

rm ~/community.*
mvn flyway:migrate

使用Bootstrap编写发布问题页面

完成发布文章功能

create table question
(
id int auto_increment,
title varchar(50),
description text,
gmt_create bigint,
gmt_modified bigint,
creator int,
constraint question_pk
primary key (id)
);

添加lombok支持

这就是个减少setter和getter方法的使用的工具

需要安装在idea安装相应的plugins

完成首页问题列表功能

如果发现写的样式没有实现,那么可能是没引用css文件

问题答疑

自动部署

讲了一个livereload热部署的插件以及依赖安装,可以实现自动重启服务的功能

分页原理和实现

看到第60分钟,顶不住了,不学了今天妈的

完善导航栏并进行页面拆解

导入jquery

通过thymeleaf抽取导航栏成单个文件

用到fragment insert两个th标签

个人资料发布问题列表实现

还是分页的问题 看得心态爆炸 妈的

坚持坚持...

拦截器

把cookie这部分放到拦截器,使代码复用

通过原码分析静态资源无法加载的问题

修复登录功能

完成更新功能

逆向工程:集成 MyBatis Generator

MBG官网文档

mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

XML Configuration Reference

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration>
<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" /> <context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
connectionURL="jdbc:db2:TEST"
userId="db2admin"
password="db2admin">
</jdbcConnection> <javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator> <sqlMapGenerator targetPackage="test.xml" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="\MBGTestProject\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table> </context>
</generatorConfiguration>

主要是根据官方文档学会看各种标签代表的意义。

善用查询快捷键以及翻译插件

遇到一个bug:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): life.majiang.community.community.mapper.UserMapper.selectByExample

结果发现就是配置文件里面classpath写成了class

mybatis.mapper-locations=classpath:mapper/*.xml

真的是coding5分钟,debug两小时...

使用 ControlerAdvice 和 ExceptionHandler 通用处理异常

实现阅读数功能

初识API

侧边栏文件

红色:表示没有放到暂存空间

绿色:没变化的

蓝色:有变化的

异常处理

49

添加事务

页面提交回复

springboot 论坛项目的更多相关文章

  1. ASP.NET Core 开源论坛项目 NETCoreBBS

    ASP.NET Core 轻量化开源论坛项目,ASP.NET Core Light forum NETCoreBBS 采用 ASP.NET Core + EF Core Sqlite + Bootst ...

  2. 手把手教你从零开始搭建SpringBoot后端项目框架

    原料 新鲜的IntelliJ IDEA.一双手.以及电脑一台. 搭建框架 新建项目 打开IDE,点击File -> New Project.在左侧的列表中的选择Maven项目,点击Next. 填 ...

  3. SpringBoot Mybatis项目中的多数据源支持

    1.概述 有时项目里里需要抽取不同系统中的数据源,需要访问不同的数据库,本文介绍在Springboot+Mybatis项目中如何支持多数据源操作. 有需要的同学可以下载 示例代码 项目结构如下: 2. ...

  4. IDEA+Maven+多个Module模块(创建多模块SpringBoot整合项目)

    最近在学习springboot,先从创建项目开始,一般项目都是一个项目下会有多个模块,这里先创建一个最简单的实例,一个项目下有一个springboot模块项目提供web服务,引用另一个java项目(相 ...

  5. 如何在spring-boot web项目中启用swagger

    swagger的三个项目及其作用 我们打开swagger的官网,会发现有三个swagger相关的项目,它们分别是 swagger-editor 作用是通过写代码,生成文档描述(一个json文件或其他格 ...

  6. springboot将项目源代码打包

    springboot将项目源代码打包并发布到仓库 如果我们有一些类和方法是公用的,可以打开公用包,而这时使用默认的build方式都所有依赖都打进去,而且你当然项目的文件虽然在包里,但却在boot-in ...

  7. springboot maven项目,为什么build成功,build path也没错误,project-->clean 也没用,项目上面还是有个红x呢?

    springboot maven项目,为什么build成功,build path也没错误,project-->clean 也没用,项目上面还是有个红x呢? 看错误信息有提示:  Descript ...

  8. springboot获取项目跟目录

      springboot部署之后无法获取项目目录的问题: 之前看到网上有提问在开发一个springboot的项目时,在项目部署的时候遇到一个问题:就是我将项目导出为jar包,然后用java -jar ...

  9. 工具IDEA 配置springboot+maven项目

    工具IDEA 配置springboot+maven项目 首先安装IDEA,至于怎么安装就不介绍了.. 第一步 配置maven环境 首先安装maven,先在网上下载一个maven包.在IDEA的sett ...

随机推荐

  1. Ubuntu 出现 Invalid operation update 或 Invalid operation upgrade的解决办法

    输入 sudo apt update && sudo apt full-upgrade

  2. find命令计算代码行数

    [anonymous@localhost ~/lvs/ipvsadm- -regex '.*Makefile.*' -o -regex '.*\.[ch]' -exec cat {} \; | wc ...

  3. InnoDB缓存---InnoDB Buffer Pool

    InnoDB Buffer Pool 定义 对于InnoDB存储引擎,不管用户数据还是系统数据都是以页的形式存储在表空间进行管理的,其实都是存储在磁盘上的. 当InnoDB处理客户端请求,需要读取某页 ...

  4. Nginx-HTTP之listen指令解析

    1. ngx_http_core_listen static char * ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void ...

  5. P3378 【模板】堆 (内含左偏树实现)

    P3378 [模板]堆 题解 其实就是一个小根堆啦,STL就可以解决,但是拥有闲情雅致的我学习了Jelly_Goat的左偏树,增加了代码长度,妙啊 Solution 1 STL STL 里面prior ...

  6. spring boot 全局异常处理及自定义异常类

    全局异常处理: 在处理controller层抛出的自定义异常时,可以实现@ControllerAdvice注解捕获,配合@ExceptionHandler来增强所有的@requestMapping方法 ...

  7. close connection error java.sql.SQLRecoverableException: IO Error: Broken pipe

    java.sql.SQLRecoverableException: IO Error: Broken pipe Table of Contents 1. 错误信息 2. 分析 2.1. 连接池 2.2 ...

  8. JSTL优点

    1. 在应用程序服务器之间提供了一致的接口,最大程序地提高了WEB应用在各应用服务器之间的移植. 2. 简化了JSP和WEB应用程序的开发.3. 以一种统一的方式减少了JSP中的scriptlet代码 ...

  9. SQL Server 时间戳与时间格式互相转换

    时间戳(Unix timestamp) 是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数. Unix时间戳不仅被使用在Unix系统.类Unix系统中,也 ...

  10. centos7修复grub2

    GRUB  :“the Grand Unified Bootloader ”引导加载程序 1.主要配置文件 #/boot/grub2/grub.cfg #rm -rf /boot/grub2/grub ...