SpringBoot与MyBatis-Plus整合(01--SqlSessionFactory的注入)
    		       		warning:
    		            这篇文章距离上次修改已过434天,其中的内容可能已经有所变动。
    		        
        		                
                
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper") // 指定Mapper接口所在包
public class MyBatisPlusConfig {
 
    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource); // 设置数据源
        // 这里可以设置更多的MyBatis-Plus配置属性
        return sqlSessionFactory;
    }
}这段代码定义了一个配置类MyBatisPlusConfig,它使用@Configuration注解标注该类为配置类。通过@MapperScan注解指定了Mapper接口所在的包。同时,它定义了一个方法sqlSessionFactory,该方法创建了一个MybatisSqlSessionFactoryBean实例,并设置了数据源。这个SqlSessionFactory实例会被Spring框架自动装配到需要它的Bean中去。
评论已关闭