目录
1.web应用加入spring,在web.xml下配置监听器(导jar)
2.spring整合mybatis,mybatis将conf.xml的内容(主要是DataSource和mapper.xml)融入到springIOC容器中(导入各种jar)
3.配置springmvc
4.spring扫描dao层和service层的包,springmvc扫描controller(或称action、handler)层的包。
<!-- web应用需要引入spring -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
(以上在开发中可以用 标签方法 实现)
第二步:生成sqlLSessionFactory核心类,在子标签中配置datasource和mapperLocation<!--1. ==========在SpringIOC容器中 创建mybatis的核心类SqlSessionFactory========== -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mapper.xml路径 ,相当于mybatis中的mappers-->
<!-- =================替代mybatis中的mappers配置==================== -->
<property name="mapperLocations" value="org/jkl/mapper/*.xml"></property>
</bean>
第三步:将sqlSessionFactory交由spring管理。<!-- 第三种 生成 mapper代理对象 的方法 ,mappers组合,不在需要配置多个mapper-->
<!-- 这里的id值已经不能指向特定的mapper,批量产生的Mapper对象在SpringIoc的id值默认就是接口名(接口名=对应的id值 -->
<bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- spring整合mybatis,就是将mybatis的sqlSessionFactory交由spring管理 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="org.jkl.mapper"></property>
</bean>
<注意>mybatis中已经将dao层写好,只需要写动态代理接口,其实现类已经写好(其实就是封装mapper.xml中的各种SQL标签,标签的ID值就是实现类的对象名)
mybatis中的mapper就是dao。
对象注入通过setter 或者构造器,因此引用对象的类中要写对象的setter方法。
----------------------------------------------------
service层中可以通过添加@Service达到省略配置的目的。
<!-- 依赖注入:给service注入dao -->
<bean id="studentService" class="org.jkl.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
上述可以用注解替代配置bean的形式,只需在类前加注解@Service如下:
@Service("studentService")
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentMapper studentMapper;
...//setter
}
使用了对应层的注解,便要在相应的配置文件里添加注解扫描器!
----------------------------------------------------
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
第二步:在springmvc.xml配置springmvc所依赖的视图解析器<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
第三步:springmvc基础配置、标配,很多功能都需要依赖此配置。springmvc.xml中配置:
<!-- 设置默认配置方案 -->
<mvc:annotation-driven/>
第四步:若有静态资源,不想经过dispatcherServlet被视图解析器解析,需要在 springmvc中配置如下:<!-- 使用默认的servlet来响应 《静态文件 》 -->
<mvc:default-servlet-handler/>
如果不在spring中配置扫描器,springmvc中将没有对象,没有对象就没有controller,所以在加载处理器,适配器的时候会找不到映射对象(映射关系),因此会在页面上出现404。
springmvc的执行流程如下: