1.创建project,添加web服务
2.在web.xml中配置spring和springmvc
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
**第4部中会配置applicationContext.xml**
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.在WEB-INF下与web.xml同级目录下创建springmvc配制文件
<context:component-scan base-package="com.atguigu.mybatis" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler/>
4.配置配置applicationContext.xml
<!-- <spring希望管理所有的业务逻辑组件-->
<context:component-scan base-package="com.atguigu.mybatis">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入数据库配制文件-->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<!-- 控制业务逻辑,事务控制等-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- spring事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启基于注解的事务-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!-- 整合mybatis
目的:spring管理所有组件,包括mapper的实现类 , service调用dao时, @Autowired:自动注入mapper
spring管理事务,
-->
<!-- 创建sqlsessionfactory对象-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源-->
<property name="dataSource" ref="dataSource"></property>
<!-- configLocation 指定全局配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:mybatis.mapper"></property>
</bean>
<!-- 扫描所有的mapper接口的实现类,让这些mapper能注入-->
<mybatis-spring:scan base-package="com.atguigu.mybatis.dao"></mybatis-spring:scan>