MyBatis-Spring + @Configuration - Can't autowire mapper beans

后端 未结 4 1859
感情败类
感情败类 2020-12-24 09:15

I have been trying to create a Spring project that uses MyBatis for the data access layer as a proof of concept for my team. I really want to avoid XML configuration if at a

4条回答
  •  佛祖请我去吃肉
    2020-12-24 09:52

    From mybatis.3.2.0 and mybatis-spring.1.2.0, instead of MapperFactoryBean, you can use MapperScan for this.

    @Configuration 
    @MapperScan("org.mybatis.spring.sample.mapper") 
    public class AppConfig 
    {   
        @Bean   
        public DataSource dataSource() 
        {     
          return new EmbeddedDatabaseBuilder().addScript("schema.sql").build();   
        }   
       @Bean   
       public DataSourceTransactionManager transactionManager() 
       {     
            return new DataSourceTransactionManager(dataSource());   
       }   
       @Bean   
       public SqlSessionFactory sqlSessionFactory() throws Exception 
       {     
           SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
           sessionFactory.setDataSource(dataSource());    
           return sessionFactory.getObject();   
       } 
    }
    

提交回复
热议问题