springmvc學習筆記(21)-springmvc整合mybatis遇到的問題及解決小結

來源:互聯網
上載者:User

springmvc學習筆記(21)-springmvc整合mybatis遇到的問題及解決小結
springmvc學習筆記(21)-springmvc整合mybatis遇到的問題及解決小結

本文主要記錄springmvc整合mybatis整合過程中遇到的各種問題和解決方案

遇到的問題在web.xml中標籤報紅
 

解決:改用2.5的版本

答案節選:

Servlet3.0是J2EE6.0規範的一部分,跟隨J2EE6.0一起發布,並且Tomcat7.0已經完全支援Servlet3.0 ;
平時,我們一般使用tomcat6.0,是不能夠使用servelt3.0的,tomcat6.0還不能支援那些規範;
至於說,為毛線不能使用lintener-class,是因為在web-app_3_0.xsd結構定義檔案中,根本就不提倡這些配置,因為Servlet3.0已經支援註解形式;

當時解決了報紅的問題。但後來我其他部分調試好了後,改回3.0也沒報錯。

BeanCreationException
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in file [D:\intellij\workspace\learnssm-firstssm\target\learnssm-firstssm-1.0-SNAPSHOT\WEB-INF\classes\spring\applicationContext-dao.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:478)    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)    ....省略Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)      ....省略

是少依賴的問題,輸入mvn dependency:tree打依賴樹:

D:\intellij\workspace\learnssm-firstssm>mvn dependency:tree[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building learnssm-firstssm 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO][INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ learnssm-firstssm ---[INFO] com.iot.learnssm:learnssm-firstssm:war:1.0-SNAPSHOT[INFO] +- org.springframework:spring-core:jar:4.2.4.RELEASE:compile[INFO] |  \- commons-logging:commons-logging:jar:1.2:compile[INFO] +- org.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile[INFO] |  +- org.springframework:spring-beans:jar:4.2.4.RELEASE:compile[INFO] |  +- org.springframework:spring-context:jar:4.2.4.RELEASE:compile[INFO] |  |  \- org.springframework:spring-aop:jar:4.2.4.RELEASE:compile[INFO] |  |     \- aopalliance:aopalliance:jar:1.0:compile[INFO] |  +- org.springframework:spring-expression:jar:4.2.4.RELEASE:compile[INFO] |  \- org.springframework:spring-web:jar:4.2.4.RELEASE:compile[INFO] +- org.springframework:spring-jdbc:jar:4.2.4.RELEASE:compile[INFO] |  \- org.springframework:spring-tx:jar:4.2.4.RELEASE:compile[INFO] +- mysql:mysql-connector-java:jar:5.1.38:compile[INFO] +- org.mybatis:mybatis:jar:3.3.1:compile[INFO] +- org.mybatis:mybatis-spring:jar:1.2.4:compile[INFO] +- log4j:log4j:jar:1.2.17:compile[INFO] +- org.slf4j:slf4j-api:jar:1.7.18:compile[INFO] +- commons-dbcp:commons-dbcp:jar:1.4:compile[INFO] |  \- commons-pool:commons-pool:jar:1.5.4:compile[INFO] +- javax.servlet:jstl:jar:1.2:compile[INFO] \- taglibs:standard:jar:1.1.2:compile[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 7.956 s[INFO] Finished at: 2016-03-03T20:06:00+08:00[INFO] Final Memory: 11M/126M[INFO] ------------------------------------------------------------------------

少了spring-aspects,spring-core等依賴,加上

    org.springframework    spring-core    ${spring.version}    org.springframework    spring-orm    ${spring.version}    org.springframework    spring-aspects    ${spring.version}     org.springframework    spring-test    ${spring.version}

spring版本統一設定

        4.2.4.RELEASE
mybatis綁定錯誤

錯誤:org.apache.ibatis.binding.BindingException: Invalid bound statement

是的,是沒有在pom.xml配置build包含 xml,導致target目錄下沒有userMapper.xml
-by 唐小明生

我一看自己的target目錄,發現還真是少mapper.xml檔案

我想到了兩種解決方案:

方案一:自訂一個外掛程式,綁定某個生命週期,比如compile,然後外掛程式目標的功能是將源碼包下的xml檔案copy到相應的輸出目錄。(現有外掛程式是否有已有這個功能,通過簡單的配置就能完成?我還不清楚) 方案二:在maven工程的src/main/resource目錄下建和mapper介面類相應的包,將每個mapper.xml存在這裡

這裡我外掛程式玩的不熟,所以沒辦法,只能手動在resources目錄下建包,把每個mapper.xml手動粘貼進去

解決後

請求參數亂碼問題

在web.xml添加post亂碼filter

    CharacterEncodingFilter    org.springframework.web.filter.CharacterEncodingFilter            encoding        utf-8        CharacterEncodingFilter    /*

以上可以解決post請求亂碼問題。解決後調試

對於get請求中文參數出現亂碼解決方案有兩個:

修改tomcat設定檔添加編碼與工程編碼一致,如下:

另外一種方法對參數進行重新編碼:

String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")

ISO8859-1是tomcat預設編碼,需要將tomcat編碼後的內容按utf-8編碼

請求參數類型轉換問題

編寫對應的轉換類才行,具體參考前面參數綁定的博文《 springmvc學習筆記(11)-springmvc註解開發之簡單參數綁定》

maven平台編碼問題

[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!

解決:

在pom.xml檔案的設定編碼即可

      UTF-8    
json格式資料問題1.請求是json格式

debug窗裡報下面的錯誤:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported

瀏覽器報下面的錯誤:

HTTP Status 415 -anddescription The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

2.請求是key/value格式

debug窗裡報下面的錯誤:

java.lang.IllegalArgumentException: No converter found for return value of type: class com.iot.learnssm.firstssm.po.ItemsCustom

參考stackoverflow的這個連結:

How to return JSON data from spring Controller using @ResponseBody

多加一個依賴jackson-databind(之前只加了jackson-mapper-asl的依賴, 間接依賴jackson-core-asl,但還不夠。

    com.fasterxml.jackson.core    jackson-databind    2.7.2
還沒弄懂但不影響啟動並執行問題載入spring容器報紅web.xml節選
     contextConfigLocation    WEB-INF/classes/spring/applicationContext-*.xml     

/classes/spring/applicationContext-*.xml這部分字會報紅,但運行起來沒問題。我使用下面的那句classpath:spring/applicationContext-*.xml不報紅.原因不清楚。

這裡兩種方式都能跑通,但是引用的路徑不同:一個是引用的的輸出的target目錄的classes下的,一個是引用輸出的target/learnssm-firstssm-1.0-SNAPSHOT目錄(相當於部署的WEBROOT或者叫做webapp),所以我覺得還是用WEB-INF下面那個更好

參數綁定配置問題

在自訂參數綁定時,spring.xml的配置如下:

                                                                                       

其中標籤會報紅,但不影響運行。去掉標籤,也可以運行成功。原因我還不知道,以後閱讀源碼會研究下這個問題。

maven依賴分析問題

輸入mvn dependency:analyze進行依賴分析

[INFO][INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ learnssm-firstssm ---[WARNING] Used undeclared dependencies found:[WARNING]    org.springframework:spring-context:jar:4.2.4.RELEASE:compile[WARNING]    org.springframework:spring-web:jar:4.2.4.RELEASE:compile[WARNING]    org.springframework:spring-beans:jar:4.2.4.RELEASE:compile[WARNING] Unused declared dependencies found:[WARNING]    org.springframework:spring-orm:jar:4.2.4.RELEASE:compile[WARNING]    org.springframework:spring-aspects:jar:4.2.4.RELEASE:compile[WARNING]    org.springframework:spring-test:jar:4.2.4.RELEASE:compile[WARNING]    org.springframework:spring-jdbc:jar:4.2.4.RELEASE:compile[WARNING]    mysql:mysql-connector-java:jar:5.1.38:compile[WARNING]    org.mybatis:mybatis-spring:jar:1.2.4:compile[WARNING]    log4j:log4j:jar:1.2.17:compile[WARNING]    org.slf4j:slf4j-api:jar:1.7.18:compile[WARNING]    commons-dbcp:commons-dbcp:jar:1.4:compile[WARNING]    javax.servlet:jstl:jar:1.2:compile[WARNING]    taglibs:standard:jar:1.1.2:compile[WARNING]    org.hibernate:hibernate-validator:jar:5.2.4.Final:compile[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 3.294 s[INFO] Finished at: 2016-03-06T16:35:23+08:00[INFO] Final Memory: 16M/164M[INFO] ------------------------------------------------------------------------

可以看到裡面有:

Used undeclared dependencies found: Unused declared dependencies found:

當時為瞭解決缺包的問題,看到相關的spring-xxx包就加進去了,具體相互之間的依賴關係也沒搞清楚,等我以後閱讀spring源碼再慢慢改好了。反正多引比少引好點,起碼不會報錯,頂多工程冗餘點。

至於說使用了未聲明的包就不知道為啥了,比如

[WARNING]    org.springframework:spring-context:jar:4.2.4.RELEASE:compile[WARNING]    org.springframework:spring-web:jar:4.2.4.RELEASE:compile[WARNING]    org.springframework:spring-beans:jar:4.2.4.RELEASE:compile

都被org.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile依賴,這點可以從依賴樹看到

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ learnssm-firstssm ---[INFO] com.iot.learnssm:learnssm-firstssm:war:1.0-SNAPSHOT[INFO] +- org.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile[INFO] |  +- org.springframework:spring-beans:jar:4.2.4.RELEASE:compile[INFO] |  +- org.springframework:spring-context:jar:4.2.4.RELEASE:compile[INFO] |  |  \- org.springframework:spring-aop:jar:4.2.4.RELEASE:compile[INFO] |  |     \- aopalliance:aopalliance:jar:1.0:compile[INFO] |  +- org.springframework:spring-expression:jar:4.2.4.RELEASE:compile[INFO] |  \- org.springframework:spring-web:jar:4.2.4.RELEASE:compile

總之,上面未解決的問題,我會留意,如果有大神指導原因,請不吝賜教。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.