一、導入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>