SSM整合
Spring--SpringMVC--MyBatis
1.Spring--MyBatis: 整合将SqlSessionFactory交给Spring
2.Spring--SpringMVC: 将 Spring-SpringMVC各自配置一遍
步骤:
1.jar包;
2.类-表;
3.整合时,conf.xml可省,放入spring配置文件applicationContext.xml;
4.通过mapper.xml将类、表建立映射关系;
5.配置Spring配置文件:applicationContext.xml;
web.xml;
<!--web项目中引入Spring--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
6.使用Spring整合myBatis:将SqlSessionFactory交给Spring
方式:扫描包
<!--使用Spring整合myBatis--> <!--第三中方式生成mapper对象 批量产生mapper对在ioc中的id值默认为接口名,接口名=id,接口名首字母小写 --> <bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--将SqlSessionFactory交给Spring--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!--指定批量产生那个包中的mapper对象--> <property name="basePackage" value="org.ghl.mapper"></property> </bean>
7.继续整合SpringMVC:将SpringMVC加入项目即可
a.加入SpringMVC需要的jar:
spring-webmvc.jar
b.给项目加入SpringMVC支持
<!--整合SpringMVC-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-controller.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
c.编写SpringMVC配置文件:applicationContext-controller.xml
<!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--SpringMVC标配--> <mvc:annotation></mvc:annotation>