SSM整合遇到的问题,非常干货,希望能帮助到您~

你们好,我是金金金。

无法自动装配

  • 配置类已经配置了扫描

那是什么原因导致?

解决

  • 很明显位置都不在一起,需要更改。

结果类型不匹配select id="selectEmployeeByCondition"

什么原因导致?

  • 这个是因为我建立了很多子模块 名字一样导致找到别的包下面去了

解决

  • 更改名称即可

java.sql.SQLException: url not set

什么原因导致?

  • 当你在Spring配置类中添加了sqlSessionFactoryBeanmapperScannerConfigurer配置方法时,可能会导致@Value注解读取不到值为null的问题。这是因为SqlSessionFactoryBeanMapperScannerConfigurer是基于MyBatis框架的配置,它们的初始化顺序可能会导致属性注入的问题。
    SqlSessionFactoryBeanMapperScannerConfigurer在配置类中通常是用来配置MyBatis相关的Bean,例如数据源、事务管理器、Mapper扫描等。这些配置类通常在@Configuration注解下定义,并且使用@Value注解来注入属性值。
    当配置类被加载时,Spring容器会首先处理Bean的定义和初始化,其中包括sqlSessionFactoryBeanmapperScannerConfigurer的初始化。在这个过程中,如果@Value注解所在的Bean还没有被完全初始化,可能会导致注入的属性值为null。

解决

  • 分成两个配置类独立配置,互不影响,数据库提取一个配置类,mybatis提取一个配置类即可解决!

数据库配置类(DataSourceJavaConfig.java

java 复制代码
@Configuration
@PropertySource("classpath:jdbc.properties")
public class DataSourceJavaConfig {


    @Value("${jdbc.user}")
    private String user;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.driver}")
    private String driver;


    //数据库连接池配置
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        return dataSource;
    }

}

mybatis配置类(MapperJavaConfig.java

java 复制代码
@Configuration // 配置类
public class MapperJavaConfig {
/**
   * 配置SqlSessionFactoryBean,用于创建SqlSessionFactory。
   * 这个方法需要一个DataSource作为参数,用来设置SqlSessionFactoryBean的dataSource属性。
   * 它还从类路径下的"mybatis-config.xml"文件中加载MyBatis的配置。
   *
   * @param dataSource 数据源对象,用于数据库连接。
   * @return SqlSessionFactoryBean配置对象,可用于创建SqlSessionFactory。
   */
  @Bean
  public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource); // 设置数据源

    Resource resource = new ClassPathResource("mybatis-config.xml"); // 加载配置文件
    sqlSessionFactoryBean.setConfigLocation(resource);
    return sqlSessionFactoryBean;
  }
  /**
   * 配置MapperScannerConfigurer,用于扫描指定包下的Mapper接口。
   * 该方法不接受任何参数,返回一个配置好的MapperScannerConfigurer实例。
   *
   * @return MapperScannerConfigurer 配置了基础包名的MapperScannerConfigurer实例。
   */
  @Bean
  public MapperScannerConfigurer mapperScannerConfigurer() {
    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    mapperScannerConfigurer.setBasePackage("com.yjx.mapper"); // //设置mapper接口和xml文件所在的共同包
    return mapperScannerConfigurer;
  }
}

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

  • 意思是:没有类型为"javax.sql"的合格bean。数据源可用:预计至少有1个bean符合自动候选条件

什么原因导致?

  • 因为第3点我拆成了两个配置文件,导致sqlSessionFactoryBean方法里面找不到dataSource了

解决

  • 编写有误还请大佬指正,万分感谢。
相关推荐
喵个咪7 分钟前
Go Wind UBA 拆解系列 - 架构总览:三服务、数据流与契约优先
大数据·后端·go
喵个咪8 分钟前
Go Wind UBA 拆解系列 - 多租户与安全:两套隔离机制的边界
大数据·后端·go
喵个咪9 分钟前
Go Wind UBA 拆解系列 - OLAP 与 SQL 硬核:25 个分析模型怎么落地
大数据·后端·go
喵个咪10 分钟前
Go Wind UBA 拆解系列 - SDK 与采集层:从浏览器到 Kafka
大数据·后端·go
掘金一周15 分钟前
对车完全小白,不知买油买电还是买混动,求建议| 沸点周刊 7.2
前端·人工智能·后端
parade岁月31 分钟前
MySQL JOIN解析:朴实无华但食之有味
数据库·后端
妙码生花32 分钟前
从 PHP 到 AI + Golang,程序员自救转型手记(十六):目录结构更新、完善 token 系统(AI 表示 token 入库无需加密?)
前端·后端·ai编程
程序me37 分钟前
Prompt、Context、Harness、Loop 之后是什么? AI工程下一个半年的关键词
前端·后端·ai编程
米沙AI1 小时前
go语言项目--实例化(图书管理)--v1
后端
MeixianAgent1 小时前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python