如何在springboot中配置mybatis

我这里有两个配置

@Configuration
public class DataConfig {

@Value("${datasource.jdbcUrl}")
private String jdbcUrl;
@Value("${datasource.username}")
private String username;
@Value("${datasource.password}")
private String password;
@Value("${datasource.driverClassName:com.mysql.jdbc.Driver}")
private String driverClassName;
@Value("${datasource.initialSize:20}")
private int initialSize;
@Value("${datasource.maxActive:30}")
private int maxActive;
@Value("${datasource.minIdle:20}")
private int minIdle;
@Value("${datasource.transactionTimeoutS:30}")
private int transactionTimeoutS;

@Value("${datasource.basePackage:com.tg.ms.mapper}")
private String basePackage;
@Value("${datasource.mapperLocations}")
private String mapperLocations;


@Bean
public DataSource dataSource() {
    DruidDataSource ds = new DruidDataSource();
    ds.setMaxWait(maxWait);
    ds.setValidationQuery(validationQuery);
    ds.setRemoveAbandoned(removeAbandoned);
    ds.setRemoveAbandonedTimeout(removeAbandonedTimeout);
    ds.setTestWhileIdle(testWhileIdle);
    ds.setTestOnReturn(testOnReturn);
    ds.setTestOnBorrow(testOnBorrow);
    ds.setMinIdle(minIdle);
    return ds;
}


@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource());

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

    sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));

    return sqlSessionFactoryBean.getObject();
}

----------另一个配置-------------

@Configuration
@AutoConfigureAfter(DataBaseConfig.class)
public class MapperScannerConfig {

@Value("${datasource.basePackage:com.tg.ms.mapper}")
private String basePackage;


@Bean
public MapperScannerConfigurer BPMapperScannerConfigurer() {
    System.out.println("mapper--1.----******----"+basePackage+"----*******");
    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    mapperScannerConfigurer.setBasePackage("com.tg.mapper");
    mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
    return mapperScannerConfigurer;
}
}
  • 我可以将 @Bean public MapperScannerConfigurer BPMapperScannerConfigurer() 放入 DataConfig 吗?我试着打印

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testMapper' defined in file [/Users/twogoods/codesource/mainetset/target/classes/com/tg/mapper/TestMapper.class]: Cannot resolve reference to bean 'sqlSessionFactoryBean' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactoryBean' defined in class path resource [com/tg/config/DataConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactoryBean' threw exception; nested exception is java.lang.NullPointerException

  • MapperScannerConfig init早于 DataConfig ,我从打印日志中得到它, @Value("${datasource.basePackage:com.tg.ms.mapper}") private String basePackage; 无法获取值(在 DataConfig 中可以得到),我使用 @AutoConfigureAfter 是没用的, MapperScannerConfig 也是预告片,我无法配置mapper basePackage

日志: Cannot enhance @Configuration bean definition 'BPMapperScannerConfigurer' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.

回答(1)

3 years ago

我遇到了同样的问题 . MapperScannerConfigurer在spring框架中过早地初始化,我认为它会导致注释@AutoConfigureAfter变得无用 . 所以我解决它:避免使用MapperScannerConfigurer:两种方式:

  • 只需使用@MapperScan(“com.a.b.package”)

  • 在mybatis映射器界面中使用注释@ org.apache.ibatis.annotations.Mapper .