一、导入jar包
jar包地址:链接:https://pan.baidu.com/s/1EzkZqRBQ3es66UVu3bkY7A 提取码:kv1h
二、配置Web.xml
【1】配置前端控制器
<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
【2】配置Post请求乱码处理
<filter>
<filter-name>characterEncoding</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>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
【3】配置Spring容器初始化
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
web.xml中主要配置,一个是SpringMVC的前端控制器,一个是Spring框架的初始化加载配置文件。
二、配置Springvc.xml
【1】引入约束
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
">
</beans>
【2】配置包扫描
<context:component-scan base-package="com.easymall.web"></context:component-scan>
【3】配置SpringMvc注解开发
<mvc:annotation-driven/>
【4】配置视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
三、配置applicationContext.xml
【1】引入约束文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
">
</beans>
【2】配置包扫描
<context:component-scan base-package="com.easymall.mapper"></context:component-scan>
<context:component-scan base-package="com.easymall.service"></context:component-scan>
<context:component-scan base-package="com.easymall.web"></context:component-scan>
【3】开启依赖注入注解配置
<context:annotation-config/>
【4】开启AOP注解配置
<aop:aspectj-autoproxy/>
【5】配置数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///mybatis"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
【6】配置事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager ">
<property name="dataSource" ref="dataSource"></property>
</bean>
【7】整合Mybatis SqlSessionFactory
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:/sqlMapperConfig.xml"></property>
<property name="mapperLocations" value="classpath:/mapper/*.xml"></property>
</bean>
主要注入数据源、sqlMapperConfig所在地、映射文件存在位置。/*表示mapper文件夹下的所有文件
【8】配置MapperBean扫描器,负责为MapperBean生成实现类
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.easymall.mapper"></property>
</bean>
我们在使用mybatis框架的时候,一般是接口文件+映射文件,进行封装数据。虽然,不需要我们书写接口文件,但是框架的底层会帮我们动态代理出一个接口的实体类。
四、配置sqlConfigLocation.xml
【1】引入约束
<?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>
</configuration>
【2】配置别名操作
<typeAliases>
<typeAlias type="com.easymall.domain.User" alias="user"/>
</typeAliases>