Springboot:龍游潛底招蝦戲

1、Idea 編譯的時候找不到ServletContext

Exception:Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext

Remark:Idea 集成maven引入jar包時scope設置問題導致的,引入依賴provided,Idea 找不到當前相應的引用類

The Solution

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
 <!--<scope>provided</scope>--> 將scope註釋,重新編譯
</dependency>
2、Springboot @Configuration 獲取不到@Value 值問題

Exception:Springboot @Configuration 獲取不到@Value 值問題,配置是默認的application.properties

Remark:Springboot 與shiro整合導致@Value獲取不到具體值,shiro生命週期bean導致該問題

@Bean
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
   return new LifecycleBeanPostProcessor();
}

The Solution

1、去掉當前的生命週期bean
2、在生命週期中添加static修飾符
//
@Bean
public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
    return new LifecycleBeanPostProcessor();
}
3、Springboot 錯誤頁面重載

Exception:Web端請求URL不存在,直接跳轉/error錯誤頁面。

Remark:/error當前頁面無法通過controller來直接接收,查詢相關資料,實現View的render方法,重載當前請求。

The Solution

i、實現View的render方法,設置當前Http跳轉請求,自定義爲/global/error。

public class LmErrorView implements View {
    @Override
    public String getContentType() {
        return "text/html";
    }
    @Override
    public void render(Map<String, ?> map, HttpServletRequest httpServletRequest
    , HttpServletResponse httpServletResponse) throws Exception {
        httpServletRequest.getRequestDispatcher("/global/error").forward(httpServletRequest, httpServletResponse);
    }
}

ii、自定義WebConfig 繼承 WebMvcConfigurationSupport 重載error錯誤

@Configuration
public class WebConfig  extends WebMvcConfigurationSupport {

    @Bean("error")
    public LmErrorView error() {
        return new LmErrorView();
    }
}
4、Springboot mapper 生成 pojo 區分大小寫

Remark:mapper默認生成的都是小寫,導致數據映射很不方便。
The Solution:在generatorConfiguration添加區分大小寫標識,改成駝峯寫法。

<?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>
    <properties resource="application.properties"/>

    <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.lmeng.generate.MyMapper"/>
        </plugin>

        <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
        </jdbcConnection>

        <!-- 生成實體類所在包-->
        <javaModelGenerator targetPackage="com.lmeng.pojo" targetProject="src/main/java"/>

        <!-- 生成Mapper所在目錄-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
        <!-- 生成Mapper對應的java映射-->
        <javaClientGenerator targetPackage="com.lmeng.mapper" targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <!--<table tableName="role"></table>-->
        <table tableName="ticket">
            <property name="useActualColumnNames" value="true"/> //添加區分大小寫
        </table>

    </context>
</generatorConfiguration>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章