Discuss / Java / 看完后有个疑问:SqlSessionFactoryBean 这个好像没有显示的用在哪里,不用注入到哪个类吗,然后通过这个类操作数据库?像@Select这样子的注解,是自动连接到数据的吗?

看完后有个疑问:SqlSessionFactoryBean 这个好像没有显示的用在哪里,不用注入到哪个类吗,然后通过这个类操作数据库?像@Select这样子的注解,是自动连接到数据的吗?

Topic source

看完后有个疑问:SqlSessionFactoryBean 这个好像没有显示的用在哪里,不用注入到哪个类吗,然后通过这个类操作数据库?像@Select这样子的注解,是自动连接到数据的吗?

不懂,哪位大佬帮忙解惑

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

注意这个

@MapperScan("com.itranswarp.learnjava.mapper")

MyBatis在启动时自动给每个Mapper接口创建如下Bean:

@Component
public class UserMapperImpl implements UserMapper {
    @Autowired
    SqlSessionFactory sessionFactory;

    public List<User> getAllUsers() {
        String sql = getSqlFromAnnotation(...);
        try (SqlSession session = sessionFactory.createSession()) {
            ...
        }
    }
}

Spring允许动态创建Bean并添加到applicationContext中


  • 1

Reply