SpringMVC配置
web.xml
<!-- 前端控制器 --> <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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 中文乱码过滤器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
spring.xml
<!-- 加载属性配置文件 --> <util:properties id="db" location="classpath:db.properties"/> <!-- 定义数据源 --> <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="#{db.driver}"/> <property name="url" value="#{db.url}"/> <property name="username" value="#{db.user}"/> <property name="password" value="#{db.pwd}"/> </bean> <!-- 定义SQLSessionFactoryBean组件 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" > <!-- 没有了MyBatis的主配置文件 --> <!-- 需要指定连接资源 --> <property name="dataSource" ref="ds"></property> <!-- 需要指定映射文件 --> <property name="mapperLocations" value="classpath:com/xms/entity/mapper/*.xml"></property> </bean> <!-- 开启注解扫描 --> <context:component-scan base-package="com.xms"/> <!-- 开启RequestMapping注解扫描 --> <mvc:annotation-driven/> <!-- 定义视图解析器ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 全局异常处理 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.Exception">error</prop> </props> </property> </bean> <!-- 定义MapperScannerrConfigurer扫描组件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > <!-- 指定Mapper接口扫描包 --> <property name="basePackage" value="com.xms.dao" ></property> <!-- 手动指定SqlSessionFactory对象 --> <!-- sqlSessionFactory属性可以不用指定,它会以Autowired方式自动注入 --> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" ></property> <!-- 推荐使用注解方法 --> <property name="annotationClass" value="com.xms.common.MyAnnontation" ></property> <!-- 接口方法 --> <!-- <property name="markerInterface" value="com.xms.common.Myinterface" /> --> </bean>
@context:compontent:表示可以在类中使用注解的方式,等价于原有的XML配置中的<bean>定义