作为一个Spring的初学者,检查过文件的配置,全部都是正确的,但是不知道为什么我已启动就是Error creating bean with name 'xxx' defined in javaconfig问题。直接上代码,求大神指点。
这是我的项目结构:
这是我引入的依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hncgzy.wutao</groupId>
<artifactId>day02_spring_04</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--spring-->
<spring.version>5.0.2.RELEASE</spring.version>
<!--日志打印框架-->
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<!--mysql-->
<mysql.version>8.0.13</mysql.version>
<!--mybatis-->
<mybatis.version>3.5.3</mybatis.version>
<!--junit-->
<junit.version>4.12</junit.version>
<!--lombok-->
<lombok.version>1.18.8</lombok.version>
<!--mybatis-spring-->
<mybatis.spring.version>1.3.0</mybatis.spring.version>
</properties>
<dependencies>
<!--引入相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--mysql的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--
引入依赖
1. mybatis
2. mybatis-spring
3. log
4. 引入spring-jdbc
-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--
mybatis整合spring的依赖
-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
持久层和业务层就是一些简单的增删改查:
@Controller
public class AccountController {
@Autowired
private AccountService accountService;
public List<Account> 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<Account> 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语句:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hncgzy.dao.AccountDao">
<select id="findAll" resultType="Account">
select *
from account;
</select>
<select id="findById" resultType="Account" parameterType="int">
select *
from account
where id = #{id}
</select>
<delete id="deleteById" parameterType="int">
delete
from account
where id = #{id}
</delete>
<insert id="add" parameterType="Account">
inner into account(name,money) values (
#{name},
#{money}
)
</insert>
<update id="update" parameterType="Account">
update account
set name = #{name},
money=#{money}
where id = #{id}
</update>
</mapper>
这是错误原因:
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