此文参考书籍为ACCP软件工程师——使用SSM框架开发企业级应用 8.0版
文章目录
- 遇到的问题:
-
- 1.错误:could not find resource src/dao/UserMapper.xml 和 mybatis启动异常:Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl'
- 2.Eclipse中的路径问题:
- 3.log4j警告之log4j:WARN No appenders could be found for logger(org.apache.ibatis.logging.LogFactory).
- 4.java.lang.NoClassDefFoundError: Could not initialize class utils.MyBatisUtil
- 5.在.properties文件中添加中文注释乱码问题
- 6.mybatis报错:The alias 'xxx' is already mapped to the value '*.*.xxx'
- 7.什么时候用resultType什么时候用resultMap
- 8.Expected one result (or null) to be returned by selectOne(), but found: 2
- 9.mysql下出现Unknown column 'id' in 'on clause'的解决方法
- 10.java.lang.NullPointerException
- 11.设置Spring的AOP配置文件applicationContext.xml报错:error at ::0 formal unbound in pointcut
- 12.设置Spring的AOP配置文件applicationContext.xml报错:nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting ')' at character position 16
- 13.Eclipse下把jar包放到工程lib下和通过buildpath加载有什么不同
- 14.Referenced file contains errors (http://www.springframework.org/schema/aop/spring-aop-3.2.xsd). For more information, right click on the message in the Problems View and select "Show Details..."
- 15.SpringMVC: No mapping found for HTTP request with URI
- 16.复制工程后出现的问题
- 17.Error creating bean with name 'userController':
- 18.Could not load the Tomcat server configuration at \Servers\Tomcat v8.5 Server at localhost-config. The Servers project is closed.
- 19.No mapping found for HTTP request with URI)
- 20. 配置jsp页面link到css时报错:javax.servlet.ServletException cannot be resolved to a type 和javax.servlet.jsp.JspException cannot be resolved to a type
- 21.No qualifying bean of type [service.UserService] found for dependency和Error creating bean with name 'userController':同时出现
- 22.Server Tomcat v8.5 Server at localhost was unable to start within 45 seconds
- 23.Web项目的root
- 24.Maven构建SpringMVC环境报错“java.lang.ClassNotFoundException: org.springframework.web.servlet. DispatcherSe
- 25.浏览器中EL表达式不起作用
- 一、导入jar文件(Mybatis)
- 二、配置文件(Mybatis——混合Dao层部分文件)
-
- 1.mybatis-config.xml(MyBatis配置文件)
- 2.database.properties(数据库登录信息)
- 3.log4j.xml(日志配置文件)
- 4.创建持久化类(POJO)
- 5.mapper.xml(创建SQL映射文件)
- 6.MyBatisUtil.java(用来集中创建SqlSessionFactory和sqlSession)
- 7. 创建测试类
- 8.创建PojoMapper.java文件(绑定映射语句的接口文件)
- 9.如何使用reslutMap做返回类型
- 10.配置PojoMapper.xml和测试类TestUserMapper.java实现增加功能
- 11.MyBatis实现分页
- 三、导入jar文件(Spring)
- 四、配置文件(Spring——混合Service层部分文件)
- 五、MyBatis与Spring的整合
- 六、加入SpringMVC
遇到的问题:
1.错误:could not find resource src/dao/UserMapper.xml 和 mybatis启动异常:Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImpl’
这里是mybatis-config.xml、UserMapper.xml中的mapper.xml的路径有问题,而且这几个路径要相同(有/和.)。寻求解决的时候提示用IDEA的时候可能需要另外配置maven中的pom.xml文件中build的标签
2.Eclipse中的路径问题:
写路径的时候src不需要被写上,如果你在src目录下的文件中要写src外文件的路径,需要写上同级目录名(例如resources目录中的文件)
3.log4j警告之log4j:WARN No appenders could be found for logger(org.apache.ibatis.logging.LogFactory).
这里是没有写log4j.properties文件,使日志没有依赖配置文件
参考链接:参考链接1
参考链接2
4.java.lang.NoClassDefFoundError: Could not initialize class utils.MyBatisUtil
(1)是因为mybatis-config.xml中的配置文件不对,这里是name属性错误
<typeAliases>
<package name="Pojo"/>
</typeAliases>
(2)再一次遇到了同样的报错,这次是在添加ArticleMapper.xml等article部分的东西,还有Pojo中怎天了article和Collection类后出现,连带User部分也出错。
暂时检查了resource的路径和以前一样没有问题,网上说应该还是xml文件的问题
参考链接:mybatis报错:The alias ‘xxx’ is already mapped to the value ‘..xxx’
第一次报错信息
这里坑了好久,以为和(1)一样还是xml的问题,检查完没有问题,然后删除了除了User外的东西重新测试也是不行。删除了报错的位置代码,出现第二次报错信息:
java.lang.ExceptionInInitializerError
at test.TestUserMapper.test(TestUserMapper.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in SQL Mapper Configuration
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.type.TypeException: The alias 'Collection' is already mapped to the value 'java.util.Collection'.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:79)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:63)
at utils.MyBatisUtil.<clinit>(MyBatisUtil.java:16)
... 24 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.type.TypeException: The alias 'Collection' is already mapped to the value 'java.util.Collection'.
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:106)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:89)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:77)
... 26 more
Caused by: org.apache.ibatis.type.TypeException: The alias 'Collection' is already mapped to the value 'java.util.Collection'.
at org.apache.ibatis.type.TypeAliasRegistry.registerAlias(TypeAliasRegistry.java:148)
at org.apache.ibatis.type.TypeAliasRegistry.registerAlias(TypeAliasRegistry.java:141)
at org.apache.ibatis.type.TypeAliasRegistry.registerAliases(TypeAliasRegistry.java:130)
at org.apache.ibatis.type.TypeAliasRegistry.registerAliases(TypeAliasRegistry.java:119)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:115)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:96)
... 28 more
注意Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.type.TypeException: The alias ‘Collection’ is already mapped to the value ‘java.util.Collection’. 这一句,竟然是Collection这个类名,在MyBatis中有重复名字,改了Collection名字,就好了!!!!!!!!!!!!!
5.在.properties文件中添加中文注释乱码问题
右键文件 -> properties -> Resource -> Text file coding 选择UTF-8
注释是用#
eclipse上的.properties文件中文编辑显示处理
6.mybatis报错:The alias ‘xxx’ is already mapped to the value ‘..xxx’
7.什么时候用resultType什么时候用resultMap
如果返回一个值或一个对象(int,String,User )就用resultType,如果要返回一个复杂对象,就可以用resultMap,也需要定义好。
要知道什么时候用哪个就需要知道他们各自的限制在哪里,resultType返回一个User的时候,必须完整返回,不能返回其中一部分元素。
但是用resultMap就可以自己指定,甚至在Pojo的User类中多定义一些信息,在resultMap映射过程中将相关的信息,同时映射进来。
特别适合多表联合查询的时候,返回的是一个多个类中的信息构成的新的类,这时候用resultMaple就好很多(相对于新建一个类型来说,这时候的新建部分就少,或者可以用以前的类进行扩充然后被包括进来)
8.Expected one result (or null) to be returned by selectOne(), but found: 2
是因为SQL语句执行完之后,找到了2个结果,但是你只用int或者单个的元素进行接收
9.mysql下出现Unknown column ‘id’ in 'on clause’的解决方法
MySQL5.0 Bug, 要把联合的表用括号包含起来才行:
参考链接:mysql下出现Unknown column ‘id’ in 'on clause’的解决方法
10.java.lang.NullPointerException
参考链接:java.lang.NullPointerException - 如何处理空指针异常
原因:
调用null对象的实例方法。
访问或修改null对象的字段。
把长度null当作一个数组。
像访问或修改null阵列一样访问或修改插槽。
投掷null就好像它是一个Throwable 价值。
应用程序应该抛出此类的实例来指示null对象的其他非法使用。
NullPointerException对象可以由虚拟机构造,就像抑制被禁用和/或堆栈跟踪不可写一样。
11.设置Spring的AOP配置文件applicationContext.xml报错:error at ::0 formal unbound in pointcut
报错信息第一行如下
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [applicationContext.xml]: BeanPostProcessor before instantiation of bean failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#1': Instantiation of bean failed;
nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception;
nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
是因为没有匹配,暂时总结原因有:
(1)设置的切入点的表达式不对(参数个数,类别等)
<aop:pointcut id="pointcut" expression="execution(public int addUser(Pojo.User))"/>
(2)设置的返回方式不对,这里的后置增强方法(afterReturning())是有返回值的,所以需要用
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
12.设置Spring的AOP配置文件applicationContext.xml报错:nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting ‘)’ at character position 16
报错信息第一行和第二行:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [applicationContext.xml]: BeanPostProcessor before instantiation of bean failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Instantiation of bean failed;
nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception;
nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting ')' at character position 16
excution(public int addUser(Pojo.User))
迷惑性很大,最后找到出错位置代码:
<aop:pointcut id="pointcut" expression="excution(public int addUser(Pojo.User))"/>
这里的excution应该改为execution,因为这里是“”中的东西,编译器不会进行检查,所以到这里一定要额外注意
13.Eclipse下把jar包放到工程lib下和通过buildpath加载有什么不同
Eclipse下把jar包放到工程lib下和通过buildpath加载有什么不同(解决找不到类的中级方法)
14.Referenced file contains errors (http://www.springframework.org/schema/aop/spring-aop-3.2.xsd). For more information, right click on the message in the Problems View and select “Show Details…”
出现在有时候对applicationContext.xml文件进行修改的时候会出现这个错误
参考链接
15.SpringMVC: No mapping found for HTTP request with URI
参考链接:SpringMVC: No mapping found for HTTP request with URI
我这里是.xml文件有两个位置写错了
(1)首先是web.xml中配置DispatcherServlet中对URL的截获设置
<!-- 配置DispatcherServlet -->
<servlet-mapping>
<servlet-name>springMvc</servlet-name> <!-- 将名为springMvc的DispatcherServlet映射到/ -->
<!-- 修改前 <url-pattern></url-pattern> -->
<url-pattern>/</url-pattern> <!-- 这里暂时截获并处理所有的URL请求 -->
</servlet-mapping>
(2)还有是springmvc-servlet.xml文件中的视图解析器中路径配置错误,一开始的jsp在WebContent中,配置路径写在了/WEB-INF/sjp/下,所以这里将jsp文件移动到了正确路径下
<!-- 配置视图解析器 作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 ,中间填的是返回的VIEW名-->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
16.复制工程后出现的问题
第一,运行后仍需要用之前原来项目的url才能访问。
第二,向服务器添加时工程名字后面括号中仍然是原工程名。
参考链接:Eclipse中将项目导入Tomcat中,并且修改了项目名字,但加载后的项目后面有一个括号显示原来项目的名字
①打开你的项目目录,找到一个.project文件,打开后修改 test中的值,此处为原来项目名字,修改为新的项目名字
②在项目目录下,打开.settings文件夹,找到org.eclipse.wst.common.component文件,修改,检查中的value值
③在eclipse中,右键点击项目(或快捷键Alt+Enter),找到最下方的Properties,点击进去找到Web Project Settings,修改Context root的值为你要修改的名字。
④刷新你的项目,将项目从服务器中一处,重启eclipse
17.Error creating bean with name ‘userController’:
完整bug:
Error creating bean with name ‘userController’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
18.Could not load the Tomcat server configuration at \Servers\Tomcat v8.5 Server at localhost-config. The Servers project is closed.
现在使用的是eclipse,在添加tomcat之后会生成一个名为Servers的工程,如果将工程关闭,就会报这个错误。
19.No mapping found for HTTP request with URI)
在服务器上运行后,出现无法连接到css文件的网页没有格式的情况,根据报错信息,暂时采用了下面链接中的最后一种方法来解决。(在springmvc-servlet.xml文件中添加)
完美解决SpringMVC中静态资源无法找到(No mapping found for HTTP request with URI)问题
20. 配置jsp页面link到css时报错:javax.servlet.ServletException cannot be resolved to a type 和javax.servlet.jsp.JspException cannot be resolved to a type
在页面中使用全局路径${pageContext.request.contextPath}出现javax.servlet.ServletException cannot be resolved to a type错误,解决方法如下:
主要是因为缺少jsp-api.jar 和 servlet-api.jar这两个Jar包。
javax.servlet.ServletException cannot be resolved to a type错误解决方法
21.No qualifying bean of type [service.UserService] found for dependency和Error creating bean with name ‘userController’:同时出现
这里消耗了两天,第一天结束后锁定在配置文件的问题,排查了Context和spring-servlet.xml文件之后,想到了web.xml文件,最终在第二天解决问题。
web.xml文件中,配置文件clsspath的第一个字母写错了,导致没有检测到springMVC的配置文件,自然就检测不到@Service等对Bean的扫描了。
<!-- 配置环境 指定spring配置文件所在位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
22.Server Tomcat v8.5 Server at localhost was unable to start within 45 seconds
参考链接
这里的原因是项目启动慢,所以来设置自动关闭时间更长一些,在Service视图中双击你要用的服务器,然后调整
23.Web项目的root
右击项目->properties->Web Project Settings
24.Maven构建SpringMVC环境报错“java.lang.ClassNotFoundException: org.springframework.web.servlet. DispatcherSe
使用maven导入之后,web项目可能会出现这个问题。
参考链接:Maven构建SpringMVC环境报错“java.lang.ClassNotFoundException: org.springframework.web.servlet. DispatcherSe
25.浏览器中EL表达式不起作用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
一、导入jar文件(Mybatis)
1.下载
2.导入
(1)导入下列包,复制到WEB-INF目录下的lib目录中
①mybatis-3.2.2 .jar:
②MySQL-connector-java-5.1.0-bin.jar:
③log4j-1.2.17:
(2)Build Path -> Configure Build Path -> Add JARS导入上述包(右键工程)
(3)DTD文件的引入
配置mybatis-config.xml和mapper.xml文件不能自动联想,需要引入mybatis-3-config.dtd和mybatis-3-mapper.dtd文件,这两个文件在mybatis-3.3.2.jar中,路径为mybatis-3.2.2\org\apache\ibatis\builder\xml中
Windows -> Preferences ->User Specified Enter ->XML Catalog -> Add… ->在location中选择dtd所在目录 key属性相对应填写-//mybatis.org//DTD Config 3.0//EN和-//mybatis.org//DTD Mapper 3.0//EN
之后注意要OK和apply
二、配置文件(Mybatis——混合Dao层部分文件)
1.mybatis-config.xml(MyBatis配置文件)
书:P12、P27
在项目下新建Source Folder类型的resourses目录中创建。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- configuration 中配置文件的根元素节点 -->
<configuration>
<!-- 引入database.properties文件 -->
<!-- 此属性文件描述数据库的连接的相关配置(数据库驱动、连接数据库的url、数据库用户名、数据库密码),位置也在/resources目录下 -->
<properties resource ="database.properties"/>
<!-- 配置mybatis的log实现为LOG4J -->
<!-- 这里设置MyBatis运行中的一些行为,比如此处设置其log日志实现为LOG4J -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 配置mybatis多套运行环境 -->
<!-- 将SQL映射到多个不同的数据库上,该元素结点可以配置多个environment元素结点,但是必须指定其中一个为默认运行环境(用default指定) -->
<environments default="development">
<!-- 配置MyBatis的一套运行环境,需指定运行ID、事务管理、数据源配置等相关信息 -->
<environment id="development">
<!-- 配置事务管理,采用JDBC的事务管理 -->
<transactionManager type="JDBC"/>
<!-- POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 将mapper文件加入到配置文件中 -->
<!-- 告诉MyBatis去哪里找到SQL映射文件(该文件内容是开发者定义的映射SQL语句),可以有1个或多个SQL映射文件 -->
<mappers>
<!-- 这里是数据库配置文件的路径,要回来修改-->
<!-- 这里具体指定SQL映射文件的路径,其中resource属性的值表述了SQL映射文件的路径 -->
<!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/> -->
<mapper resource="dao/UserMapper.xml"/