Springmvc Study Notes (21)-springmvc integration of mybatis Problems and Solutions
Springmvc Study Notes (21)-springmvc integration of mybatis Problems and Solutions
This article mainly records various problems and solutions encountered during springmvc integration of mybatis.
Problems encountered in web. xml
Label red
Solution: Use version 2.5.
Answer excerpt:
Servlet3.0 is part of the J2EE6.0 specification, which is released along with J2EE6.0 and Tomcat7.0 fully supports Servlet3.0;
Usually, tomcat6.0 is used, but servelt3.0 cannot be used. tomcat6.0 does not support those specifications;
As for the wool cannot use lintener-class, because in the web-app_3_0.xsd structure definition file, do not advocate these configurations, because Servlet3.0 has supported the annotation form;
At that time, the problem of red was solved. But after I debug other parts, no error is reported if I change back to 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 ).... omitted 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 ).... omitted
Is less dependent, inputmvn dependency:tree
Create a 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] ------------------------------------------------------------------------
With less dependencies such as spring-aspects and 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}
Unified spring version settings
4.2.4.RELEASE
An error occurred while binding mybatis.
Error:org.apache.ibatis.binding.BindingException: Invalid bound statement
Yes, the build is not configured in pom. xml to contain xml, so there is no userMapper. xml in the target directory.
-By Tang xiaomingsheng
I looked at my target directory and found that the mapper. xml file is really missing.
I came up with two solutions:
Solution 1: Customize a plug-in and bind a lifecycle, such as compile. Then, the plug-in's target function is to copy the xml file under the source code package to the corresponding output directory. (Does the existing plug-in have this function, which can be completed through simple configuration? I still don't know) solution 2:
src/main/resource
Directory and corresponding package of the mapper interface class.
Here I am not familiar with plug-ins, so there is no way, you can only manually create a package under the resources Directory, paste each mapper. xml manually
After solution
Request Parameter garbled
Add post garbled filter in web. xml
CharacterEncodingFilter
Org. springframework. web. filter. CharacterEncodingFilter
Encoding
UTF-8
CharacterEncodingFilter
/*
The above can solve the post request garbled problem. Debug after solution
There are two solutions to garbled Chinese parameters in the get request:
Modify the tomcat configuration file to add the same encoding as the project code, as shown below:
Another method is to re-encode the parameters:
String userName = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")
The ISO8859-1 is the default tomcat encoding, And the content after tomcat encoding needs to be encoded according to UTF-8
Type conversion of Request Parameters
Compile the corresponding conversion class. For details, refer to the previous parameter binding blog springmvc Study Notes (11)-simple parameter binding for springmvc annotation development.
Maven platform Encoding Problems
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
Solution:
Set the encoding in the pom. xml file.
UTF-8
Json format data problem 1. The request is in json format
The following error is reported in the debug window:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
The browser reports the following error:
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. The request is in key/value format.
The following error is reported in the debug window:
java.lang.IllegalArgumentException: No converter found for return value of type: class com.iot.learnssm.firstssm.po.ItemsCustom
Refer to the link of stackoverflow:
How to return JSON data from spring Controller using @ ResponseBody
Add one more dependencyjackson-databind
(Previously, onlyjackson-mapper-asl
Dependency, indirect dependencyjackson-core-asl
But not enough.
com.fasterxml.jackson.core
jackson-databind
2.7.2
Loading spring containers with red web. xml excerpt
ContextConfigLocation
WEB-INF/classes/spring/applicationContext-*. xml
/classes/spring/applicationContext-*.xml
This part of the text will be red, but it is okay to run. I use the following sentence classpath:spring/applicationContext-*.xml
The reason is unclear.
Both methods can run normally, but the referenced paths are different: one is under the classes of the target directory of the referenced output, and the other is to reference the outputtarget/learnssm-firstssm-1.0-SNAPSHOT
Directory (equivalent to the deployment of WEBROOT or webapp), so I think it is better to use the one below the WEB-INF
Parameter binding configuration problems
When binding custom parameters, configure spring. xml as follows:
Where
The label is red, but the operation is not affected. Remove
Tag. I still don't know the reason. I will study this issue later when I read the source code.
Maven dependency analysis problems
Inputmvn dependency:analyze
Perform dependency analysis
[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] ------------------------------------------------------------------------
We can see that:
Used undeclared dependencies found:
Unused declared dependencies found:
At that time, in order to solve the problem of missing packages, the relevant spring-xxx package was added, and the dependencies between them were not clear, wait for me to read the spring source code and then change it. At least no error is reported. At most, there are engineering redundancy points.
I don't know why when I use an undeclared package, for example
[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
Areorg.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile
Dependency, which can be seen from the dependency tree
[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
In short, I will pay attention to the unsolved issues above. If you have any great guidance, please kindly advise me.