简体   繁体   English

mybatis @MapperScan不起作用

[英]mybatis @MapperScan not working

I'm trying to set up the Java config for MyBatis & the @MapperScan does not appear to be accomplishing anything. 我正在尝试为MyBatis设置Java配置,并且@MapperScan似乎没有完成任何事情。 Note, I can get the application to work with XML config. 注意,我可以使该应用程序与XML配置一起使用。

What am I missing? 我想念什么? The com.test.mapper package definitely exists & has a file/iterface called TestMapper. com.test.mapper软件包确实存在并且具有名为TestMapper的文件/接口。 The corresponding xml is in the correct location in the resources folder. 相应的xml在resources文件夹中的正确位置。

*************************** APPLICATION FAILED TO START ***************************申请无法开始


Description: 描述:

Field templateMapper in com.test.TestController required a bean of type 'com.test.mapper.TestMapper' that could not be found. com.test.TestController中的字段templateMapper需要一个类型为com.test.mapper.TestMapper的bean,但找不到。

Action: 行动:

Consider defining a bean of type 'com.test.mapper.TestMapper' in your configuration. 考虑在配置中定义类型为“ com.test.mapper.TestMapper”的bean。

Autowired that is failing 自动连线失败

@Autowired
TestMapper _testMapper;

config 配置

@Configuration
@MapperScan("com.test.mapper")
public class AppConfig {

   @Bean
   public DataSource dataSource() {

     SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
     try {
       dataSource.setDriverClass(com.microsoft.sqlserver.jdbc.SQLServerDriver.class);
       //dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
       dataSource.setUrl("jdbc:sqlserver://server;databaseName=db1;integratedSecurity=true;");

     } catch (Exception e) {

     }
     return dataSource;
   }

   @Bean
   public DataSourceTransactionManager transactionManager() {
     return new DataSourceTransactionManager(dataSource());
   }

   @Bean
   public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
     SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

     sqlSessionFactoryBean.setTypeAliasesPackage("com.test.domain");

     sqlSessionFactoryBean.setDataSource(dataSource());
     return sqlSessionFactoryBean;
   }
}

I solved this. 我解决了 My issue wasn't with Mybatis. 我的问题与Mybatis无关。 It was with Spring. 是在春天。 This link to the Spring docs says to "...locate your main application class in a root package above other classes". 指向 Spring文档的此链接表示“ ...将主应用程序类放在其他类之上的根包中”。

I had not done that. 我没有那样做。 Once I moved application class ( annotated with SpringBootApplication) then the @MapperScan annotation worked. 一旦我移动了应用程序类(用SpringBootApplication注释),然后@MapperScan注释就起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM