步骤: 1、导入相关的jar (SpringMVC是Spring的一个方向的延申,使用时需要在Spring的基础上操作) spring-core、spring-beans、spring-expression、spring-jcl、spring-test、spring-context、spring-web、spring-webmvc、spring-aop 2、配置DispatcherServlet(SpringMVC本质还是通过servlet来进行请求及响应的处理,SpringMVC中的前端控制器是不需要程序员手动创建的,只需要在tomcat容器初始化时进行加载即可,因为需要通过servlet去读取配置文件,因此在加载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:springMVC.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <!--指定拦截请求的规则--> <url-pattern>*.do</url-pattern> </servlet-mapping>3、创建配置文件(因为SpringMVC的使用需要Spring的支持,同时需要使用相关的注解)
<?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" 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"> <!-- 配置扫描 --> <context:component-scan base-package="cn.yunhe.controller"/> </beans>4、接收请求并根据请求执行对应的业务,反馈结果(将视图和数据模型返回出去–ModelAndView)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> code--${code } <hr> msg--${msg } <hr> data--${data } </body> </html>6、测试–启动tomcat
目标:通过SSM的形式实现数据列表的展示效果 步骤: 1、导入jar spring-core、spring-beans、spring-expression、spring-jcl、spring-test、spring-context spring-web、spring-webmvc、spring-aop、spring-tx、spring-jdbc、spring-aspect、aop联盟、织入、mybatis、mybatis-spring、mysql-connector-java 2、创建bean、dao层,指定要做的功能
@Repository public interface IUserDao { /** * 查询用户列表 * @return */ List<User> queryUsers(); }3、创建Mybatis的配置文件及数据库文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置别名 --> <typeAliases> <package name="cn.yunhe.beans"/> </typeAliases> </configuration> jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///shop jdbc.username=root jdbc.password=777777774、创建Spring的配置文件
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 开启扫描 --> <context:component-scan base-package="cn.yunhe.dao"/> <context:property-placeholder location="classpath:db.properties"/> <!-- 1、配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 2、配置Mybatis的mapper扫描,批量加载 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.yunhe.dao"/> <property name="sqlSessionFactoryBeanName" value="ssf"/> </bean> <!-- 3、创建SqlSessionFactory的实例 --> <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 3.1 加载Mybatis的相关配置 --> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> <!-- 3.2 关联数据源 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 4、声明式事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 5、事务管理策略 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="query*" read-only="true"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 6、织入事务 --> <aop:config> <aop:pointcut expression="execution(* cn.yunhe.dao..*(..))" id="myPointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"/> </aop:config> </beans>5、配置前端控制器
<!-- 配置前端控制器 --> <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:spring*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>6、创建接收和处理请求的功能
@Controller public class UserController { @Autowired IUserDao userDao; @RequestMapping("queryUsers.do") public ModelAndView queryUsers() { ModelAndView mv = new ModelAndView(); Map<String,Object> map = new HashMap<>(); List<User> userList = userDao.queryUsers(); map.put("code", 0); map.put("msg", "success"); map.put("count", userList.size()); map.put("data", userList); mv.addAllObjects(map); mv.setViewName("index.jsp"); return mv; } }7、测试–启动tomcat 8、项目目录结构 9、配置视图解析器的前缀和后缀(springmvc的配置文件中)
<!-- 配置扫描 --> <context:component-scan base-package="cn.yunhe.controller"/> <!-- 代替注解适配器和注解映射器的配置 --> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="suffix" value=".jsp"></property> </bean>