applicationContext.xml
1.配置数据源(此处用的阿里的Druid连接池)
<bean class="com.alibaba.druid.pool.DruidDataSource" id="druidDataSource">
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver "/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
2.配置mybatis的sqlSessionFactory工厂
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
3 .自动扫描包,并且生成mapper的实现类
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.sxt.mapper"/>
<!--不用配置SqlSessionFactory,自动注入-->
<!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>-->
</bean>
4.配置事务核心管理器(利用Spring事务机制必配)
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
5.开启注解事务
SpringMVC.xml
1.开启注解扫描
<context:component-scan base-package="com.wang"/>
2.配置处理器映射器,处理器适配器
<mvc:annotation-driven/>
3.配置视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
4.配置处理器对静态资源放行
<mvc:resources mapping="/css/**" location="/WEB-INF/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/"/>
MybatisConfig.xml
<configuration>
<!-- 设置别名 -->
<typeAliases>
<!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
<package name="com.wang.po" />
</typeAliases>
</configuration>
Web.xml
1.配置spring容器相关的资源文件和创建容器的监听类
<!--定位spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--web容器启动时就会触发此监听器调用相应的方法创建spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.处理post方式提交乱码问题
<filter>
<filter-name>encoding</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>
3.配置springmvc的核心servlet接管请求
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 定位SpringMVC配置文件路径,因为SpringMVC与Spring在同一配置文件下,所以直接写Spring的xml -->
<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>/</url-pattern>
</servlet-mapping>