在服务中调用 DAO 时出现 AbstractMethodError 和 null maper

AbstractMethodError and null maper when calling the DAO in a service

我正在使用 Spring 启动、Mybatis(spring 和生成器)构建一个独立的应用程序,我认为我配置一切正常但是自动连接的映射器为空,当我得到一个 AbstractMethodError 时调用它。

实际上我认为这是因为空映射器。我一直在搜索它,并遵循了所有可能的解决方案,但没有任何效果。

关于代码:我也在使用 JavaFX,但这对我的问题没有任何影响。

my.project.core.Main.java

@SpringBootApplication
@MapperScan({"my.project.persistence"})
public class Main extends Application{

    private ConfigurableApplicationContext springContext;

    public static void main(String[] args){
        launch(args);
    }

    public void start(Stage stage) throws Exception {

        springContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);

        //Some code to display the views

        System.out.println("test 1: " + springContext.getBean(UsuarioMapper.class));

        stage.show();
    }
}

(用MyBatis-Generator生成的DAO) my.project.persistence.UsuarioMapper.java

@Mapper //I've also tried @Component
public interface UsuarioMapper {

    int countByExample(UsuarioExample example);

    int deleteByExample(UsuarioExample example);

    int deleteByPrimaryKey(String codUsuario);

    int insert(UsuarioBean record);

    //Etc etc etc
}

(服务)my.project.services.ServicioUsuario.java

@Service
public class ServicioUsuario {

    @Autowired private UsuarioMapper usuarioMapper;

    public boolean userExists(String user){

        System.out.println("test 2: " + usuarioMapper);
        UsuarioBean bean = usuarioMapper.selectByPrimaryKey(user);
        return bean!=null;
    }

}

(配置)my.project.config.SpringConfiguration.java

@Configuration
public class SpringConfiguration {

    /*========== SERVICES ==========*/

    @Bean 
    public ServicioUsuario servicioUsuario() throws Exception{
        return new ServicioUsuario();
    }

    /*========== DAO ==========*/

    //I've already tried passing userMapper().getObject() in ServicioUsuario
    //constructor, but the output is the same, null mapper
    @Bean
    public MapperFactoryBean<UsuarioMapper> userMapper() throws Exception {
        MapperFactoryBean<UsuarioMapper> factoryBean = new MapperFactoryBean<UsuarioMapper>(UsuarioMapper.class);
        factoryBean.setSqlSessionFactory(sqlSessionFactory());
        return factoryBean;

    /*========== CONNECTION ==========*/

    @Bean
    public DataSource dataSource() throws ClassNotFoundException, SQLException{
        OracleDataSource dataSource = new OracleDataSource();

        dataSource.setDriverType("thin");
        dataSource.setServerName("localhost");
        dataSource.setDatabaseName("xe");
        dataSource.setPortNumber(1521);
        dataSource.setUser("testuser");
        dataSource.setPassword("pass123");

        Class.forName("oracle.jdbc.driver.OracleDriver");

        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
        return sqlSessionFactory;
    }

    @Bean
    public DataSourceTransactionManager transactionManager() throws ClassNotFoundException, SQLException {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public SqlSessionTemplate sqlSession() throws Exception {
          return new SqlSessionTemplate(sqlSessionFactory());
        }

}

输出(摘要):

18:30:41.023 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'springConfiguration'
18:30:41.095 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'servicioUsuario'
18:30:41.096 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userMapper'
18:30:41.097 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'sqlSessionFactory'
18:30:41.107 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dataSource'
18:30:41.231 [JavaFX Application Thread] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration
18:30:41.273 [JavaFX Application Thread] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Property 'mapperLocations' was not specified or no matching resources found
18:30:41.481 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'transactionManager'
18:30:41.490 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'sqlSession'
test 1: null
test 2: null
18:32:26.250 [JavaFX Application Thread] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
18:32:26.501 [JavaFX Application Thread] DEBUG java.sql.Connection - ooo Connection Opened
//And now the full stacktrace
Exception in thread "JavaFX Application Thread" java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransactionFactory.newTransaction(Ljava/sql/Connection;Z)Lorg/apache/ibatis/transaction/Transaction;
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:77)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:40)
    at org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:100)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:428)
    at com.sun.proxy.$Proxy14.selectOne(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:66)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35)
    at com.sun.proxy.$Proxy15.selectByPrimaryKey(Unknown Source)
    at my.project.services.ServicioUsuario.userExists(ServicioUsuario.java:42)
    at my.project.gui.LoginController.btAceptar(LoginController.java:70)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)

是否应该实现MyBatis Generator生成的mapper接口?我有没有配置错误的bean?我已经尝试了一切,那个映射器只想保持为空。谢谢。

堆栈跟踪清楚地表明映射器已注入且不为空:

    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35)
    at com.sun.proxy.$Proxy15.selectByPrimaryKey(Unknown Source)
    at my.project.services.ServicioUsuario.userExists(ServicioUsuario.java:42)

这些行表明映射器的调用确实发生了,而不是 null

由于mybatis和spring-mybatis和spring版本不兼容导致的错误。

您需要检查这些库的实际使用版本(maven 和 gradle 的实际操作方式不同)并确保它们都是兼容的。您可以使用 this site 来检查。

例如从这个页面 https://mvnrepository.com/artifact/org.mybatis/mybatis-spring/2.0.3 我可以看到 mybatis-spring 版本 2.0.3 使用 spring 版本 5.1.10.RELEASEmybatis 版本3.5.3。然后将其与构建工具(mvn 或 gradle)解决的实际依赖版本进行比较。

对于 gradle 这是通过 dependencies 任务完成的。

对于 maven 使用 dependencies 插件。