【问题标题】:The Mybatis @MapperScan doesn't inject the mapper interface into the class, and my mapper is always nullMybatis @MapperScan 没有将mapper接口注入到类中,我的mapper总是为null
【发布时间】:2022-01-14 20:55:45
【问题描述】:

就像标题说的那样,我有这个配置类,我定义了我的 batis 工作所需的所有东西

package my.package.noti.config;

@Configuration
@ComponentScan("my.package")
@MapperScan("my.package.noti.bd")
public class AppConfig {

    private final static Logger logger = LogManager.getLogger(AppConfig.class);

    static {
        System.setProperty("java.awt.headless", "true");
    }

    /**
     * Obtener el datasource
     * @return
     */
    @Bean(destroyMethod = "")
    public DataSource getDataSource() {

        if (logger.isDebugEnabled()) {
            logger.debug("getDataSource");
        }
        InitialContext ic2;
        DataSource ds = null;
        try {
            ic2 = new InitialContext();
            ds = (DataSource) ic2.lookup("java:jboss/datasources/MyDS");
        } catch (NamingException e) {
            logger.error(e.getMessage(), e);
        }

        return ds;
    }

    /**
     * Obtener el transactionManager
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactionManager() {
        if (logger.isDebugEnabled()) {
            logger.debug("transactionManager");
        }
        return new DataSourceTransactionManager(getDataSource());
    }

    /**
     * Obtener el sqlSessionFactory
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("sqlSessionFactory");
        }
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(getDataSource());
        return sessionFactory.getObject();
    }

    /**
     * Obtener el propertySourcesPlaceholderConfigurer
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

这是我的映射器:

@Mapper
public interface NotiMapper {

    @Select(value="{ CALL MYPACKAGUE.MYPROCEDURE("
            + "#{response.notis.noti, javaType=java.sql.ResultSet , jdbcType=CURSOR, mode=OUT, resultMap = notification})}")
    @ResultType(notification.class)
    @Options(statementType = StatementType.CALLABLE)
    @Results(id = "notification",
    value = {
            @Result(property = "noti_n", column = "NOTI_N"),
            @Result(property = "noti_t", column = "NOTI_T"),
            @Result(property = "tpno_f", column = "TPNO_F"),
            @Result(property = "tpno_tp", column = "TPNO_TP"),
    })
    public void ObtainNoti(@Param("response")Response response);
    
}

但是当一个类调用 Mapper 时,它总是为 null

@Service
public class PublisherJboss {
@Autowired
    NotiMapper notiMapper ;
    
    public void process() {
        try {
            //Iniciando JMS
            InitialContext ic = getInitialContext(Config.getProperties().getProperty("providerURLJboss"));
            init(ic, DEFAULT_DESTINATION);
            
            Response respuesta = new Response();
            notiMapper .ObtainNoti(response);
            //Do some other things .... but the notiMapper is always null
}
}

也许你们中的一些人以前遇到过这个问题,我读过一些关于 MapperScannerConfigurer 是一个 BeanDefinitionRegistryPostProcessor 的东西,它在 BeanFactoryPostProcessor 之前触发,但我不明白。

【问题讨论】:

    标签: java spring mybatis


    【解决方案1】:

    在我的情况下,这是因为我正在使用新的 PublisherJboss() 创建类 PublisherJboss,因此 spring 创建了 Mapper bean,但不知道我试图注入映射器 bean 的 PublisherJboss 类的存在到。

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      相关资源
      最近更新 更多