jdbc.config
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
@PropertySource("classpath:druid.properties")
public class JdbcConfig {
@Value("${driverClassName}")
private String driverClassName;
@Value("${druid.url}")
private String url;
@Value("${druid.username}")
private String username;
@Value("${druid.password}")
private String password;
@Bean("dataSource")
public DataSource getDataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
spring.config
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
@ComponentScan("vx")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}
mybatis.config 正常写法
mport org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import vx.service.Impl.MoneyServiceImpl;
import javax.sql.DataSource;
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setTypeAliasesPackage("vx.daomain");
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
mybatis.config 错误写法
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import vx.service.Impl.MoneyServiceImpl;
import javax.sql.DataSource;
public class MybatisConfig {
@Autowired
private DataSource dataSource;
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setTypeAliasesPackage("vx.daomain");
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
如上图 ,求指导
@Autowired
private DataSource dataSource;
换成
@Resource
private DataSource dataSource;试一下
@Autowired 注解,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。好像没有看到你的DataSource dataSource配置在哪里。只看到getDataSource注册为bean
这种的话,十有八九是配置类加载先后的问题,
MybatisConfig后于JdbcConfig的话,MybatisConfig可以加DependsOn注解,
或者参考springboot mybatis的自动配置@AutoConfigureAfter({JdbcConfig.class})
@Autowired(required = false)或者
@Autowired
@Lazy