zoukankan      html  css  js  c++  java
  • springboot 论坛项目

    项目演示地址: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="MBGTestProjectsrc">
          <property name="enableSubPackages" value="true" />
          <property name="trimStrings" value="true" />
        </javaModelGenerator>
    
        <sqlMapGenerator targetPackage="test.xml"  targetProject="MBGTestProjectsrc">
          <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
    
        <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao"  targetProject="MBGTestProjectsrc">
          <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

    添加事务

    页面提交回复

    我曾难自拔于世界之大 也沉溺于其中梦话 不得真假 不做挣扎 不惧笑话
  • 相关阅读:
    php实现多张图片打包下载
    bootstrap的简单学习
    接口的简单调用模式
    oracle 日期常用函數 (SYSDATE、日期格式)
    php通过ajax请求数据后台返回数据要求和前台解析
    jquery+ajax实现分页
    Fatal error: Call to undefined function curl_init()问题
    利用php比较精确的统计在线人数的办法
    sql服务器启动不了问题
    字符串字母大小写转换(10)
  • 原文地址:https://www.cnblogs.com/senup/p/11974679.html
Copyright © 2011-2022 走看看