Spring+SpringMVC+Mybatis整合,你可能遇到的问题
一、安利工具
mybatis提供的工具generator,可以去官网下载
二、你可能会遇到的问题
1、将webapp改为3.0的版本
原文件:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
改为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
</web-app>
并修改org.eclipse.wst.common.project.facet.core.xml
<installed facet="jst.web" version="2.3"/> 改为 <installed facet="jst.web" version="3.0"/>
2、spring-core包中不包括spring-context,设置pom.xml的时候需要都引入,否则会找不到context下相应的类
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
刚开始只引入了spring-core包,maven没有自动下载spring-beans,spring-aop,spring-experssion包,引入context后会自动下载其他三个jar包。
3、导入了mybatis-spring包后依然报java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean错误
原因:maven配置中,可以换个版本的mybatis-spring包
4、java.lang.NoClassDefFoundError: org/springframework/dao/support/DaoSupport 错误
原因:缺少spring-tx包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
5、java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy;
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
6、org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.zd.dao.UserMapper.selectByPrimaryKey
一篇怪异的帖子回答:http://www.360doc.com/content/14/0424/09/15113968_371624182.shtml
最后一种回答解决了问题。。。。(我表示很诧异)
7、maven项目报错,但是看不到报错文件
打开窗口Markers,在工具栏处Window->Show View->Markers
可以看到以下错误信息:
Description Resource Path Location Type One or more constraints have not been satisfied. ReBanana line 1 Maven Java EE Configuration Problem
解决办法:在pom.xml配置文件中加入如下语句:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
之后再右键->Maven->Update Project,红叉消失。
8、没有SpringJUnit4ClassRunner类
没有引入spring-test包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
9、RequestMapping cannot be resolved to a type
缺少spring-webmvc包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
10、Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter]
原因:类名写错了,新版本类名为MappingJackson2HttpMessageConverter
11、Cannot resolve com.mysq.jdbc.Connection.ping method. Will use 'SELECT 1' instead. java.lang.NullPointerException
原因:mysql-connector-java版本问题,原来版本为6.0.3改为5.1.39后,没有异常了
12、Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined
原因:在web.xml一定要加上启动spring的监听器,这样配置在xml文件中的bean才会初始化
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
13、CoreException: Could not get the value for parameter compilerId for plugin execution default-compile: PluginResolutionException: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.5.1 or one of its dependencies could not be resolved: Failed to collect dependencies at org.apache.maven.plugins:maven-compiler-plugin:jar:3.5.1
maven-compiler-plugin版本太高,jar包没下载下来,换个低版本的3.0
如下:
14、Servlet /ReBanana threw load() exception
java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException
maven配置文件添加一下配置:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
15、打包文件发给他人时,需在当前目录cmd中执行mvn clean命令,删除maven项目产生的target文件。若maven正确安装完成(可通过maven -version查看版本信息),显示mvn不是内部命令。
在设置环境变量path的时候,可能覆盖了原先设置着的变量,只要在path后面将
;%maven_home%\bin ----> ;%SystemRoot%\system32;%maven_home%\bin
16、在上一问题后,如果再次运行run as--->maven install则会build failure
出现一下类似错误 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'roleService': Unsatisfied dependency expressed through method 'setRoleMapper' parameter 0: Error creating bean with name 'roleMapper' defined in file
先run as--->maven install,下载相应jar包,然后点击工具类的project---->clean----->clean project selected below----->选择自己项目后OK,再次run as--->maven install则不会出现该错误。(前提是你之前测试的时候是完全正确的)
如果遇到其他的问题之后再补充。
若有错误,望纠正。
转载于:https://my.oschina.net/eager/blog/732647