春季4&x2B;使用Java8的MyBatis集成问题
在使用Java8(1.8.060)、Spring4.2.1和MyBatis3.3.0时,我面临以下异常春季4&x2B;使用Java8的MyBatis集成问题,java,spring,java-8,mybatis,spring-mybatis,Java,Spring,Java 8,Mybatis,Spring Mybatis,在使用Java8(1.8.060)、Spring4.2.1和MyBatis3.3.0时,我面临以下异常 Sep 29, 2015 11:02:58 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationC
Sep 29, 2015 11:02:58 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@246b179d: startup date [Tue Sep 29 11:02:58 EDT 2015]; root of context hierarchy
Sep 29, 2015 11:02:58 AM org.mybatis.spring.mapper.ClassPathMapperScanner checkCandidate
WARNING: Skipping MapperFactoryBean with name 'userMapper' and 'com.poc.test.spring4app.mapper.UserMapper' mapperInterface. Bean already defined with the same name!
Sep 29, 2015 11:02:58 AM org.mybatis.spring.mapper.ClassPathMapperScanner doScan
WARNING: No MyBatis mapper was found in '[com.poc.test.spring4app.mapper]' package. Please check your configuration.
Creating tables
Sep 29, 2015 11:02:59 AM org.springframework.context.annotation.AnnotationConfigApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in com.poc.test.spring4app.config.AppConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.poc.test.spring4app.mapper.UserMapper]: Factory method 'userMapper' threw exception; nested exception is org.apache.ibatis.binding.BindingException: Type interface com.poc.test.spring4app.mapper.UserMapper is not known to the MapperRegistry.
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.poc.test.spring4app.config.AppInitializer.main(AppInitializer.java:16)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.poc.test.spring4app.mapper.UserMapper]: Factory method 'userMapper' threw exception; nested exception is org.apache.ibatis.binding.BindingException: Type interface com.poc.test.spring4app.mapper.UserMapper is not known to the MapperRegistry.
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 13 more
Caused by: org.apache.ibatis.binding.BindingException: Type interface com.poc.test.spring4app.mapper.UserMapper is not known to the MapperRegistry.
at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:675)
at org.mybatis.spring.SqlSessionTemplate.getMapper(SqlSessionTemplate.java:293)
at com.poc.test.spring4app.config.AppConfiguration.userMapper(AppConfiguration.java:51)
at com.poc.test.spring4app.config.AppConfiguration$$EnhancerBySpringCGLIB$$8dd4b125.CGLIB$userMapper$0(<generated>)
at com.poc.test.spring4app.config.AppConfiguration$$EnhancerBySpringCGLIB$$8dd4b125$$FastClassBySpringCGLIB$$677c8295.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318)
at com.poc.test.spring4app.config.AppConfiguration$$EnhancerBySpringCGLIB$$8dd4b125.userMapper(<generated>)
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.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 14 more
用户映射器
package com.poc.test.spring4app.config;
import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import com.poc.test.spring4app.domain.User;
import com.poc.test.spring4app.mapper.UserMapper;
public class AppInitializer {
public static void main(String[] args) {
AbstractApplicationContext context = null;
try {
context = new AnnotationConfigApplicationContext(
AppConfiguration.class);
context.registerShutdownHook();
UserMapper userMapper = context.getBean(UserMapper.class);
List<User> users = userMapper.getAllUsers();
System.out.println(users.toString());
context.close();
}
catch (final Exception e) {
if (context != null) {
context.close();
}
}
}
}
package com.poc.test.spring4app.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.poc.test.spring4app.domain.User;
public interface UserMapper {
@Select("select id, firstName, lastName, email from users")
@Results(value={
@Result(column="id", property="id"),
@Result(column="firstName", property="firstName"),
@Result(column="lastName", property="lastName"),
@Result(column="email", property="email")
})
List<User> getAllUsers();
}
package com.poc.test.spring4app.domain;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private long id;
private String firstName;
private String lastName;
private String email;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
// getters and setters
}
我相信这可能是你的问题:
WARNING: Skipping MapperFactoryBean with name 'userMapper' and 'com.poc.test.spring4app.mapper.UserMapper' mapperInterface. Bean already defined with the same name!
试着消除这种两面性
问题的解决方案:
使用Spring4和MyBatis3,如果您已经在@MapperScan上扫描MyBatisMapper实现,则不应该显式定义映射器。bean的实例化是自动处理的。我们使用Java 8、Spring 4、Spring引导以及最新的MyBatis和MyBatis Spring。此堆栈工作正常。显示代码。@luboskrnac我用代码更新了问题。提前谢谢。谢谢你把它指向卢博斯克纳克!这个POC有帮助;我的实际应用程序中的Spring警告被抑制;很可能错过了。但是你知道为什么它在Spring3.x中工作而不是在Spring4中吗?对不起,没有线索,没有机会在Spring3中使用MyBatis。在我的春季三天里使用了不同的库。
package com.poc.test.spring4app.domain;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private long id;
private String firstName;
private String lastName;
private String email;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
// getters and setters
}
WARNING: Skipping MapperFactoryBean with name 'userMapper' and 'com.poc.test.spring4app.mapper.UserMapper' mapperInterface. Bean already defined with the same name!