1. 程式人生 > >樂優商城第一天(Springboot)

樂優商城第一天(Springboot)

傳智部落格的的新專案樂優商城,很幸運可以學新專案,哈哈哈

我們是第一期學到樂優商城的,真的好爽。當得到訊息,我們第一個專案是樂優商城的時候,我都激動死了,感覺可以學到時髦的springboot和springcloud真是再好不過的事情。可是,我們班有些人竟然說自己是小白鼠,可是在我看來,這是天大的好事。幸好,最後上面計劃沒有改變。我還是如願上到了樂優商城。

第一天遇到兩個坑,花費了我好幾個小時,值得寫一波

第一個坑

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun May 20 17:19:12 CST 2018

There was an unexpected error (type=Internal Server Error, status=500).

### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'note' in 'field list' ### The error may exist in com/leyou/mapper/UserMapper.java (best guess) ### The error may involve com.leyou.mapper.UserMapper.selectByPrimaryKey-Inline ### The error occurred while setting parameters ### SQL: SELECT id,user_name,password,name,age,sex,birthday,created,updated,note FROM tb_user WHERE id = ? ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'note' in 'field list' ; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'note' in 'field list'

報這個錯誤的原因是pojo類比它對應的表中多一個欄位,把哪個多餘的欄位刪除即可。

通用mapper應該是通過pojo類生成具體的sql語句的,如果pojo類中多一個欄位,那麼查詢的時候,這個欄位就會存在於sql語句中,這樣就導致了這個錯誤。

解決方法:刪掉pojo類中多餘的欄位

第二個坑:

Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver

原因:springboot自帶版本管理,我只需要引入相關依賴,而不用引入具體版本,沒想到mysql的驅動沒有載入進來,這個是萬萬沒想到的,配置檔案配置的時候也出錯

這裡driver是紅色的,證明沒有載入進來,坑啊,mysql驅動是5.1.45這個版本,我把本地庫裡的刪了,又重新載入了一下就好了,都是淚,花費了好幾個小時研究這個問題,各種清空沒下載完成的都沒用,沒想到是下載好的完整的jar包出了問題。

解決方案:在maven的本地倉庫中找到mysql驅動對應 的版本,去依賴庫中刪除,重新下載就好了。

進入正題,springboot入門

1.入門專案

  • 匯入springboot的父啟動器,起著依賴管理的作用
  • <!--springboot的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
  • 入門專案是一個web專案,匯入web的啟動器,另外jdk的版本設定為1.8
  • <!--web專案,匯入mvc啟動器-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <!--java的版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>
  • springboot啟動是通過main方法的,需要一個main方法
  • @SpringBootApplication
    public class Application {
    
        public static void main(String[] args){
            SpringApplication.run(Application.class,args);
        }
    }
  • 再寫一個Controller,啟動main方法就可以訪問資源
  • @RestController
    public class HelloController {
    
        @GetMapping("hello")
        public String hello(){
            return "hello springboot";
        }
    }
    
    其中RestController規定了返回值必須是json,相當於@ResponseBody和@controller的結合

       GetMapping相當於

  • @RequestMapping(method = RequestMethod.GET)

2.入門案例執行原理

主要原因是3個註解

@SpringBootConfiguration宣告類是一個配置類

@EnableAutoConfiguration自動配置,根據依賴去猜測該配置什麼

@ComponentScan註解的自動掃描,相當於之前的<context:component-scan>,如果不左任何配置的話,預設掃描的包是出現@SpringbootApplication註解的後面的包,所以一般啟動類放在所有類的前面。

3.springboot是如何消滅xml的

java註解的方式

@configuration = xml檔案

@Bean = <Bean>

@value = <property 屬性注入> 只能注入基本的型別,不能注入複雜的型別

@PropertySource =

springboot的方式

先用一個類A來取出配置檔案中的資料

@ConfigurationProperties 這個註解定義的類來封裝application.properties中的資料

在配置類中通過註解引入類A

@Configuration

@EnableConfigurationProperties

在配置類中引入類A的物件有三種方式

  • 方法引數中注入
  • 構造方法注入
  • Autowired注入

通過@Bean進行屬性的注入,

如果這個配置只在一處使用,那麼就沒必要寫一個配置類,我們直接把外部配置引入到配置類中

 

4.springboot如何新增攔截器

springboot中使用攔截器,

  • 自定義一個攔截器,
  • 一個類通過實現WebMvcConfigurer並新增@Configuration,
  • 在這個類中重寫   addInterceptors方法,這裡還有一點巧妙的是通過@Bean的方式,將日誌引入
    @Bean
    public LoginInterceptor loginInterceptor(){
        return new LoginInterceptor();
    }
    
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.loginInterceptor()).addPathPatterns("/**");
    }

回顧:springmvc實現攔截器的方式

首先自定義一個攔截器

然後在配置檔案有兩種方式配置

<!-- 配置自定義的攔截器 -->

<mvc:interceptors>

<!-- 方式1:直接在這裡配置<bean> 對所有的Controller都攔截 -->

<bean class="cn.itcast.pojo.MyInterceptor1"/>

<!-- 方式2:通過mvc:interceptor來配置,同時可以指定要攔截的路徑: -->

<mvc:interceptor>

<!-- 指定要攔截的路徑,這裡可以寫固定路徑,也可以使用Ant風格萬用字元,/**代表就是任意層數任意路徑了 -->

<mvc:mapping path="/**"/>

<bean class="cn.itcast.pojo.MyInterceptor2"/>

</mvc:interceptor>

</mvc:interceptors>

5.與mybaits整合,實現增刪改查

1.引入jdbc技術的啟動器

2.引入mysql的依賴,告訴springboot用的是mysql的驅動

6.Thymeleaf

一個html,既可以讓前端看到靜態效果,也可以讓後端調試出動態效果。

使用時,需要引入啟動器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

其他:

1.springboot使用日誌的級別

# 設定com.leyou包的日誌級別為debug
logging.level.com.leyou=debug

2.jdk8新特性,介面方法的預設實現,好處是,這樣減去用一個A類空實現這個介面中的方法,然後B類再繼承A類這樣的事情

報錯:java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider.<init>()

應該導下面這個包,否則會報錯

@樂優商城