Mybatis @MapperScan 没有将mapper接口注入class,我的mapper一直是null
The Mybatis @MapperScan doesn't inject the mapper interface into the class, and my mapper is always null
就像标题所说的那样,我有这个配置 class 我定义了我的 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);
}
但是当 class 调用 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 之前触发,但我不明白。
在我的例子中,这是因为我用新的 PublisherJboss() 创建了 class PublisherJboss,所以这样 spring 创建了 Mapper bean 但不知道 PublisherJboss 的存在class 我正在尝试将映射器 bean 注入到其中。
就像标题所说的那样,我有这个配置 class 我定义了我的 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);
}
但是当 class 调用 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 之前触发,但我不明白。
在我的例子中,这是因为我用新的 PublisherJboss() 创建了 class PublisherJboss,所以这样 spring 创建了 Mapper bean 但不知道 PublisherJboss 的存在class 我正在尝试将映射器 bean 注入到其中。