Spring 整合 MyBatis 遇到的错误

问题一:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Pointcut

解决办法:

加入 下面依赖:



    org.aspectj
    aspectjweaver
    1.9.6
    runtime

问题二:使用全注解整合Mybatis 时没能创建Mapper 的Bean

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.power.node.bank.mapper.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

解决办法:加入Mapper 扫描

@Bean
public MapperScannerConfigurer getMapperScan(){
    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    mapperScannerConfigurer.setBasePackage("com.power.node.bank.mapper");
    return mapperScannerConfigurer;
}

全注解Config 配置:

package com.power.node.bank;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 *
 * @Author darren
 * @Date 2022/11/23 19:15
 */
@ComponentScan("com.power.node.bank")
@EnableAspectJAutoProxy
public class SpringConfig {

    @Bean(name = "myDruidDataSource")
    public DruidDataSource getDruidDataSource(){
        DruidDataSource myDruidDataSource = new DruidDataSource();
        myDruidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        myDruidDataSource.setUrl("jdbc:mysql://localhost:3306/test");
        myDruidDataSource.setUsername("root");
        myDruidDataSource.setPassword("root");
        return myDruidDataSource;
    }

    @Bean(name = "sqlSessionFactoryBean")
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DruidDataSource myDruidDataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(myDruidDataSource);
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config2.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("com.power.node.bank.pojo");
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer getMapperScan(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.power.node.bank.mapper");
        return mapperScannerConfigurer;
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager getTransactionManager(DruidDataSource myDruidDataSource) {
        DataSourceTransactionManager dataSourceTransactionManager =
                new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(myDruidDataSource);
        return dataSourceTransactionManager;
    }
}

对照XML配置:




    
    

    
    

    
    
        
        
        
        
    

    
    
        
        
        
    

    
    
        
    

    
    
        
    

    
    

Spring 整合 MyBatis 遇到的错误_第1张图片

 

你可能感兴趣的:(Spring,MyBatis,mybatis,spring,java)