在整合时controller调用service业务逻辑
service层调用dao层和数据库交互

解决思路:

1.bean有没有注入 √
2.junit单元测试,看service调用dao层能否查询成功 √
3.所以问题出在了spring上
…1):springMVC的配置文件,指向的是dispatcherServlet.xml,里面没有service的bean

视频所使用的解决方法:

1.将web.xml中springMVC配置所指向的文件改为applicationContext.xml
2.新建一个xml文件(称之为service层配置文件),里面的内容是原先applicationContext.xml的内容,自动扫描的包是service层,在applicationContext.xml里将两个或以上的配置import进来
3.DispatcherServlet.xml自动扫描的包是controller层
4.在service新建一个接口,接口的方法与mapper接口的方法一模一样(要除去@Param),然后再实现该接口的实例方法,方法中:加入mapper接口属性,并实现该属性的setter方法,再在配置文件中(service层配置文件)把该类注入:

public class BooksServiceImpl implements BooksService{
    private BooksMapper booksMapper;
    public void setBooksMapper(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }
    @Override
    public Books selectById(Integer id) {
        return booksMapper.selectById(id);
    }
    ......
}
=====================================================================
再在service层配置文件把该类注入:
<bean id="booksServiceImpl" class="com.springMVC.CRUD.service.BooksServiceImpl">
        <property name="booksMapper" ref="booksMapper"/>
    </bean>
    class:新定义的接口的全类名
    property -ref指向mapper接口

最后applicationContext.xml只有如下配置:

已忽略的文件头...
<!--    dao层:数据源+sqlSessionFactory-->
    <import resource="classpath:spring-dao.xml"/>
<!--    service层:配置自动扫描service的bean,并将业务类注入进来-->
    <import resource="classpath:spring-service.xml"/>
<!--    注入dispatcherServlet.xml-->
    <import resource="dispatcherServlet.xml"/>

具体的配置文件如下:我测试过:spring-dao.xml与spring-service.xml可以合在一起

spring-dao.xml
已忽略的文件头...
    <context:component-scan base-package="com.lai.ssm.service"/>
    <!--    引入配置文件来配置数据库-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--    spring用来控制业务逻辑等等 数据源 事务控制-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <!--                jdbc:mysql://000.000.000.000:3306/etc?serverTimezone=UTC-->
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--        使用的数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--        指定全局配置文件的位置-->
        <property name="configLocation" value="classpath:MyBatisConfig.xml"/>
        <property name="mapperLocations" value="classpath:mappers/BooksMapper.xml"/>
    </bean>
    <!--    扫描MyBatis接口的实现,加入到IOC容器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--   指定mapper接口的包名,一般在dao层-->
        <property name="basePackage" value="com.lai.ssm.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <!--    配置一个可以执行批量操作的bean-->
    <bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <constructor-arg name="executorType" value="BATCH"/>
    </bean>
</beans>

spring-service.xml,注意注入进来的bean

已忽略的头文件...
    <context:component-scan base-package="com.lai.ssm.service"/>
<!--    将业务类注入进来-->
    <bean id="BooksServiceImpl" class="com.lai.ssm.service.BooksServiceImpl">
        <property name="booksMapper" ref="booksMapper"/>
       <!--这里是service层定义了接口,接口的方法和dao层一样,然后实现类调用的是dao层的实现,实现类代码贴在最后-->
    </bean>
    
    <!--    =================================事务控制=================================-->
    <!--    事务管理器-->
    <bean id="dataSourceTransactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--        控制数据库的事务,引进property属性里面-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

web.xml,注意spring和springMVC的配置文件的书写

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <!--    配置spring配置文件,启动spring容器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--        配置spring配置文件的路径-->
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
  </listener>

  <!--配置springMVC配置文件-->
  <!--    配置DispatcherServlet-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
      <!--      是applicationContext.xml而不是dispatcherServlet.xml-->
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 过滤器1: 解决中文乱码.字符过滤器 要放在所有filter之前-->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceRequestEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

<session-config>
  <session-timeout>15</session-timeout>
</session-config>

  <!--过滤器2: 使用Rest风格的URI,可以把POST请求转化为DELETE 或 PUT-->
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

service实现的接口类:

package com.lai.ssm.service;

import com.lai.ssm.dao.BooksMapper;
import com.lai.ssm.pojo.Books;
import com.lai.ssm.pojo.BooksExample;
import com.lai.ssm.pojo.BooksKey;

import java.util.List;

public class BooksServiceImpl implements BooksService{
    private BooksMapper booksMapper;
    public void setBooksMapper(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }

    @Override
    public Books selectById(Integer id) {
        return booksMapper.selectById(id);
    }

    @Override
    public long countByExample(BooksExample example) {
        return booksMapper.countByExample(example);
    }

   ......一堆的数据库实现返回代码
}