You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.5 KiB
37 lines
1.5 KiB
package com.example.demo.config;
|
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
import org.mybatis.spring.SqlSessionFactoryBean; // 修复:正确导入类
|
|
import org.mybatis.spring.SqlSessionTemplate;
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Primary;
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
/**
|
|
* 数据库leadnews_user配置
|
|
*/
|
|
@Configuration
|
|
@MapperScan(basePackages = {"com.example.demo.mapper.db2"}, sqlSessionFactoryRef = "sqlSessionFactoryUser")
|
|
public class dBSrcUser {
|
|
|
|
@Bean
|
|
@Primary
|
|
public SqlSessionFactory sqlSessionFactoryUser(@Qualifier("mysql2") DataSource dataSource) throws Exception {
|
|
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean(); // 修复:使用正确类名
|
|
sqlSessionFactory.setDataSource(dataSource);
|
|
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
|
|
.getResources("classpath:mapper/db2/*.xml")); // 确保路径正确
|
|
return sqlSessionFactory.getObject();
|
|
}
|
|
|
|
@Bean
|
|
@Primary
|
|
public SqlSessionTemplate sqlSessionTemplateUser(@Qualifier("sqlSessionFactoryUser") SqlSessionFactory sqlSessionFactory) throws Exception {
|
|
return new SqlSessionTemplate(sqlSessionFactory);
|
|
}
|
|
}
|