欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot項(xiàng)目中遇到的BUG問題及解決方法

 更新時(shí)間:2020年11月23日 11:10:02   作者:Riqk_Qin  
這篇文章主要介紹了SpringBoot項(xiàng)目中遇到的BUG問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.啟動(dòng)項(xiàng)目的時(shí)候報(bào)錯(cuò)

1.Error starting ApplicationContext.
To display the auto-configuration report re-run your application with 'debug' enabled.

解決方法:

在yml配置文件中加入debug: true,因?yàn)槟J(rèn)的話是false

2.在集成mybatis時(shí)mapper包中的類沒被掃描

org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.app.mapper.UserMapper' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

解決方法:

在springboot的啟動(dòng)類中加入@MapperScan("mapper類的路徑")
或者直接在Mapper類上面添加注解@Mapper,建議使用上面那種,不然每個(gè)mapper加個(gè)注解也挺麻煩的

3.在向數(shù)據(jù)庫插入數(shù)據(jù)時(shí)報(bào)錯(cuò)

"\r\n### Error updating database. Cause:
com.mysql.jdbc.MysqlDataTruncation:
Data truncation: Data too long for column 'password' at row 1\r\n###

數(shù)據(jù)庫表password這個(gè)字段太短了,應(yīng)該設(shè)長點(diǎn)

java.lang.ClassCastException: 
com.app.entity.User cannot be cast to java.lang.Integer

4.用mybatis查詢時(shí)報(bào)錯(cuò)

org.mybatis.spring.MyBatisSystemException: 
nested exception is org.apache.ibatis.binding.BindingException: 
Parameter 'user_type' not found. Available parameters are [2, 1, 0, param1, param2, param3]

原因:@Param注解缺失,當(dāng)只有一個(gè)參數(shù)時(shí),Mapper接口中可以不使用

public User getUser(String name);

有多個(gè)參數(shù)時(shí)就必須使用

public User getUser(@Param("name") String name,@Param("password") String password); 

5.Mybatis查詢傳入一個(gè)字符串傳參數(shù)報(bào)錯(cuò)

mapper接口:
PkRecord findByPkStudentNumber(String pkStudentNumber);
對(duì)應(yīng)的mapper配置文件
<select id="findByPkStudentNumber" resultMap="recordMap" >
 SELECT * FROM pk_record
 <where>
 <if test="pkStudentNumber!=null">
 pk_student_number=#{pkStudentNumber}
 </if>
 </where>
 </select>

然后就會(huì)報(bào)如下錯(cuò)誤

There is no getter for property named 'XXX' in 'class java.lang.String'

原因:

Mybatis默認(rèn)采用ONGL解析參數(shù),所以會(huì)自動(dòng)采用對(duì)象樹的形式取string.num值,引起報(bào)錯(cuò)。

解決方法:

①在mapper配置文件中參數(shù)名,都要改成_parameter

<select id="findByPkStudentNumber" resultMap="recordMap" >
 SELECT * FROM pk_record
 <where>
 <if test="_parameter!=null">
 pk_student_number=#{_parameter}
 </if>
 </where>
 </select>

②在mapper接口中用@Param在相關(guān)方法說明參數(shù)值

PkRecord findByPkStudentNumber(@Param("pkStudentNumber") String pkStudentNumber);

6.mybatis返回值報(bào)錯(cuò)

org.apache.ibatis.binding.BindingException: 
Mapper method 'com.hoomsun.mybatis.dao.CostMapperDao.dongtaislq' 
has an unsupported return type: class java.lang.String

dao接口類中對(duì)應(yīng)的方法去掉返回值,用void,例如:

public void dongtaislq(Map map);

7.mybatis中集合與Stirng類型的比較

報(bào)錯(cuò)信息

invalid comparison: java.util.ArrayList and java.lang.String

原因:無法比較這兩種類型

<if test="categoryIds!=null and categoryIds!=' ' ">
 AND category_id IN
 <foreach collection="categoryIds" item="categoryIds" open="(" separator="," close=")">
 #{categoryIds}
 </foreach>
</if>

在接收list的時(shí)候加了判斷 list !=' ',引起了集合與Stirng類型的比較,所以報(bào)錯(cuò),將判斷條件改為 : list.size >0就可以了

<if test="categoryIds!=null and categoryIds.size>0" >
 AND category_id IN
 <foreach collection="categoryIds" item="categoryIds" open="(" separator="," close=")">
 #{categoryIds}
 </foreach>
</if>

8.保存對(duì)象數(shù)據(jù)進(jìn)數(shù)據(jù)庫后根據(jù)ID查詢并返回該對(duì)象時(shí)為null

<insert id="saveUser" useGeneratedKeys="true" keyColumn="id" >
 insert into user(username,password,nickname) values (#{username},#{password},#{nickname})
</insert>

這樣寫的話數(shù)據(jù)可以保存到數(shù)據(jù)庫,沒問題,ID也可以自動(dòng)增長,不過保存后立刻根據(jù)ID查詢時(shí)返回會(huì)為null
解決的方法是把keyColumn換成keyProperty就可以了

<insert id="saveUser" useGeneratedKeys="true" keyProperty="id" >
 insert into user(username,password,nickname) values (#{username},#{password},#{nickname})
</insert>

9.idea運(yùn)行項(xiàng)目時(shí)報(bào)錯(cuò)

//子容器啟動(dòng)失敗
ERROR 8760 --- [cat-startStop-1] org.apache.catalina.core.ContainerBase : A child container failed during start
//未能啟動(dòng)Tomcat組件
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: 
Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]

這里的問題主要是jre環(huán)境沒選好,可能是由于你之前項(xiàng)目要求改變jre,然后導(dǎo)致之前的項(xiàng)目jre環(huán)境也改變了。

idea具有內(nèi)置tomcat,所以可以不用額外配置tomcat

在idea中點(diǎn)擊運(yùn)行→編輯結(jié)構(gòu)→在配置中選擇jre環(huán)境

我這里是選用1.8的環(huán)境

Paste_Image.png

再次啟動(dòng)項(xiàng)目:

Paste_Image.png

啟動(dòng)成功了

10.mybatis插入數(shù)據(jù)時(shí)默認(rèn)值不生效

插入語句

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
 insert into mmall_category (id, name, status)
 values (#{id}, #{name},#{status})
 </insert>

對(duì)應(yīng)的mapper

void insert(Category category);

需要傳入的是一個(gè)對(duì)象,假如你在數(shù)據(jù)庫設(shè)計(jì)時(shí)把status設(shè)置默認(rèn)值
在傳入對(duì)象時(shí)只賦值給name,結(jié)果你可以發(fā)現(xiàn)數(shù)據(jù)庫中status的值是null

這是因?yàn)檫@個(gè)對(duì)象的其他屬性成員你不賦值的話默認(rèn)為null,并且你在sql語句中#{status},也就是把null賦給了status,但是有時(shí)候有需要傳status,不能把#{status}去掉,那該怎么辦呢?
解決方法:

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
 insert into mmall_category 
 (id, name
 <if test="status != null">
 ,status
 </if>)
 values (#{id}, #{name}
 <if test="status != null">
 ,#{status}
 </if>)
 </insert>

使用mybatis的if test進(jìn)行值的判斷,如果是null的話就不賦值

mybatis的高級(jí)結(jié)果映射

association – 一個(gè)復(fù)雜的類型關(guān)聯(lián);許多結(jié)果將包成這種類型
嵌入結(jié)果映射 – 結(jié)果映射自身的關(guān)聯(lián),或者參考一個(gè)

看起來挺難懂的,看下實(shí)例
在resultMap中,有這樣的一個(gè)映射

<association property="user" column="user_id" select="com.mapper.UserMapper.selectByPrimaryKey"/>

當(dāng)你用select查詢出來對(duì)象時(shí)想獲取userId的值要先獲取映射的對(duì)象再獲取其ID,不然直接獲取userId會(huì)為空

11.InterlliJ Debug方式啟動(dòng)特別慢

Method breakpoints may dramatically slow down debugging

不管你是重啟服務(wù)器和重啟idea還是報(bào)這個(gè)問題。由該提示語我們可以知道要把方法斷點(diǎn)給關(guān)掉,查看斷點(diǎn)的快捷方式是Ctrl + Shift +F8

Java Method Breakpoints去掉即可

錯(cuò)誤Caused by: java.lang.IllegalStateException: In the composition of all global method configuration, no annotation support was actually activated

原因:在所有全局方法配置的組合中,實(shí)際上沒有激活注釋支持

解決方法:

在啟動(dòng)類中加入@EnableGlobalMethodSecurity(securedEnabled = true)

@SpringBootApplication
@EnableGlobalMethodSecurity(securedEnabled = true)
public class Application {
 public static void main(String[] args) {
 SpringApplication.run(Application.class, args);
 }
}

12.MyBatis綁定錯(cuò)誤

Invalid bound statement (not found)

這個(gè)錯(cuò)誤主要是因?yàn)?code>mapper接口與mapper.xml的路徑?jīng)]有一一對(duì)應(yīng),并且mapper.xml不能放在src目錄里,配置文件必須放resources里,src目錄下的xml文件默認(rèn)不會(huì)編譯到target。

13.使用請(qǐng)求轉(zhuǎn)發(fā)或者重定向出現(xiàn)異常

java.lang.IllegalStateException: Cannot forward after response has been committed

原因:

報(bào)異常的原因是重復(fù)轉(zhuǎn)發(fā)或者重定向了請(qǐng)求

解決方法:

如果有多個(gè)轉(zhuǎn)發(fā)或者重定向,需要在每個(gè)轉(zhuǎn)發(fā)或者重定向請(qǐng)求之后加上return語句(最后一個(gè)請(qǐng)求轉(zhuǎn)發(fā)或者重定向可以不加)

14.SpringBoot配置數(shù)據(jù)庫連接池,但日志卻每次都新建連接

Mybatis中動(dòng)態(tài)打印SQL語句到控制臺(tái),只需要在SpringBoot配置文件中添加如下配置即可

mybatis:
 configuration:
 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

但是如果沒有用到任何連接池的話,是不會(huì)打印的

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2a5ca7d7] was not 
registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9a51d74] will not be managed by Spring
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2a5ca7d7]

解決方法:

確保有可用的連接池被使用,引入第三方連接池要做好配置

15.SpringBoot項(xiàng)目中service層互相引用

Description:
The dependencies of some of the beans in the application context form a cycle:
 xxxController (field private aaaService xxxController.aaaService)
┌─────┐
| aaaImpl defined in file [aaaImpl.class]
↑ ↓
| bbbImpl (field private aaaService bbbImpl.orderService)
└─────┘

解決方法:

注入方式用的是@RequiredArgsConstructor 注解final方式注入報(bào)錯(cuò)
將注入方式改為@Autowired成功解決

16.SpringBoot配置文件中使用了過時(shí)的配置項(xiàng)

Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException:
The elements [spring.resources.chain.gzipped] were left unbound.

已廢棄的配置項(xiàng)

spring:
 resources:
 chain:
 gzipped: true

解決方法:刪掉過期的配置項(xiàng)即可

到此這篇關(guān)于SpringBoot項(xiàng)目中遇到的BUG問題及解決方法的文章就介紹到這了,更多相關(guān)SpringBoot項(xiàng)目遇到bug內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于maven實(shí)現(xiàn)私服搭建步驟圖解

    基于maven實(shí)現(xiàn)私服搭建步驟圖解

    這篇文章主要介紹了基于maven實(shí)現(xiàn)私服搭建步驟圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Spring中@PropertySource的使用方法和運(yùn)行原理詳解

    Spring中@PropertySource的使用方法和運(yùn)行原理詳解

    這篇文章主要介紹了Spring中@PropertySource的使用方法和運(yùn)行原理詳解,PropertySource注解可以方便和靈活的向Spring的環(huán)境容器(org.springframework.core.env.Environment?Environment)中注入一些屬性,這些屬性可以在Bean中使用,需要的朋友可以參考下
    2023-11-11
  • springcloud檢索中間件?ElasticSearch?分布式場(chǎng)景的使用

    springcloud檢索中間件?ElasticSearch?分布式場(chǎng)景的使用

    單機(jī)的elasticsearch做數(shù)據(jù)存儲(chǔ),必然面臨兩個(gè)問題:海量數(shù)據(jù)存儲(chǔ)問題、單點(diǎn)故障問題,本文重點(diǎn)給大家介紹springcloud檢索中間件?ElasticSearch?分布式場(chǎng)景的運(yùn)用,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • java 中JDBC連接數(shù)據(jù)庫代碼和步驟詳解及實(shí)例代碼

    java 中JDBC連接數(shù)據(jù)庫代碼和步驟詳解及實(shí)例代碼

    這篇文章主要介紹了java 中JDBC連接數(shù)據(jù)庫代碼和步驟詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • 關(guān)于Kill指令停掉Java程序的問題

    關(guān)于Kill指令停掉Java程序的問題

    這篇文章主要介紹了Kill指令停掉Java程序的思考,主要探究kill指令和java的關(guān)閉鉤子的問題,需要的朋友可以參考下
    2021-10-10
  • Java多線程并發(fā)編程和鎖原理解析

    Java多線程并發(fā)編程和鎖原理解析

    這篇文章主要介紹了Java多線程并發(fā)編程和鎖原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Json傳輸出現(xiàn)中文亂碼問題的解決辦法

    Json傳輸出現(xiàn)中文亂碼問題的解決辦法

    最近遇到一個(gè)問題,就是將中文消息以json格式推給微信服務(wù)器時(shí),收到的消息是亂碼,所以下面這篇文章主要給大家介紹了關(guān)于Json傳輸出現(xiàn)中文亂碼問題的解決辦法,需要的朋友可以參考下
    2023-05-05
  • 詳解Mybatis中常用的約束文件

    詳解Mybatis中常用的約束文件

    這篇文章主要介紹了詳解Mybatis中常用的約束文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java中WeakHashMap的回收問題詳解

    Java中WeakHashMap的回收問題詳解

    這篇文章主要介紹了Java中WeakHashMap的回收問題詳解,WeakHashMap弱鍵大致上是通過WeakReference和ReferenceQueue實(shí)現(xiàn),WeakHashMap的key是"弱鍵",即是WeakReference類型的,ReferenceQueue是一個(gè)隊(duì)列,它會(huì)保存被GC回收的"弱鍵",需要的朋友可以參考下
    2023-09-09
  • Spring Boot 中的 @EnableDiscoveryClient 注解的原理

    Spring Boot 中的 @EnableDiscoveryClient 注解

    @EnableDiscoveryClient 注解是 Spring Boot 應(yīng)用程序注冊(cè)到服務(wù)注冊(cè)中心的關(guān)鍵注解,這篇文章主要介紹了Spring Boot 中的 @EnableDiscoveryClient 注解,需要的朋友可以參考下
    2023-07-07

最新評(píng)論