我在这里有两个配置:
let date = this.Model.year + '-' + this.Model.month + '-' + this.Model.day
----------另一个配置-------------
@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
init早于MapperScannerConfig
,我从打印日志中获取,DataConfig
无法获取值(@Value("${datasource.basePackage:com.tg.ms.mapper}") private String basePackage;
可以获得),我使用{ {1}}没用,DataConfig
也是预告片,我无法配置mapper basePackage
日志:@AutoConfigureAfter
答案 0 :(得分:0)
我遇到了同样的问题。 MapperScannerConfigurer在spring框架中过早地初始化,我认为它会导致注释@AutoConfigureAfter变得无用。 所以我解决它:避免使用MapperScannerConfigurer: 两种方式:
只需使用@MapperScan(“com.a.b.package”)
在mybatis映射器界面中使用注释@ org.apache.ibatis.annotations.Mapper。