需要的jar包

​springIOC需要的jar包​

MyBatis和Spring和SpringMVC的简单整合_spring


​jdbc需要的jar包​

MyBatis和Spring和SpringMVC的简单整合_mvc_02


​springmvc需要的jar包​

MyBatis和Spring和SpringMVC的简单整合_spring_03


​JSTL需要的jar包​

MyBatis和Spring和SpringMVC的简单整合_spring_04


​springAOP和spring允许的日志包​

MyBatis和Spring和SpringMVC的简单整合_xml_05


​mybatis运行的jar包和数据交互的包​

MyBatis和Spring和SpringMVC的简单整合_mvc_06


MyBatis和Spring和SpringMVC的简单整合_mvc_07


​mybatis和spring整合的适配包​

MyBatis和Spring和SpringMVC的简单整合_mvc_08

mybatis准备

bean,dao,mybatis全局配置文件,映射文件,什么乱七八糟的基本配置准备好

SpringMVC准备

在web.xml里面配置

<!-- springmvc配置 -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

web.xml的同级目录的spring-servlet.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- SpringMVC控制网站跳转逻辑 -->
<!-- 只扫描控制器 -->
<context:component-scan base-package="bean" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<context:component-scan base-package="dao" 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/>

</beans>

spring准备

配置springIOC容器随web一起启动,在web.xml里面配置

<!--Spring配置: needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

spring的配置文件applicationContext.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- Spring管理所有的业务逻辑组件 -->
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 引入数据库配置文件 -->
<context:property-placeholder location="classpath:jdbcconfig.properties"/>
<!-- Spring用来控制业务逻辑,数据源,事务控制,AOP... -->
<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.usename}"></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
作用目的:1.spring来管理所有组件,比如那些mapper实现类
这样就不用在代码里面写getmapper了
比如service层调用Dao层的时候,就直接@Auto wired:自动注入mapper就行
2.spring来管理事务,比如spring的声明式事务
-->
<!-- 配置出一个SqlSessionFactory对象 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- configLocation指定mybatis的全局配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis.xml"></property>
<!-- 指定mapper文件的位置 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
</bean>

<!--配置一个可以进行批量执行的sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>


<!-- 扫描所有的mapper接口,让这些mapper能够自动注入 -->
<mybatis-spring:scan base-package="dao"/>
<!--也可以这样写
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao"></property>
</bean>
-->
</beans>