1.web.xml
(1)spring的文档路径及监听
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config/applicationContext.xml
</param-value>
</context-param>
<!-- 配置Spring上下文监听器,它的作用就是在启动WEB容器时,就会自动装在我们applicationContext.xml配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
(2)SpringMVC的基本文档路径及对应的过滤内容
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2.spring MVC applicationContext.xmlspringmvc-servlet.xml
(1)controller控制器的路径
<!-- scan the package and the sub package -->
<context:component-scan base-package="com.test.controller"/>
(2)对静态资源的处理(不获取静态资源的handle)
<!-- don't handle the static resource -->
<mvc:default-servlet-handler />
(3)对java中含有注解的类的处理
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />
(4)页面解析
<!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.Spring applicationContext.xml
一般与Spring的集合的框架都要在这里配置,所以指定mybatis文档路径的配置也在这里配置。
(1)数据库信息的配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8"
p:username="root" p:password="root"
>
</bean>
(2)service层注入到bean
<context:component-scan base-package="com.test.service"/>
(3)mybatis的数据源配置、基本配置文档路径及映射文档路径
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dataSource"/>
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="classpath:config/Configuration.xml" />
<!-- 所有配置的mapper文件 通过ClassPathXmlApplicationContext。getbean()时传入小写的接口名 -->
<property name="mapperLocations" value="classpath*:com/test/mapper/*.xml" />
</bean>
(4)dao层注入到bean
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.inter" />
</bean>
(5)对数据源进行数据管理
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
4.mybatis的configuration.xml
(1)配置实体类的别名
<configuration>
<typeAliases>
<typeAlias alias="User" type="com.test.model.User"/>
<typeAlias alias="Article" type="com.test.model.Article"/>
</typeAliases>
</configuration>
5.mybatis的映射文档
略