SSM框架Spring和Mybatis整合问题

请问在两者整合期间我是那里出了问题呀
这是我的数据库内容

img


这是我的xml文件

img


这是我的mybatis-config.xml文件

img


这是我的dao层接口

img


这是我的dao层application-dao.xml文件(Spring和Mybatis整合配置)

img

img


这是我的service层接口

img


这是我的Service层接口的实现类(爆红出错的地方)

img

请问为什么出错呀!我不熟悉Mybatis和Spring的整合,如果您能讲解一下就更好了
最后这是整个项目结构

img

img

img

因为你没有加@Mapper注解或者在启动类哪儿加入@MapperScan注解,导致在dao层没有正常注入bean,你用@Autoware注解就获取不到没有注入的dao,所以会报错

  • 这篇博客: SSM整合01---Spring-Mybatis层中的 八、spring-dao.xml 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                               http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!--    关联数据库配置文件-->
        <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    
    <!--    数据库连接池 c3p0-->
        <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
            <property name="driverClass" value="${driver}"></property>
            <property name="jdbcUrl" value="${url}}"></property>
            <property name="user" value="${username}}"></property>
            <property name="password" value="${password}}"></property>
        </bean>
    
    <!--    sqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
            <property name="mapperLocations" value="classpath:cn/kexing/dao/*.xml"></property>
        </bean>
    
    <!--    SqlSessionTemplate-->
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
    
        <bean class="cn.kexing.dao.BookMapperImpl" id="bookMapper">
            <property name="sqlSession" ref="sqlSession"></property>
        </bean>
    </beans>
    

    db.properties:

    driver=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://localhost://3306/ssmbuild?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=true
    username=root
    password=123
    
  • 您还可以看一下 李礼强老师的spring+springMVC+mybatis(ssm框架)在线商城电子商务系统实战开发教程课程中的 第二讲创建项目设计商品分类实体、service、dao层小节, 巩固相关知识点

你使用的不对吧

img