naijuxO_o 2024-05-01 23:22 采纳率: 0%
浏览 20
已结题

mybatis的代理对象无法通过@Autowired装填

如题,题主正在学ssm,学到了spring整合Mybatis这一块,但mapper的代理对象自动装填这里一直报错(我一个字一个字照着黑马的视频敲的),网上也找不到解决方案了

img

这是整体项目目录

img

这是JdbcConfig

package com.xxx.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.Configuration;
import javax.sql.DataSource;

public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }
}

这是MybatisConfig

package com.xxx.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.xxx.config.JdbcConfig;
import javax.sql.DataSource;
public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        //设置模型类的别名扫描
        ssfb.setTypeAliasesPackage("com.xxx.domain");
        //设置数据源
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc =new MapperScannerConfigurer();
        msc.setBasePackage("com.xxx.dao");
        return msc;
    }
}

这是SpringConfig

package com.xxx.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan("com.xxx")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MybatisConfig.class})
public class SpringConfig {

}

这是AccountDao

package com.xxx.dao;

import com.xxx.domain.Account;
import org.apache.ibatis.annotations.*;

import java.util.List;
public interface AccountDao {
    @Insert("insert into tbl_account(name,money)values(#{name},#{money})")
    void save(Account account);
    @Delete("delete from tbl_account where id = #{id} ")
    void delete(Integer id);
    @Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ")
    void update(Account account);
    @Select("select * from tbl_account")
    List<Account> findAll();
    @Select("select * from tbl_account where id = #{id} ")
    Account findById(Integer id);
}

这是Account

package com.xxx.domain;

import java.io.Serializable;

public class Account implements Serializable {
    public Integer id;
    public String name;
    public double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}


这是AccountServiceImpl

package com.xxx.service.impl;

import com.xxx.dao.AccountDao;
import com.xxx.domain.Account;
import com.xxx.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;
    public void save(Account account) {
        accountDao.save(account);
    }
    public void update(Account account){
        accountDao.update(account);
    }
    public void delete(Integer id) {
        accountDao.delete(id);
    }
    public Account findById(Integer id) {
        return accountDao.findById(id);
    }
    public List<Account> findAll() {
        return accountDao.findAll();
    }
}


这是AccountService

package com.xxx.service;

import com.xxx.domain.Account;

import java.util.List;

public interface AccountService {
    void save(Account account);
    void delete(Integer id);
    void update(Account account);
    List<Account> findAll();
    Account findById(Integer id);
}

这是App

import com.xxx.config.SpringConfig;
import com.xxx.dao.AccountDao;
import com.xxx.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;

public class App {
    public static void main(String[] args) {

        ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
        AccountService cs = ctx.getBean(AccountService.class);
        cs.findById(1);
        System.out.println(cs);

    }
}


这是jdbc.properties


jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/Mybatis?useSSL=false
jdbc.username=root
jdbc.password=131466

这是数据库

img

还望各位不吝赐教

  • 写回答

14条回答 默认 最新

  • 专家-皆知 2024-05-01 23:22
    关注

    参考免费微信小程序:皆我百晓生 作答,经由专家-皆知整理回复,如问题解决,望采纳;如仍有疑问,请评论回复。

    从你提供的信息来看,问题可能出在几个关键点上,让我们逐一检查并尝试解决。

    1. 确保包扫描路径正确

    确保Spring的组件扫描路径包含了所有需要被管理的类。在SpringConfig中,你已经设置了扫描com.xxx包下的所有组件,这看起来是正确的,只要你的AccountDao接口、实现类以及服务类等都位于这个包或其子包下。

    2. 检查接口和实现类的命名与注解

    确保你的AccountDao接口有对应的Mapper实现(尽管MyBatis通常不需要手动编写实现类,但确保接口命名正确)。同时,确认AccountServiceAccountServiceImpl的命名与注入路径一致,且AccountServiceImpl上有@Service注解。

    3. MapperScannerConfigurer配置

    你已经在MybatisConfig中配置了MapperScannerConfigurer来扫描Mapper接口,这是正确的。但要注意的是,配置的basePackage需要精确到包含Mapper接口的包名,你已经设为了com.xxx.dao,看起来没有问题。

    4. ComponentScan是否遗漏

    确保所有配置类(JdbcConfig, MybatisConfig, SpringConfig)都被正确扫描或导入。你已通过@ImportSpringConfig中引入了其他配置,这应该足够了。

    5. 数据库连接与配置文件

    检查你的jdbc.properties文件中的数据库连接信息是否正确无误,包括URL、用户名、密码等,以及驱动类名是否与你的MySQL版本兼容。

    6. Bean命名冲突或类型不匹配

    虽然你未提及,但有时Spring容器中可能存在同名或类型不明确的Bean导致注入失败。确保没有其他地方定义了相同类型的Bean(例如,另一个AccountDao实例)。

    解决方案检查清单

    • 确认包结构:所有类应正确放置在指定的包及其子包内。
    • 检查接口与实现:确保接口与实现类(如果有)的命名准确,且服务类实现了正确的接口。
    • 重新审视配置:再次确认MybatisConfig中的MapperScannerConfigurer配置的包路径是否精确。
    • 验证数据库配置:测试数据库连接是否成功,可以尝试在应用启动时打印数据源信息来确认。
    • Spring日志查看:开启详细的Spring日志,特别是在调试阶段,这样可以帮助定位问题,比如Bean加载失败的具体原因。
    • 清除并重建项目:有时候IDE的一些缓存问题也可能导致注入失败,尝试清理项目并重新构建。

    如果以上步骤都检查无误,且问题依旧存在,可能需要更详细的错误日志来进一步分析。请尝试在启动应用时增加日志级别,特别是关注Spring初始化和Bean创建相关的日志信息。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月2日
  • 修改了问题 5月1日
  • 创建了问题 5月1日

悬赏问题

  • ¥30 不会,学习,有偿解答
  • ¥15 SQL查询语句报错(检查)
  • ¥15 此表中公式应该怎么写
  • ¥15 求HI-TECH PICC 9.50 PL3安装包
  • ¥15 下载ctorch报错,求解
  • ¥15 如何入门学习c语言,单片机
  • ¥15 idea 编辑语言的选择
  • ¥15 Windows下部署Asmjit
  • ¥15 请问双层规划模型的上下层目标函数不一致,是如何保证迭代收敛性的
  • ¥30 微信小程序 前端页面内容搜索