作为一个Spring的初学者,检查过文件的配置,全部都是正确的,但是不知道为什么我已启动就是Error creating bean with name 'xxx' defined in javaconfig问题。直接上代码,求大神指点。
这是我的项目结构:
这是我引入的依赖:
4.0.0 com.hncgzy.wutao day02_spring_041.0-SNAPSHOT UTF-8 1.8 1.8 5.0.2.RELEASE 1.6.6 1.2.12 8.0.13 3.5.3 4.12 1.18.8 1.3.0 org.springframework spring-context${spring.version} junit junit${junit.version} test org.projectlombok lombok${lombok.version} mysql mysql-connector-java${mysql.version} org.mybatis mybatis${mybatis.version} org.mybatis mybatis-spring${mybatis.spring.version} log4j log4j${log4j.version} org.slf4j slf4j-api${slf4j.version} org.slf4j slf4j-log4j12${slf4j.version} org.springframework spring-jdbc${spring.version}
持久层和业务层就是一些简单的增删改查:
@Controller
public class AccountController {
@Autowired
private AccountService accountService;
public List findAll() throws SQLException {
return accountService.findAll();
}
public Account findById(int id) throws SQLException {
return accountService.findById(id);
}
public void deleteById(int id) throws SQLException {
accountService.deleteById(id);
}
public void add(Account account) throws SQLException {
accountService.add(account);
}
public void update(Account account) throws SQLException {
accountService.update(account);
}
}
public interface AccountService {
//查询所有账号信息
List findAll() throws SQLException;
//根据id查询账号信息
Account findById(int id) throws SQLException;
//根据id删除账号信息
void deleteById(int id) throws SQLException;
//添加账号信息
void add(Account account) throws SQLException;
//修改账号信息
void update(Account account) throws SQLException;
}
这是config层用来替代applicationContext.xml
@Configuration
@ComponentScan(basePackages = "com.hncgzy")
@import(MybatisConfig.class)
public class SpringConfig {
}
@PropertySource("classpath:mybatis.properties")
public class MybatisConfig {
@Value("${mybatis.username}")
private String username;
@Value("${mybatis.password}")
private String password;
@Value("${mybatis.url}")
private String url;
@Value("${mybatis.driver}")
private String driver;
@Value("${mybatis.typeAliases.package}")
private String typeAliasesPackage;
@Value("${mybatis.mapper.package}")
private String mapperPackage;
@Bean
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setUrl(url);
dataSource.setDriverClassName(driver);
return dataSource;
}
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
return sqlSessionFactoryBean;
}
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setbasePackage(mapperPackage);
return mapperScannerConfigurer;
}
}
这是sql语句:
这是错误原因:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getMapperScannerConfigurer' defined in com.hncgzy.config.MybatisConfig: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'basePackage' is required
求大神指点。owo



