Using mybatis in springboot, a bean of type mapper interface could not be found

99 Views Asked by At

I'm a new hand in springboot and mybatis, I just write an empty springboot project and I want to connect it to a mariadb using mybatis. I use maven to construct my project. I write my code about mybatis according to its website: http://mybatis.org/spring/getting-started.html

The file system looks like this:


│   ├── main
│   │   ├── java
│   │   │   └── ct
│   │   │       └── ariel
│   │   │           └── helloworld
│   │   │               ├── HelloworldApplication.java
│   │   │               ├── config
│   │   │               │   └── MyBatisConfig.java
│   │   │               ├── http
│   │   │               │   └── Demo.java
│   │   │               ├── mapper
│   │   │               │   └── UserMapper.java
│   │   │               └── model
│   │   │                   └── UserDo.java
│   │   └── resources
│   │       ├── application.properties
│   │       ├── applicationContext.xml

I have mybatis config as

package ct.ariel.helloworld.config;

@Configuration
public class MyBatisConfig {
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        return factoryBean.getObject();
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://10.0.0.118:3306/ariel?useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }
}

the UserMapper interface is

package ct.ariel.helloworld.mapper;

public interface UserMapper {
    @Select("select * from user")
    List<UserDo> selectAll();
}

and I use it in my http api Demo as

@Autowired
private UserMapper userMapper;

I define this bean in applicationContext.xml as

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Bean definitions go here -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="ct.ariel.helloworld.mapper.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

</beans>

When I start the app, I get this error: 2024-01-20T15:38:08.110+08:00 WARN 15927 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo': Unsatisfied dependency expressed through field 'userMapper': No qualifying bean of type 'ct.ariel.helloworld.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

What did I do wrong?

I'm expecting the app can start normally and can access the mariadb

0

There are 0 best solutions below