****IDEA和Eclipse整合SSM框架的神坑
今天带领班级同学开始整合SSM框架, 使用Eclipse很快就完成了, 有同学要求我使用Idea来去实现整合, 就带着同学开始搭建。。。
搭建到Spring和Mybatis整合到一起后, 迫不及待的开始使用SqlSessionTemplate测试,结果:
### Exception in thread “main” org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for cn.kgc.smbms.dao.user.UserMapper.selectUserList
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for cn.kgc.smbms.dao.user.UserMapper.selectUserList
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate
S
q
l
S
e
s
s
i
o
n
I
n
t
e
r
c
e
p
t
o
r
.
i
n
v
o
k
e
(
S
q
l
S
e
s
s
i
o
n
T
e
m
p
l
a
t
e
.
j
a
v
a
:
364
)
a
t
c
o
m
.
s
u
n
.
p
r
o
x
y
.
SqlSessionInterceptor.invoke(SqlSessionTemplate.java:364) at com.sun.proxy.
SqlSessionInterceptor.invoke(SqlSessionTemplate.java:364)atcom.sun.proxy.Proxy2.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:194)
at cn.kgc.smbms.dao.user.UserMapperImpl.selectUserList(UserMapperImpl.java:22)
at cn.kgc.smbms.service.impl.UserServiceImpl.getUserList(UserServiceImpl.java:26)
at cn.kgc.smbms.test.Test.main(Test.java:14)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for cn.kgc.smbms.dao.user.UserMapper.selectUserList
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for cn.kgc.smbms.dao.user.UserMapper.selectUserList
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:107)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:354)
… 5 more
然后就各种的排除错, 一时懵逼, 后来仔细阅读异常信息:
Mapped Statements collection does not contain value for cn.kgc.smbms.dao.user.UserMapper.selectUserList
翻译:
“映射语句集合不包含cn.kgc.smbms.dao.user.UserMapper.selectUserList的值”
然后就检查UserMapper接口中写的方法和UserMapper.xml的命名空间一致,select标签的id也一致,所以断定UserMapper文件没有被读到,然后就把UserMapper.xml直接拷贝到resources文件夹中, 然后修改applicationContext-public.xml配置文件中的mapperLocations的路径为:
classpath:*.xml,然后运行后就可了, 如果非要在dao包下放置mapper.xml文件的话,因为在默认情况下去运行测试时,maven不会对dao包下的xml文件进行编译, 所以需要在pom.xml文件中在标签中添加如下的配置:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>